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 = "[email protected]"
mail_pass = "aaaaaa"
sender = "[email protected]"
receivers = ["[email protected]"]
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 = "[email protected]"
mail_pass = "aaaaa"
sender = "[email protected]"
receivers = ["[email protected]"]
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 = "[email protected]"
mail_pass = "abc115698"
sender = "[email protected]"
receivers = ["[email protected]"]
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("[email protected]","aaaaaa")
MSG = MIMEText("hello,send by python..","plain","utf-8")
server.sendmail("[email protected]",["[email protected]"],MSG.as_string())
參考連結: http://www.runoob.com