#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#EMAIL_switch是我的邮件开关,在项目中使用时候判断是否发送邮件
from lightning_ape_api.settings import EMAIL_switch
class Notice:
def email(self,message,subject):
if EMAIL_switch ==1:
sender = '发件人邮箱'
receivers = ['****@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText(message, 'plain', 'utf-8')
message['From'] = Header("发送者", 'utf-8') # 发送者
message['To'] = Header("接收者", 'utf-8') # 接收者
subject = subject
message['Subject'] = Header(subject, 'utf-8')
try:
password='这是密码,有些邮箱需要使用授权码'
smtp=smtplib.SMTP_SSL('smtp.exmail.qq.com')
smtp.connect('smtp.exmail.qq.com',465)
smtp.login(sender,password)
smtp.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
else:
print("邮件开关未开启")
if __name__=='__main__':
print(Notice().email(message='test',subject='test'))
Python实现发送邮件
最新推荐文章于 2023-07-28 14:44:27 发布