Commit 23cb9f19 authored by shenshuo's avatar shenshuo

发邮件异步支持

parent 410d7db9
......@@ -80,6 +80,68 @@ class SendMail(object):
print(str(e))
return False
class SendMailAsync(object):
def __init__(self, mail_host, mail_port, mail_user, mail_password, mail_ssl):
"""
:param mail_host: SMTP主机
:param mail_port: SMTP端口
:param mail_user: SMTP账号
:param mail_password: SMTP密码
:param mail_ssl: SSL=True, 如果SMTP端口是465,通常需要启用SSL, 如果SMTP端口是587,通常需要启用TLS
"""
self.mail_host = mail_host
self.mail_port = mail_port
self.__mail_user = mail_user
self.__mail_password = mail_password
self.mail_ssl = mail_ssl
async def send_mail(self, to_list, subject, content, subtype='plain', att=None):
"""
:param to_list: 收件人,多收件人半角逗号分割, 必填
:param subject: 标题, 必填
:param content: 内容, 必填
:param subtype: 格式,默认:plain, 可选html
:param att: 附件,支持单附件,选填
"""
msg = MIMEMultipart()
msg['Subject'] = subject ## 标题
msg['From'] = self.__mail_user ## 发件人
msg['To'] = to_list # 收件人,必须是一个字符串
# 邮件正文内容
msg.attach(MIMEText(content, subtype, 'utf-8'))
if att:
if not os.path.isfile(att):
raise FileNotFoundError('{0} file does not exist'.format(att))
dirname, filename = os.path.split(att)
# 构造附件1,传送当前目录下的 test.txt 文件
att1 = MIMEText(open(att, 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="{0}"'.format(filename)
msg.attach(att1)
try:
if self.mail_ssl:
'''SSL加密方式,通信过程加密,邮件数据安全, 使用端口465'''
# print('Use SSL SendMail')
server = smtplib.SMTP_SSL()
await server.connect(self.mail_host, self.mail_port) # 连接服务器
await server.login(self.__mail_user, self.__mail_password) # 登录操作
await server.sendmail(self.__mail_user, to_list.split(','), msg.as_string())
server.close()
else:
# print('Use TLS SendMail')
'''使用普通模式'''
server = smtplib.SMTP()
await server.connect(self.mail_host) # 连接服务器
await server.login(self.__mail_user, self.__mail_password) # 登录操作
await server.sendmail(self.__mail_user, to_list.split(','), msg.as_string())
server.close()
return True
except Exception as e:
print(str(e))
return False
class SendSms(object):
def __init__(self, REGION, DOMAIN, PRODUCT_NAME, sms_access_key_id, sms_access_key_secret, ):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment