day12_框架一sendmail.py代码

import yagmail
# 发送邮件,user为发送邮件的邮箱名,password为邮箱授权码,host为邮箱服务器,cc为抄送的邮箱
# to为接收者的邮箱多个邮箱用list,subject为邮件标题,contents为邮件正文,attachments为邮件带的附件(即生成的最新的报告)
class Sendmail(object):
def __init__(self,user,password,host,to,cc,subject,contents,attachments=None):
self.user = user # 发送者邮箱
self.password = password # 发送者邮箱授权码
self.host = host # 邮箱服务器
self.to = to # 接收者的邮箱地址
self.cc = cc # 抄送邮件
self.subject = subject # 邮件标题
self.contents = contents # 邮件正文
self.attachments = attachments # 附件

def send_mail(self):
yag = yagmail.SMTP(user=self.user, password=self.password, host=self.host)
try:
yag.send(to=self.to,cc=self.cc,subject=self.subject,contents=self.contents,attachments=self.attachments)
print('邮件发送成功!')
except Exception as e:
print('邮件发送出错,请检查!错误是%s' % e)

if __name__ == '__main__':
user = 'jiangshuihengliu@126.com' # 发件箱
password = 'a123456' # 邮箱授权码
host = 'smtp.126.com' # 邮件服务器
to = 'sunshujiang184@163.com' # 收件邮箱
cc = ["174596537@qq.com"] # 抄送邮箱
subject = '测试报告' # 邮件标题
contents = '这是测试' # 附件内容
attachments = r'D:jxzapi_auto eportshtml' # 附件
y = Sendmail(user, password, host, to, cc, subject, contents)
y.send_mail()
原文地址:https://www.cnblogs.com/laosun0204/p/8616085.html