python 中使用yagmail 發(fā)送郵件功能
1.使用前先要安裝 yagmail
pip install yagmail -i https://pypi.douban.com/simple
2.使用QQ郵箱發(fā)送郵件,使用的是授權(quán)碼,需要先到QQ郵箱申請(qǐng)授權(quán)碼。
郵箱設(shè)置-->賬戶
3.yagmail 模塊發(fā)送郵件更加簡(jiǎn)單,四行代碼
# -*- encoding: utf-8 -*- import yagmail def E_mali_jj(fr,key,etype,text,to,attachments): ''' :param fr: 發(fā)送郵箱 :param key: 授權(quán)碼 :param etype: 郵件類型 :param text: 文本 :param to: 接受郵箱 :param attachments: 附件文件地址,空則填'' :return: ''' # 鏈接郵箱服務(wù)器 yag=yagmail.SMTP(user=fr,password=key,host=etype) # 郵箱正文 contents=[text] # 發(fā)送郵件 yag.send(to=to,subject='郵件標(biāo)題',contents=contents, attachments=attachments) #subject 標(biāo)題 yag.close() print("郵件發(fā)送成功") if __name__ == '__main__': E_mali_jj("123456@qq.com", "gwheybuaamrqbihh", 'smtp.qq.com', "郵件正文內(nèi)容", "123456@qq.com", "E:\\proto_code\\Roshan-01-microscript-proto_test-master-src\\src\\report\\report.html")
郵件發(fā)送給多個(gè)人,將接受的郵箱放在列表中即可
# 發(fā)送郵件 yag.send(to = ['123456@qq.com','678910@qq.com', '10111213@qq.com'], subject='subject', contents = contents, attachments="")
4.發(fā)送郵件帶附件
# -*- coding:utf-8 -*- import yagmail yag = yagmail.SMTP( user="157540957@qq.com", password="kayzilfyziulbhbb1", host='smtp.qq.com') """ user: 發(fā)送的郵箱 password: 授權(quán)碼 """ # 郵箱正文 contents = ['測(cè)試發(fā)送郵件'] # 附件 attachments = "D:\\code\\0906\\api_test009\\report\\report.html" # 發(fā)送郵件 try: yag.send(to = '3437871062@qq.com', subject='subject', contents = contents, attachments=attachments) except Exception as e : print("Error: 抱歉!發(fā)送郵件失敗。", e) """ to : 接收者 subject : 郵件主題 contents: 正文 attachments: 附件 """ yag.close()
5.封裝
# -*- coding:utf-8 -*- import yagmail def send(user, password, receiver): yag = yagmail.SMTP( user=user, password=password, host='smtp.qq.com') """ user: 發(fā)送的郵箱 password: 授權(quán)碼 """ # 郵箱正文 contents = ['測(cè)試發(fā)送郵件'] # 附件 attachments = "D:\\code\\0906\\api_test009\\report\\report.html" # 發(fā)送郵件 try: yag.send(to=receiver, subject='subject', contents = contents, attachments=attachments) except Exception as e : print("Error: 抱歉!發(fā)送郵件失敗。", e) """ to : 接收者 subject : 郵件主題 contents: 正文 attachments: 附件 """ yag.close() if __name__ == '__main__': send("123456@qq.com", "kayzilfyziulbhbb1", "45678910@qq.com")
到此這篇關(guān)于python 中使用yagmail 發(fā)送郵件的文章就介紹到這了,更多相關(guān)python yagmail 發(fā)送郵件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 對(duì)圖片進(jìn)行簡(jiǎn)單的處理
介紹一些圖片處理的Python代碼,有了這些處理代碼,我們可以很方便的處理一些圖片,Python這里處理圖片一般都使用Pillow包,有此需求的朋友可以參考下2021-06-06微信小程序python用戶認(rèn)證的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序python用戶認(rèn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07單步調(diào)試 step into/step out/step over 區(qū)
這篇文章主要介紹了單步調(diào)試 step into/step out/step over 區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11如何在Anaconda中打開(kāi)python自帶idle
這篇文章主要介紹了如何在Anaconda中打開(kāi)python自帶idle,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09pycharm工具連接mysql數(shù)據(jù)庫(kù)失敗問(wèn)題
這篇文章主要介紹了pycharm工具連接mysql數(shù)據(jù)庫(kù)失敗問(wèn)題及解決方法,非常不錯(cuò)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04調(diào)用其他python腳本文件里面的類和方法過(guò)程解析
這篇文章主要介紹了調(diào)用其他python腳本文件里面的類和方法過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11python中的np.argmax() 返回最大值索引號(hào)
這篇文章主要介紹了python中的np.argmax() 返回最大值索引號(hào)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06