python腳本發郵件,通常會用到smtplib和email這兩個模塊。看看該模塊怎麼使用,先看smtplib模塊。 html
smtplib模塊定義了一個簡單的SMTP客戶端,能夠用來在互聯網上發送郵件。
定義的類有以下:
python
class smtplib.SMTP([host[, port[, local_hostname[, timeout]]]]) class smtplib.SMTP_SSL([host[, port[, local_hostname[, keyfile[, certfile[, timeout]]]]]]) class smtplib.LMTP([host[, port[, local_hostname]]])還有一些已經定義好的異常
exception smtplib.SMTPException exception smtplib.SMTPServerDisconnected exception smtplib.SMTPResponseException exception smtplib.SMTPSenderRefused exception smtplib.SMTPRecipientsRefused exception smtplib.SMTPDataError exception smtplib.SMTPConnectError exception smtplib.SMTPHeloError exception smtplib.SMTPAuthenticationError
這麼多已定義的類中,咱們最經常使用的的仍是smtplib.SMTP類,就具體看看該類的用法:
smtp實例封裝一個smtp鏈接,它支持全部的SMTP和ESMTP操做指令,若是host和port參數被定義,則smtp會在初始化期間自動調用connect()方法,若是connect()方法失敗,則會觸發SMTPConnectError異常,timeout參數設置了超時時間。在通常的調用過程當中,應該遵connetc()、sendmail()、quit()步驟。 安全
下面咱們來看看該類的方法: 服務器
SMTP.set_debuglevel(level)
設置輸出debug調試信息,默認不輸出調試信息。
SMTP.docmd(cmd[, argstring])
發送一個command到smtp服務器,
SMTP.connect([host[, port]])
鏈接到指定的smtp服務器,默認是本機的25端口。也能夠寫成hostname:port的形式。
SMTP.helo([hostname])
使用helo指令向smtp服務器確認你的身份。
SMTP.ehlo([hostname])
使用ehlo指令向esmtp服務器確認你的身份。
SMTP.ehlo_or_helo_if_needed()
若是在之前的會話鏈接中沒有提供ehlo或者helo指令,這個方法調用ehlo()或者helo()。
SMTP.has_extn(name)
判斷指定的名稱是否在smtp服務器上。
SMTP.verify(address)
判斷郵件地址是否在smtp服務器上存在。
SMTP.login(user, password)
登錄須要驗證的smtp服務器,若是以前沒有提供ehlo或者helo指令,則會先嚐試ESMTP的ehlo指令。
SMTP.starttls([keyfile[, certfile]])
使smtp鏈接運行在TLS模式,全部的smtp指令都會被加密。
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
發送郵件,該方法須要一些郵件地址和消息。
SMTP.quit()
終止smtp會話而且關閉鏈接。 app
通過搜索學習發現網上大多都是用smtp類的sendmail這個方法來發郵件,那就先看看這個例子: python2.7
#!/usr/bin/python import smtplib from_mail='xxx@126.com' to_mail='yyy@qq.com' server=smtplib.SMTP('smtp.126.com') server.docmd('ehlo','xxx@126.com') server.login('xxx@126.com','password') msg='''from:xxx@126.com to:yyy@qq.com subject:I am guol I'm come 126 . ''' server.sendmail(from_mail,to_mail,msg) server.quit()上例是使用sendmail方式,採用郵箱驗證的模式來發郵件,由於如今大多數郵件系統都有反垃圾郵件機制及驗證機制,我在開始實驗非驗證模式時就被126看成垃圾郵件而拒絕發送。
下面這個例子採用原始的smtp指令來發郵件。經常使用的smtp指令以下: 學習
HELO 向服務器標識用戶身份
MAIL 初始化郵件傳輸 mail from:
RCPT 標識單個的郵件接收人;常在MAIL命令後面,可有多個rcpt to:
DATA 在單個或多個RCPT命令後,表示全部的郵件接收人已標識,並初始化數據傳輸,以.結束
VRFY 用於驗證指定的用戶/郵箱是否存在;因爲安全方面的緣由,服務器常禁止此命令
EXPN 驗證給定的郵箱列表是否存在,擴充郵箱列表,也常被禁用
HELP 查詢服務器支持什麼命令
NOOP 無操做,服務器應響應OK
QUIT 結束會話
RSET 重置會話,當前傳輸被取消
MAIL FROM 指定發送者地址
RCPT TO 指明的接收者地址 測試
#!/usr/bin/python import base64 import smtplib from_mail='xxx@126.com' to_mail='yyy@qq.com' user=base64.encodestring('xxx@126.com').strip() passwd=base64.encodestring('password').strip() server=smtplib.SMTP('smtp.126.com') server.set_debuglevel(1) server.docmd('ehlo','xxx@126.com') server.docmd('auth login') server.docmd(user) server.docmd(passwd) server.docmd('mail FROM:<%s>' % from_mail) server.docmd('rcpt TO:<%s>' % to_mail) server.docmd('data') server.docmd('''from:xxx@126.com to:yyy@qq.com subject:I am guol I'm come here . ''') server.getreply() server.quit()
注意在以上兩個例子中在發送消息的subject後面書寫正文時,須要空一行,正文以'.'結尾。 ui
果真簡單,若是想在郵件中攜帶附件、使用html書寫郵件,附帶圖片等等,就須要使用email模塊及其子模塊。下面來看看email包,email包是用來管理email信息的,它包括MIME和其餘基於RFC 2822的消息格式。email包的主要特徵是在它內部解析和生成email信息是分開的模塊來實現的。 this
MIME消息由消息頭和消息體兩大部分組成,在郵件裏就是郵件頭和郵件體。郵件頭與郵件體之間以空行進行分隔。
郵件頭包含了發件人、收件人、主題、時間、MIME版本、郵件內容的類型等重要信息。每條信息稱爲一個域,由域名後加「: 」和信息內容構成,能夠是一行,較長的也能夠佔用多行。域的首行必須「頂頭」寫,即左邊不能有空白字符(空格和製表符);續行則必須以空白字符打頭,且第一個空白字符不是信息自己固有的。
郵件體包含郵件的內容,它的類型由郵件頭的「Content-Type」域指出。最多見的類型有text/plain(純文本)和text/html(超文本)。郵件體被分爲多個段,每一個段又包含段頭和段體兩部分,這兩部分之間也以空行分隔。常見的multipart類型有三種:multipart/mixed, multipart/related和multipart/alternative。
在email的包裏面包含了不少模塊:
email.message
email.parser
email.generator
email.mime 建立email和MIME對象
email.header
email.charset
email.encoders
email.ereors
email.utils
email.iterators
主要來看看email.mime,在郵件中攜帶附件、圖片、音頻時,主要使用的是該模塊。通常狀況下,你經過解析一個文件或者一段text來生成一個消息對象結構,你也能夠從頭開始創建一個消息結構,實際上,你能夠給一個已經存在的消息結構追加一個新的消息對象。你能夠經過建立message實例來建立一個對象結構,而後給該結構追加附件和頭部信息。email包提供了一些子類使得該操做變得很容易。
email.mime中經常使用的類以下:
email.mime.base email.mime.base.MIMEBase(_maintype, _subtype, **_params)
email.mime.nonmultipart email.mime.nonmultipart.MIMENonMultipart
email.mime.multipart email.mime.multipart.MIMEMultipart([_subtype[, boundary[, _subparts[, _params]]]])
A subclass of MIMEBase, this is an intermediate base class for MIME messages that are multipart. Optional _subtype defaults to mixed, but can be used to specify the subtype of the message. A Content-Type header of multipart/_subtype will be added to the message object. A MIME-Version header will also be added.email.mime.application email.mime.application.MIMEApplication(_data[, _subtype[, _encoder[, **_params]]])
A subclass of MIMENonMultipart, the MIMEImage class is used to create MIME message objects of major type image. _imagedata is a string containing the raw image data.email.mime.message email.mime.message.MIMEMessage(_msg[, _subtype])
A subclass of MIMENonMultipart, the MIMEText class is used to create MIME objects of major type text. _text is the string for the payload. _subtype is the minor type and defaults to plain. _charset is the character set of the text and is passed as a parameter to the MIMENonMultipart constructor; it defaults to us-ascii. If _text is unicode, it is encoded using the output_charset of _charset, otherwise it is used as-is.
只解釋了經常使用的幾個類,詳細信息見: http://docs.python.org/2/library/email.mime.html
模擬在郵件內容中攜帶圖片,以下:
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage from_mail='xxx@126.com' to_mail='yyy@qq.com' msg=MIMEMultipart() msg['From']=from_mail msg['To']=to_mail msg['Subject']='I am from email package' body='I am come 126,i content with pic' con=MIMEText('<b>%s</b><br><img src="cid:/opt/25343674.png"><br>' % body,'html') msg.attach(con) img=MIMEImage(file('/opt/25343674.png','rb').read()) img.add_header('Content-ID','/opt/25343674.png') msg.attach(img) server=smtplib.SMTP('smtp.126.com') server.docmd('ehlo','xxx@126.com') server.login('yyy@126.com','password') server.sendmail(from_mail,to_mail,msg.as_string()) server.quit()模擬在 郵件中攜帶附件 ,以下:
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage from_mail='xxx@126.com' to_mail='yyy@qq.com' msg=MIMEMultipart() msg['From']=from_mail msg['To']=to_mail msg['Subject']='I am from email package' content=MIMEText('<b>I am come 126,i with attach<b>','html') msg.attach(content) attac=MIMEImage(file('/opt/25343674.png','rb').read()) attac['Content-Type']='application/octet-stream' attac.add_header('content-disposition','attachment',filename='/opt/25343674.png') msg.attach(attac) server=smtplib.SMTP('smtp.126.com') server.docmd('ehlo','xxx@126.com') server.login('yyy@126.com','password') server.sendmail(from_mail,to_mail,msg.as_string()) server.quit()以上代碼均在python2.7下通過測試。
參考:
http://outofmemory.cn/code-snippet/1105/python-usage-smtplib-fayoujian-carry-fujian-code http://docs.python.org/2/library/smtplib.html http://stackoverflow.com/questions/3362600/how-to-send-email-attachments-with-python