python腳本發(fā)郵件,一般會用到smtplib和email這兩個模塊??纯丛撃K怎么使用,先看smtplib模塊。
smtplib模塊定義了一個簡單的SMTP客戶端,可以用來在互聯(lián)網(wǎng)上發(fā)送郵件。
定義的類有如下:
1 2 3 4 5 6 7 8 9 | 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參數(shù)被定義,則smtp會在初始化期間自動調(diào)用connect()方法,如果connect()方法失敗,則會觸發(fā)SMTPConnectError異常,timeout參數(shù)設置了超時時間。在一般的調(diào)用過程中,應該遵connetc()、sendmail()、quit()步驟。
下面我們來看看該類的方法:
SMTP.set_debuglevel(level)
設置輸出debug調(diào)試信息,默認不輸出調(diào)試信息。
SMTP.docmd(cmd[, argstring])
發(fā)送一個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指令,這個方法調(diào)用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])
發(fā)送郵件,該方法需要一些郵件地址和消息。
SMTP.quit()
終止smtp會話并且關閉連接。
經(jīng)過搜索學習發(fā)現(xiàn)網(wǎng)上大多都是用smtp類的sendmail這個方法來發(fā)郵件,那就先看看這個例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #!/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() |
下面這個例子采用原始的smtp指令來發(fā)郵件。常用的smtp指令如下:
HELO 向服務器標識用戶身份
MAIL 初始化郵件傳輸 mail from:
RCPT 標識單個的郵件接收人;常在MAIL命令后面,可有多個rcpt to:
DATA 在單個或多個RCPT命令后,表示所有的郵件接收人已標識,并初始化數(shù)據(jù)傳輸,以.結束
VRFY 用于驗證指定的用戶/郵箱是否存在;由于安全方面的原因,服務器常禁止此命令
EXPN 驗證給定的郵箱列表是否存在,擴充郵箱列表,也常被禁用
HELP 查詢服務器支持什么命令
NOOP 無操作,服務器應響應OK
QUIT 結束會話
RSET 重置會話,當前傳輸被取消
MAIL FROM 指定發(fā)送者地址
RCPT TO 指明的接收者地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #!/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() |
注意在以上兩個例子中在發(fā)送消息的subject后面書寫正文時,需要空一行,正文以'.'結尾。
果然簡單,如果想在郵件中攜帶附件、使用html書寫郵件,附帶圖片等等,就需要使用email模塊及其子模塊。下面來看看email包,email包是用來管理email信息的,它包括MIME和其他基于RFC 2822的消息格式。email包的主要特征是在它內(nèi)部解析和生成email信息是分開的模塊來實現(xiàn)的。
MIME消息由消息頭和消息體兩大部分組成,在郵件里就是郵件頭和郵件體。郵件頭與郵件體之間以空行進行分隔。
郵件頭包含了發(fā)件人、收件人、主題、時間、MIME版本、郵件內(nèi)容的類型等重要信息。每條信息稱為一個域,由域名后加“: ”和信息內(nèi)容構成,可以是一行,較長的也可以占用多行。域的首行必須“頂頭”寫,即左邊不能有空白字符(空格和制表符);續(xù)行則必須以空白字符打頭,且第一個空白字符不是信息本身固有的。
郵件體包含郵件的內(nèi)容,它的類型由郵件頭的“Content-Type”域指出。最常見的類型有text/plain(純文本)和text/html(超文本)。郵件體被分為多個段,每個段又包含段頭和段體兩部分,這兩部分之間也以空行分隔。常見的multipart類型有三種:multipart/mixed, multipart/related和multipart/alternative。
在email的包里面包含了很多模塊:
email.message
email.parser
email.generator
email.mime 創(chuàng)建email和MIME對象
email.header
email.charset
email.encoders
email.ereors
email.utils
email.iterators
主要來看看email.mime,在郵件中攜帶附件、圖片、音頻時,主要使用的是該模塊。一般情況下,你通過解析一個文件或者一段text來生成一個消息對象結構,你也可以從頭開始建立一個消息結構,實際上,你可以給一個已經(jīng)存在的消息結構追加一個新的消息對象。你可以通過創(chuàng)建message實例來創(chuàng)建一個對象結構,然后給該結構追加附件和頭部信息。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]]]])
1 | 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. |
1 | 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
模擬在郵件內(nèi)容中攜帶圖片,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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() |
參考:
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