Python通过yagmail实现发送邮件代码解析
作者:铁扇公主 时间:2022-12-31 13:44:58
之前使用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()
*收件人可使用列表,同时发送给多个邮箱*
接收到的邮件:
来源:https://www.cnblogs.com/princessironfan/p/13220601.html
标签:Python,yagmail,发送,邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP中使用SQL语句教程
2008-09-03 12:17:00
Python可视化tkinter详解
2022-12-31 06:09:19
![](https://img.aspxhome.com/file/2023/9/115509_0s.png)
浅谈五大Python Web框架
2023-12-10 07:33:25
![](https://img.aspxhome.com/file/2023/6/77866_0s.png)
SQL Server连接失败错误及解决
2008-01-28 21:09:00
![](https://img.aspxhome.com/file/UploadPic/20081/28/2008128211959685s.jpg)
用 XSLT 把 XML 数据生成柱状图
2009-05-19 12:46:00
跟老齐学Python之数据类型总结
2023-05-30 07:15:07
网页特效文字之—银箔字
2013-08-07 00:21:39
![](https://img.aspxhome.com/file/UploadPic/20072/20072311613309s.jpg)
利用Python+PyQt5实现简易浏览器的实战记录
2022-10-30 23:16:26
![](https://img.aspxhome.com/file/2023/3/77853_0s.png)
asp简单可逆运算字符串加密解密函数
2010-05-04 16:42:00
将Python脚本打包成MACOSAPP程序过程
2022-03-07 14:20:43
![](https://img.aspxhome.com/file/2023/5/114605_0s.png)
asp检测表单输入EMAIL合法性的函数
2007-10-16 13:39:00
[欣赏] 情景互动广告
2008-08-06 12:59:00
![](https://img.aspxhome.com/file/UploadPic/20088/6/200886131045714s.jpg)
PHP组合模式Composite Pattern优点与实现过程
2023-05-29 02:10:44
Python使用pyautogui模块实现自动化鼠标和键盘操作示例
2022-10-27 16:02:25
长文章自动分页asp实例-支持HTML
2007-10-10 21:29:00
简单了解python gevent 协程使用及作用
2021-11-27 04:05:26
IE中不可遍历的属性
2010-01-19 13:47:00
Active Server Pages 错误 “ASP 0141”
2009-08-19 17:10:00
区分有单选框的选项和普通的选项
2008-04-17 13:44:00
![](/images/sg_trans.gif)
python递归函数绘制分形树的方法
2021-04-22 02:16:02
![](https://img.aspxhome.com/file/2023/9/67339_0s.jpg)