yagmail 實現發郵件
yagmail 可以簡單的來實現自動發郵件功能。
安裝
pip install yagmail
簡單例子
import yagmail #鏈接郵箱服務器 yag = yagmail.SMTP( user="user@126.com", password="1234", 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"])
只需要添加要發送的附件列表即可。