SMTP(Simple Mail Transfer Protocol)即簡單郵件傳輸協議,它是一組用於由源地址到目的地址傳送郵件的規則,由它來控制信件的中轉方式。html
python的smtplib提供了一種很方便的途徑發送電子郵件。它對smtp協議進行了簡單的封裝。python
Python建立 SMTP 對象語法以下:服務器
import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
參數說明:app
Python SMTP 對象使用 sendmail 方法發送郵件,語法以下:測試
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
參數說明:編碼
這裏要注意一下第三個參數,msg 是字符串,表示郵件。咱們知道郵件通常由標題,發信人,收件人,郵件內容,附件等構成,發送郵件的時候,要注意 msg 的格式。這個格式就是 smtp 協議中定義的格式。spa
如下執行實例須要你本機已安裝了支持 SMTP 的服務,如:sendmail。代理
如下是一個使用 Python 發送郵件簡單的實例:code
import smtplib from email.mime.text import MIMEText from email.header import Header sender = 'from@runoob.com' receivers = ['429240967@qq.com'] # 接收郵件,可設置爲你的QQ郵箱或者其餘郵箱 # 三個參數:第一個爲文本內容,第二個 plain 設置文本格式,第三個 utf-8 設置編碼 message = MIMEText('Python 郵件發送測試...', 'plain', 'utf-8') message['From'] = Header("菜鳥教程", 'utf-8') # 發送者 message['To'] = Header("測試", 'utf-8') # 接收者 subject = 'Python SMTP 郵件測試' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message.as_string()) print "郵件發送成功" except smtplib.SMTPException: print "Error: 沒法發送郵件"
咱們使用三個引號來設置郵件信息,標準郵件須要三個頭部信息: From, To, 和 Subject ,每一個信息直接使用空行分割。htm
咱們經過實例化 smtplib 模塊的 SMTP 對象 smtpObj 來鏈接到 SMTP 訪問,並使用 sendmail 方法來發送信息。
執行以上程序,若是你本機安裝 sendmail(郵件傳輸代理程序),就會輸出:
$ python test.py 郵件發送成功
若是咱們本機沒有 sendmail 訪問,也可使用其餘郵件服務商的 SMTP 訪問(QQ、網易、Google等)。
import smtplib from email.mime.text import MIMEText from email.header import Header # 第三方 SMTP 服務 mail_host="smtp.XXX.com" #設置服務器 mail_user="XXXX" #用戶名 mail_pass="XXXXXX" #口令 sender = 'from@runoob.com' receivers = ['429240967@qq.com'] # 接收郵件,可設置爲你的QQ郵箱或者其餘郵箱 message = MIMEText('Python 郵件發送測試...', 'plain', 'utf-8') message['From'] = Header("菜鳥教程", 'utf-8') message['To'] = Header("測試", 'utf-8') subject = 'Python SMTP 郵件測試' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) # 25 爲 SMTP 端口號 smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print "郵件發送成功" except smtplib.SMTPException: print "Error: 沒法發送郵件"
Python發送HTML格式的郵件與發送純文本消息的郵件不一樣之處就是將MIMEText中_subtype設置爲html。具體代碼以下:
import smtplib from email.mime.text import MIMEText from email.header import Header sender = 'from@runoob.com' receivers = ['429240967@qq.com'] # 接收郵件,可設置爲你的QQ郵箱或者其餘郵箱 mail_msg = """ <p>Python 郵件發送測試...</p> <p><a href="http://www.runoob.com">這是一個連接</a></p> """ message = MIMEText(mail_msg, 'html', 'utf-8') message['From'] = Header("菜鳥教程", 'utf-8') message['To'] = Header("測試", 'utf-8') subject = 'Python SMTP 郵件測試' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message.as_string()) print "郵件發送成功" except smtplib.SMTPException: print "Error: 沒法發送郵件"
執行以上程序,若是你本機安裝sendmail,就會輸出:
$ python test.py 郵件發送成功
發送帶附件的郵件,首先要建立MIMEMultipart()實例,而後構造附件,若是有多個附件,可依次構造,最後利用smtplib.smtp發送。
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header sender = 'from@runoob.com' receivers = ['429240967@qq.com'] # 接收郵件,可設置爲你的QQ郵箱或者其餘郵箱 #建立一個帶附件的實例 message = MIMEMultipart() message['From'] = Header("菜鳥教程", 'utf-8') message['To'] = Header("測試", 'utf-8') subject = 'Python SMTP 郵件測試' message['Subject'] = Header(subject, 'utf-8') #郵件正文內容 message.attach(MIMEText('這是菜鳥教程Python 郵件發送測試……', 'plain', 'utf-8')) # 構造附件1,傳送當前目錄下的 test.txt 文件 att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 這裏的filename能夠任意寫,寫什麼名字,郵件中顯示什麼名字 att1["Content-Disposition"] = 'attachment; filename="test.txt"' message.attach(att1) # 構造附件2,傳送當前目錄下的 runoob.txt 文件 att2 = MIMEText(open('runoob.txt', 'rb').read(), 'base64', 'utf-8') att2["Content-Type"] = 'application/octet-stream' att2["Content-Disposition"] = 'attachment; filename="runoob.txt"' message.attach(att2) try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message.as_string()) print "郵件發送成功" except smtplib.SMTPException: print "Error: 沒法發送郵件"
$ python test.py 郵件發送成功
郵件的 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 sender = 'from@runoob.com' receivers = ['429240967@qq.com'] # 接收郵件,可設置爲你的QQ郵箱或者其餘郵箱 msgRoot = MIMEMultipart('related') msgRoot['From'] = Header("菜鳥教程", 'utf-8') msgRoot['To'] = Header("測試", 'utf-8') subject = 'Python SMTP 郵件測試' 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')) # 指定圖片爲當前目錄 fp = open('test.png', 'rb') msgImage = MIMEImage(fp.read()) fp.close() # 定義圖片 ID,在 HTML 文本中引用 msgImage.add_header('Content-ID', '<image1>') msgRoot.attach(msgImage) try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, msgRoot.as_string()) print "郵件發送成功" except smtplib.SMTPException: print "Error: 沒法發送郵件"
$ python test.py 郵件發送成功