Python提供了用于发送邮件的标准库smtplib,但是第三方库yagmail使用更简单。
安装yagmail库:
pip install yagmail
SMTP类和send方法的说明:


===1 单个收件人,邮件内容使用默认样式===
示例代码:
import yagmail
yag = yagmail.SMTP('shaoqian9527@163.com','xqoffice','smtp.163.com')
yag.send('29947277@qq.com','mail测试','Just a test mail.')注意:密码xqoffice是指邮箱的授权密码,不是网页登录密码。如果使用企业邮箱发信,则是用登录密码。
效果:

===2 多个收件人,邮件内容为HTML格式===
示例代码:
import yagmail
yag = yagmail.SMTP('shaoqian9527@163.com','xqoffice','smtp.163.com')
receiver = ['29947277@qq.com','313129791@qq.com']
issue = '邮件 by yagmail'
content = '<h1>欢迎语</h1><p>小千办公欢迎您!</p>'
yag.send(receiver,issue,content)效果:

===3 设置抄送,添加单个附件===
示例代码:
import yagmail
yag = yagmail.SMTP('shaoqian9527@163.com','xqoffice','smtp.163.com',encoding='gbk')
receiver = '29947277@qq.com'
issue = 'yagmail邮件'
content = '<h1>欢迎语</h1><p>xqoffice欢迎您!</p>'
yag.send(
to=receiver,
cc='313129791@qq.com',
subject=issue,
contents=content,
attachments='D:/py/镜像.txt'
)效果:

备注:如果发现发往qq邮箱的中文附件名显示乱码,则需要设置encoding='gbk'。
===4 同时添加多个附件===
示例代码:
import yagmail
yag = yagmail.SMTP('29947277@qq.com','xqoffice','smtp.qq.com',encoding='gbk')
receiver = 'shaoqian9527@163.com'
issue = '邮件demo'
content = '<h1>这是一封派森邮件</h1>'
yag.send(
to=receiver,
cc='313129791@qq.com',
subject=issue,
contents=content,
attachments=['D:/py/国内源.txt',
'D:/py/xqoffice.png']
)效果:

===5 正文为富文本,添加多个附件,同时图片既作为附件之一,又嵌入正文===
示例代码:
import yagmail
yag = yagmail.SMTP('29947277@qq.com','xqoffice','smtp.qq.com',encoding='gbk')
receiver = 'shaoqian9527@163.com'
issue = '邮件demo'
content = ['<h1>自我简介</h1>',
'<p><a href="https://xqoa.taobao.com">小千办公</a>的LOGO如下:</p>',
yagmail.inline('D:/py/xqoffice.png')]
yag.send(
to=receiver,
cc='313129791@qq.com',
subject=issue,
contents=content,
attachments=['D:/py/国内源.txt',
'D:/py/invoice.xlsx',
'D:/py/xqoffice.png']
)效果:


参考资料: