python發(fā)送郵件庫yagmail

出處:蟲師?python自動(dòng)發(fā)郵件庫yagmail

一般發(fā)郵件方法

我以前在通過Python實(shí)現(xiàn)自動(dòng)化郵件功能的時(shí)候是這樣的:

import smtplib

from email.mime.text import MIMEText

from email.header import Header

# 發(fā)送郵箱服務(wù)器

smtpserver='smtp.sina.com'

# 發(fā)送郵箱用戶/密碼

user='username@sina.com'password='123456'

# 發(fā)送郵箱

sender='username@sina.com'

# 接收郵箱

receiver='receive@126.com'

# 發(fā)送郵件主題

subject='Python email test'

# 編寫HTML類型的郵件正文

msg=MIMEText('你好!','html','utf-8')msg['Subject']=Header(subject,'utf-8')

# 連接發(fā)送郵件

smtp=smtplib.SMTP()

smtp.connect(smtpserver)

smtp.login(user, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

其實(shí),這段代碼也并不復(fù)雜,只要你理解使用過郵箱發(fā)送郵件,那么以下問題是你必須要考慮的:

你登錄的郵箱帳號/密碼

對方的郵箱帳號

郵件內(nèi)容(標(biāo)題,正文,附件)

郵箱服務(wù)器(SMTP.xxx.com/pop3.xxx.com)

yagmail 實(shí)現(xiàn)發(fā)郵件

yagmail 可以更簡單的來實(shí)現(xiàn)自動(dòng)發(fā)郵件功能。

github項(xiàng)目地址:https://github.com/kootenpv/yagmail

安裝

pip install yagmail

簡單例子

import yagmail

#鏈接郵箱服務(wù)器

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']

# 發(fā)送郵件

yag.send('taaa@126.com','subject', contents)

總共四行代碼搞定,是不是比上面的例子簡單太多了。

給多個(gè)用戶發(fā)送郵件

# 發(fā)送郵件

yag.send(['aa@126.com','bb@qq.com','cc@gmail.com'],'subject', contents)

只需要將接收郵箱 變成一個(gè)list即可。

發(fā)送帶附件的郵件

# 發(fā)送郵件

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

只需要添加要發(fā)送的附件列表即可。

我都快感動(dòng)哭了,到哪兒去找這么良心庫去?簡單的有點(diǎn)不像編程語言!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容