day12_框架二sendmail_new.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.to = to # 接收者的邮箱地址
self.cc = cc # 抄送邮件
self.subject = subject # 邮件标题
self.contents = contents # 邮件正文
self.attachments = attachments # 附件
self.user = user # 发送者邮箱
self.password = password # 发送者邮箱密码
self.host = host # 邮箱服务器

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:\python\byz-code\day12\fuck_rf\report\html'
y = SendMail(user, password, host, to, cc, subject, contents)
y.send_mail()
posted @ 2018-03-25 16:35  laosun0204  阅读(127)  评论(0编辑  收藏  举报