Posted in Python onOctober 27, 2020
之前使用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()
*收件人可使用列表,同时发送给多个邮箱*
接收到的邮件:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。
Python通过yagmail实现发送邮件代码解析
- Author -
铁扇公主声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@