python 郵件發送

Python SMTP發送郵件
SMTP(Simple Mail Transfer Protocol)即簡單郵件傳輸協議,它是一組用於由源地址到目的地址傳送郵件的規則,由它來控制信件的中轉方式。

python的smtplib提供了一種很方便的途徑發送電子郵件。它對smtp協議進行了簡單的封裝。

Python創建 SMTP 對象語法如下:

import smtplib

smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
參數說明:

host: SMTP 服務器主機。 你可以指定主機的ip地址或者域名如:smtp.163.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: 發送消息
這裏要注意一下第三個參數,msg是字符串,表示郵件。我們知道郵件一般由標題,發信人,收件人,郵件內容,附件等構成,發送郵件的時候,要注意msg的格式。這個格式就是smtp協議中定義的格式。

使用第三方 SMTP 服務發送簡單郵件
import smtplib
from email.mime.text import MIMEText
from email.header import Header

#帶HTML格式發送
#第三方 SMTP 服務
mail_host = "smtp.163.com" # 設置服務器,即你的郵箱服務器
mail_user = "[email protected]" # 用戶名
mail_pass = "xxxx" # 口令,你的郵箱密碼

sender = '[email protected]'#發送方的郵箱地址
receivers = ['[email protected]'] # 接收郵件,可設置爲你的QQ郵箱或者其他郵箱

mail_msg = """
<p>Python 郵件發送測試...</p>
<p><a href="http://blog.csdn.net/mtbaby">這是一個鏈接</a></p&gt;
"""
message = MIMEText(mail_msg, 'html', 'utf-8') #郵件內容
message['From'] = Header("hello", '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格式的郵件
import smtplib
from email.mime.text import MIMEText
from email.header import Header

#帶附件發送
#第三方 SMTP 服務
mail_host = "smtp.163.com" # 設置服務器,即你的郵箱服務器
mail_user = "[email protected]" # 用戶名
mail_pass = "xxxx" # 口令,你的郵箱密碼

sender = '[email protected]'#發送方的郵箱地址
receivers = ['[email protected]'] # 接收郵件,可設置爲你的QQ郵箱或者其他郵箱

#創建一個帶附件的實例
message = MIMEMultipart()
message['From'] = Header("hello", 'utf-8')#括號裏的對應發件人郵箱暱稱、發件人郵箱賬號
message['To'] = Header("測試", 'utf-8')#括號裏的對應收件人郵箱暱稱
subject = 'Python SMTP 郵件測試' #郵件的主題,也可以說是標題
message['Subject'] = Header(subject, 'utf-8')

#郵件正文內容
message.attach(MIMEText('這是MTbaby 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="this is test.txt content"'
message.attach(att1)

#構造附件2,傳送當前目錄下的 test2.txt 文件
att2 = MIMEText(open('test2.txt', 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="this is test2.txt content"'
message.attach(att2)

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 發送帶附件的郵件
發送帶附件的郵件,首先要創建MIMEMultipart()實例
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart

#帶附件發送
#第三方 SMTP 服務
mail_host = "smtp.163.com" # 設置服務器,即你的郵箱服務器
mail_user = "[email protected]" # 用戶名
mail_pass = "xxxx" # 口令,你的郵箱密碼

sender = '[email protected]'#發送方的郵箱地址
receivers = ['[email protected]'] # 接收郵件,可設置爲你的QQ郵箱或者其他郵箱

#創建一個帶附件的實例
message = MIMEMultipart()
message['From'] = Header("hello", 'utf-8')#括號裏的對應發件人郵箱暱稱、發件人郵箱賬號
message['To'] = Header("測試", 'utf-8')#括號裏的對應收件人郵箱暱稱
subject = 'Python SMTP 郵件測試' #郵件的主題,也可以說是標題
message['Subject'] = Header(subject, 'utf-8')

#郵件正文內容
message.attach(MIMEText('這是MTbaby 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="this is test.txt content"'
message.attach(att1)

#構造附件2,傳送當前目錄下的 test2.txt 文件
att2 = MIMEText(open('test2.txt', 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="this is test2.txt content"'
message.attach(att2)

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: 無法發送郵件"

在 HTML 文本中添加圖片
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

#在HTML中添加圖片
mail_host = "smtp.163.com" # 設置服務器,即你的郵箱服務器
mail_user = "[email protected]" # 用戶名
mail_pass = "xxxx" # 口令,你的郵箱密碼

sender = '[email protected]'#發件人郵箱賬號
receivers = ["xxxxxxxx.com"] # 接收郵件,可設置爲你的QQ郵箱或者其他郵箱

msgRoot = MIMEMultipart('related')
msgRoot['From'] = Header("hello", '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://blog.csdn.net/mtbaby">這是一個鏈接</a></p&gt;
<p>圖片演示:</p>
<p><img src="cid:image1"></p>
"""
msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))

#指定圖片爲當前目錄
fp = open('xx.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

#定義圖片 ID,在 HTML 文本中引用
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)

try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 爲 SMTP 端口號
smtpObj.login(mail_user, mail_pass)##括號中對應的是發件人郵箱賬號、郵箱密碼
smtpObj.sendmail(sender, receivers, msgRoot.as_string())#括號中對應的是發件人郵箱賬號、收件人郵箱賬號、發送郵件
print "郵件發送成功"
except smtplib.SMTPException:
print "Error: 無法發送郵件"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章