使用yagmail发送邮件
- 安装yagmail库
#windows系统
pip install yagmail
#苹果或linux系统
pip3 install yagmail
- 导入yagmail库及配置邮箱(以腾讯企业邮箱为例)
import yagmail
#链接邮箱服务器
yag = yagmail.SMTP( user="邮箱", password="授权码", host='smtp.exmail.qq.com',port = 25,smtp_ssl=False)
# 发送邮件
yag.send(to = ['邮箱1','邮箱2'],
subject= '邮件主题',
contents = '内容',
attachments=[r'附件.xlsx'])
#关闭
yag.close()
如果foxmail等客户端收不到附件,可能是因为没有加【port = 25,smtp_ssl=False】,很多教程都没有提,需要注意下。
- 报错问题解决
如果报错提示utf-8,可能是因为电脑名称设置为中文,改成字母即可
如果提示503错误,可能是授权码写成了邮箱密码
如果提示535错误,可能是host='smtp.exmail.qq.com'写错了
- 获取授权码
https://jingyan.baidu.com/article/6181c3e0b12548152ef153db.html - 补充点:修改文字样式
因为邮箱内容是嵌入在HTML里面展示的,所以,可以通过HTML标签修改样式
import yagmail
#链接邮箱服务器
yag = yagmail.SMTP( user="邮箱", password="授权码", host='smtp.exmail.qq.com')
# 发送邮件
yag.send(to = ['邮箱1','邮箱2'],
subject= '邮件主题',
contents = "
<h3>《题都城南庄》</h3>\n
<font size=1 color='red'>去年今日此门中,</font>\n
<font size=1 color='red'>人面桃花相映红。</font>\n
<font size=1 color='red'>人面不知何处去,</font>\n
<font size=1 color='red'>桃花依旧笑春风。</font>
",
attachments=[r'附件.xlsx'])
#关闭
yag.close()
地振高冈,一派溪山千古秀;门朝大海,三合河水万年流。

浙公网安备 33010602011771号