之前使用email模块+smtplib模块发送邮件,虽然可以实现功能,但过程比较繁琐,今天发现一个宝藏库(yagmail),可以说是炒鸡好用啦!!!

贴上实现代码,大家细品

yagmail安装

pip install yagmail

封装邮件发送服务:

import yagmail


class SendEmail:
def __init__(self, receiver,subject=None,contents=None,attachment=None):
        """

:param receiver: 收件人
:param attachment: 附件
:param contents: 正文
:param subject: 标题
"""
self.receiver = receiver
self.attachment = attachment
self.contents = contents
self.subject = subject

def send_email(self):
# 链接邮箱服务器
yag = yagmail.SMTP(user="29xxx959@qq.com", password="授权码", host='smtp.qq.com')

# 发送邮件
yag.send(to=self.receiver, subject=self.subject, contents=self.contents, attachments=self.attachment)

测试发送(带附件)

pro_dir = os.getcwd()
report = os.path.join(pro_dir, r'ApiReport_2020_06_28 16_31_00.html')
send = SendEmail('864xxx590@qq.com','ceshifasong', '测试', report)
send.send_email()

*收件人可使用列表,同时发送给多个邮箱*

接收到的邮件:

 

 

做一棵小草,谁也撼动不了………

 

posted on 2020-07-01 17:53  小草小草随风飘摇  阅读(806)  评论(0编辑  收藏  举报