#借用 163邮箱实现邮件发送
# 邮件
import smtplib
from email.mime.text import MIMEText # 文本 HTML
from email.header import Header
from email.mime.multipart import MIMEMultipart # 附件
# 邮件提醒 send_mail(email_user, email_pwd, maillist, title, content) 0成功 1 失败
email_user = 'xlipengpeng@163.com' # 借助邮箱账号
email_pwd = '888888' # 发送者密码 手机扩展
maillist = '1461681596@qq.com,duyingshu@icloudata.net,2465379913@qq.com' #发往邮箱 多个逗号隔开
title = '短信积压超量提醒'
content='dddd' #短信内容 可以为HTML
# 发送邮件
def send_mail(username, passwd, recv, title, content, mail_host='smtp.163.com', port=25):
'''
发送邮件函数,默认使用163smtp
:param username: 邮箱账号 xx@163.com
:param passwd: 邮箱密码
:param recv: 邮箱接收人地址,多个账号以逗号隔开
:param title: 邮件标题
:param content: 邮件内容
:param mail_host: 邮箱服务器
:param port: 端口号
:return:
'''
# 创建一个带附件的实例
message = MIMEMultipart()
message['Subject'] = title # 邮件主题
message['From'] = username # 发送者账号
message['To'] = recv # 接收者账号列表
# 邮件正文内容
message.attach(MIMEText(content, 'html', 'utf-8'))
# 构造附件1,传送当前目录下的 test.txt 文件
# att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
# att1["Content-Type"] = 'application/octet-stream'
# # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
# att1["Content-Disposition"] = 'attachment; filename="test.csv"'
# message.attach(att1)
# 构造附件2,传送当前目录下的 runoob.txt 文件
# att2 = MIMEText(open('runoob.txt', 'rb').read(), 'base64', 'utf-8')
# att2["Content-Type"] = 'application/octet-stream'
# att2["Content-Disposition"] = 'attachment; filename="runoob.txt"'
# message.attach(att2)
try:
# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
smtp = smtplib.SMTP(mail_host, port=port) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
smtp.login(username, passwd) # 发送者的邮箱账号,密码
smtp.sendmail(message['From'], message['To'].split(","), message.as_string())
return '0'
except smtplib.SMTPException as e:
print(e)
return '1'
finally:
smtp.close() # 发送完毕后退出smtp