python 中使用yagmail 發(fā)送郵件功能
1.使用前先要安裝 yagmail
pip install yagmail -i https://pypi.douban.com/simple
2.使用QQ郵箱發(fā)送郵件,使用的是授權碼,需要先到QQ郵箱申請授權碼。
郵箱設置-->賬戶

3.yagmail 模塊發(fā)送郵件更加簡單,四行代碼
# -*- encoding: utf-8 -*-
import yagmail
def E_mali_jj(fr,key,etype,text,to,attachments):
'''
:param fr: 發(fā)送郵箱
:param key: 授權碼
:param etype: 郵件類型
:param text: 文本
:param to: 接受郵箱
:param attachments: 附件文件地址,空則填''
:return:
'''
# 鏈接郵箱服務器
yag=yagmail.SMTP(user=fr,password=key,host=etype)
# 郵箱正文
contents=[text]
# 發(fā)送郵件
yag.send(to=to,subject='郵件標題',contents=contents,
attachments=attachments) #subject 標題
yag.close()
print("郵件發(fā)送成功")
if __name__ == '__main__':
E_mali_jj("123456@qq.com",
"gwheybuaamrqbihh",
'smtp.qq.com',
"郵件正文內容",
"123456@qq.com",
"E:\\proto_code\\Roshan-01-microscript-proto_test-master-src\\src\\report\\report.html")
郵件發(fā)送給多個人,將接受的郵箱放在列表中即可
# 發(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: 授權碼
"""
# 郵箱正文
contents = ['測試發(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: 授權碼
"""
# 郵箱正文
contents = ['測試發(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")
到此這篇關于python 中使用yagmail 發(fā)送郵件的文章就介紹到這了,更多相關python yagmail 發(fā)送郵件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
單步調試 step into/step out/step over 區(qū)
這篇文章主要介紹了單步調試 step into/step out/step over 區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
pycharm工具連接mysql數(shù)據(jù)庫失敗問題
這篇文章主要介紹了pycharm工具連接mysql數(shù)據(jù)庫失敗問題及解決方法,非常不錯大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04

