python之yagmail模块--小白博客

时间:2021-07-14 00:59:54

yagmail 实现发邮件

yagmail 可以简单的来实现自动发邮件功能。

安装

pip install yagmail

简单例子

import yagmail

#链接邮箱服务器
yag = yagmail.SMTP( user="user@126.com", password="", host='smtp.126.com') # 邮箱正文
contents = ['This is the body, and here is just text http://somedomain/image.png',
'You can find an audio file attached.', '/local/path/song.mp3'] # 发送邮件
yag.send('taaa@126.com', 'subject', contents)
#关闭
yag.close()

总共四行代码搞定。

给多个用户发送邮件

# 发送邮件
yag.send(['aa@126.com','bb@qq.com','cc@gmail.com'], 'subject', contents)

只需要将接收邮箱 变成一个list即可。

发送带附件的邮件

# 发送邮件
yag.send('aaaa@126.com', '发送附件', contents, ["d://log.txt","d://baidu_img.jpg"])

只需要添加要发送的附件列表即可。

python之yagmail模块--小白博客