在LINUX平臺下使用python實現一個郵件收發程序
最近在服務器上運行一個爬蟲程序,兩天完成一輪後需要手動修改,所以需要在程序運行結束後發送一個提醒郵件。
python實現
1:通過QQ郵箱發送和接收
需要輸入收發方的郵箱地址,發送方不需要QQ密碼,但需要設置QQ郵箱的第三方服務授權碼參考授權碼獲取方法
2:源碼實現
def Email(Subject,content):
"""
Subject:郵件標題
content:發送內容,字符串類型
"""
import smtplib
from email.mime.text import MIMEText
_user = "[email protected]" # 郵件發送方郵箱
_pwd = "授權碼" # 郵件發送方郵箱第三方服務授權碼,不是密碼
_to = "[email protected]" # 郵件接收方郵箱
msg = MIMEText(content) # 郵件內容
msg["Subject"] = Subject # 郵件標題
msg["From"] = _user # 郵件發送方
msg["To"] = _to # 郵件發送對象
try:
s = smtplib.SMTP_SSL("smtp.qq.com",465)
s.login(_user,_pwd)
s.sendmail(_user,_to,msg.as_string())
s.quit()
print("Success")
except smtplib.SMTPException.e:
print("Falied,%s"%e)
if __name__ == '__main__':
Email("cj","測試郵件")