SMTP(Simple Mail Transfer Protocol)即簡單郵件傳輸協議,它是一組用於由源地址到目的地址傳送郵件的規則,由它來控制信件的中轉方式
python的smtplib提供了一種很方便的途徑傳送電子郵件。它對smtp協議進行了簡單的封裝。
Python建立 SMTP 物件語法:
import smtplib
smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
引數說明:
host: SMTP 伺服器主機。 你可以指定主機的ip地址或者域名如: runoob.com,這個是可選引數。
port: 如果你提供了 host 引數, 你需要指定 SMTP 服務使用的埠號,一般情況下 SMTP 埠號為25。
local_hostname: 如果 SMTP 在你的本機上,你只需要指定伺服器地址為 localhost 即可。
Python SMTP 物件使用 sendmail 方法傳送郵件,語法如下:
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options]
引數說明:
from_addr: 郵件傳送者地址。
to_addrs: 字串列表,郵件傳送地址。
msg: 傳送訊息
# 使用163郵箱給自己發郵件資訊
import smtplib from email.mime.text import MIMEText from email.header import Header mail_host = "smtp.163.com" mail_user = "aaaaaa@163.com" mail_pass = "aaaaaa" sender = "aaaaa@163.com" receivers = ["aaaaaa@163.com"] message = MIMEText("python 郵件測試","plain","utf-8") message["From"] = Header("ceshi","utf-8") message["To"] = Header("CESHI","UTF-8") subject = "python semp email ceshi ...." message["Subject"] = Header(subject,"utf-8") try: smtpOBJ = smtplib.SMTP() smtpOBJ.connect(mail_host, 25) smtpOBJ.login(mail_user,mail_pass) smtpOBJ.sendmail(sender,receivers,message.as_string()) except smtplib.SMTPException: print("error")
# 使用163郵箱傳送資訊,帶附件
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header mail_host = "smtp.163.com" mail_user = "aaaaa@163.com" mail_pass = "aaaaa" sender = "aaaaa@163.com" receivers = ["aaaaa@163.com"] message = MIMEMultipart() message["From"] = Header("wo","utf-8") message["To"] = Header("ni","utf-8") subject = "python smtp text, 附件" message["Subject"] = Header("subject","utf-8") message.attach(MIMEText('這是菜鳥教程Python 郵件傳送測試……', 'plain', 'utf-8')) att1 = MIMEText(open("tt.yaml","r").read(),"base64","utf-8") att1["Content-Type"] = "application/octet-stream" att1["Content-Disposition"] = 'attachment; filename="tt.yaml"' message.attach(att1) try: smtpOBJ = smtplib.SMTP() smtpOBJ.connect(mail_host, 25) smtpOBJ.login(mail_user,mail_pass) smtpOBJ.sendmail(sender,receivers,message.as_string()) except smtplib.SMTPException: print("error")
# 使用163郵箱傳送資訊,帶附件和html
在 HTML 文字中新增圖片
郵件的 HTML 文字中一般郵件服務商新增外鏈是無效的,正確新增突破的例項如下所示:
import smtplib from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header mail_host = "smtp.163.com" mail_user = "15800220134@163.com" mail_pass = "abc115698" sender = "15800220134@163.com" receivers = ["15800220134@163.com"] msgRoot = MIMEMultipart("related") msgRoot["From"] = Header("wo") msgRoot["To"] = Header("ni") subject = "Python SMTP 郵件測試,html + 圖片" msgRoot["Subject"] = Header(subject,"utf-8") msgAlternative = MIMEMultipart("alternative") msgRoot.attach(msgAlternative) mail_msg = """ <p>Python 郵件傳送測試...</p> <p><a href="http://www.runoob.com">菜鳥教程連結</a></p> <p>圖片演示:</p> <p><img src="cid:image1"></p> """ msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8')) fb = open("1111.jpg","rb") msgImage = MIMEImage(fb.read()) fb.close() msgImage.add_header('Content-ID', '<image1>') msgRoot.attach(msgImage) try: smtpOBJ = smtplib.SMTP() smtpOBJ.connect(mail_host, 25) smtpOBJ.login(mail_user,mail_pass) smtpOBJ.sendmail(sender,receivers,msgRoot.as_string()) except smtplib.SMTPException: print("error")
# 驗證開通郵箱的smtp協議的授權碼,並驗證登入
驗證程式碼:
import smtplib from email.mime.text import MIMEText server = smtplib.SMTP("smtp.163.com",25) server.login("aaaaaa@163.com","aaaaaa") MSG = MIMEText("hello,send by python..","plain","utf-8") server.sendmail("aaaaaa@163.com",["aaaaaaa@163.com"],MSG.as_string())
參考連結: http://www.runoob.com