yagmail的GitHub地址:https://github.com/kootenpv/yagmail
import yagmail
#登陸自己的郵箱
yagmail.register('yourmail@163.com','password')
yag = yagmail.SMTP(user="yourmail@163.com", password="password",host='smtp.163.com')
#編輯郵件內(nèi)容
contentsbody = ['這是一封測(cè)試郵件']
#發(fā)送郵件
yag.send(to = 'person1@163.com', subject='[title:測(cè)試郵件]', contents = contentsbody)
發(fā)送成功后,郵箱里就收到了你剛才編輯后的郵件了志衣。
如果想要發(fā)送帶html格式的郵件:
先在文件目錄下編輯一個(gè)郵件模板html文檔,再讀取為內(nèi)容:
# 編輯郵件
#讀取郵件模板
file_object = open('mailcontent.html')
try:
contentsbody = file_object.read()
finally:
file_object.close( )
contents = contentsbody
#發(fā)送郵件
yag.send(to = 'winterfzw@163.com', subject='[html郵件]', contents = contents)
print("郵件發(fā)送成功")
出現(xiàn)問(wèn)題:
1、yagmail默認(rèn)使用Gmail郵箱服務(wù),因此在第一步登陸中榛搔,遇到服務(wù)器沒(méi)法發(fā)送的問(wèn)題。
我一開始是修改了yagmail里sender.py中的配置信息齿风,把host手動(dòng)改成了host='smtp.163.com'药薯,后來(lái)發(fā)現(xiàn)可以直接在yag = yagmail.SMTP里設(shè)置參數(shù)绑洛。