一直想著給框架添加郵件發送功能、所以整理下python下郵件發送功能
首先python是支持郵件的發送、內置smtp庫、支持發送純文本、HTML及添加附件的郵件。之后是郵箱、像163、qq、新浪等郵箱默認關閉SMTP服務,需要我們手動打開,打開后通過發件人郵箱、授權密碼 通過發件人的SMTP服務發送
代碼如下:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
#!/usr/bin/env python # -*- coding: utf_8 -*- from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEBase from email import encoders from email.header import Header from email.utils import parseaddr, formataddr import smtplib class SendEmail: # 發件箱地址 password = "wxqcl258258" # 授權密碼 不是郵箱登錄密碼 # 收件箱地址 smtp_server = "smtp.163.com" # 發件箱服務器地址 def __init__( self ): pass @classmethod def _format_address( cls , text): name, address = parseaddr(text) return formataddr((Header(name, "utf-8" ).encode(), address)) @classmethod def send_email_text( cls ): msg = MIMEText( "測試smtp郵件發送功能" , "plain" , "utf-8" ) # 第一個參數:郵件正文 # 第二個參數:郵件類型 純文本 # 第三個參數:編碼 msg[ "From" ] = SendEmail._format_address( "來自163的一封郵件 <%s>" % SendEmail.outbox) # 發件人姓名與發件箱地址 msg[ "To" ] = SendEmail._format_address( "管理員 <%s>" % SendEmail.inbox) # 收件人姓名與收件箱地址 msg[ "Subject" ] = Header( "來自SMTP的問候" , "utf-8" ).encode() # 郵件標題 try : server = smtplib.SMTP(SendEmail.smtp_server, 25 ) # 構造smtp服務器連接 # server.set_debuglevel(1) # debug輸出模式 默認關閉 server.login(SendEmail.outbox, SendEmail.password) # 登錄smtp服務器 server.sendmail(SendEmail.outbox, [SendEmail.inbox], msg.as_string()) # 發送郵件 server.quit() print "郵件發送成功" except Exception, e: print str (e) print "郵件發送失敗" if __name__ = = '__main__' : SendEmail.send_email_text() |
這只是純文本的內容、可以支持HTML格式的內容、修改內容如下:
msg = MIMEText("測試smtp郵件發送功能", "plain", "utf-8")
內容修改成HTML格式、 “plain”改成 “html”
最后是添加附件的郵件
代碼如下:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
@classmethod def send_email_multipart( cls ): msg = MIMEMultipart() msg[ "From" ] = SendEmail._format_address( "來自163的一封郵件 <%s>" % SendEmail.outbox) # 發件人姓名與發件箱地址 msg[ "To" ] = SendEmail._format_address( "管理員 <%s>" % SendEmail.inbox) # 收件人姓名與收件箱地址 msg[ "Subject" ] = Header( "來自SMTP的問候" , "utf-8" ).encode() # 郵件標題 msg.attach(MIMEText( "測試添加附件的smtp郵件發送功能" , "plain" , "utf-8" )) with open ( "E:\\work\\python project\\CreateProject\\20160421140953.xml" , "rb" ) as f: # 設置附件的MIME和文件名 mime = MIMEBase( "xml" , "xml" , filename = "測試報告.xml" ) # 加上必要的頭信息 mime.add_header( 'Content-Disposition' , 'attachment' , filename = "測試報告.xml" ) mime.add_header( 'Content-ID' , '<0>' ) mime.add_header( 'X-Attachment-Id' , '0' ) # 把附件的內容讀進來: mime.set_payload(f.read()) # 用Base64編碼: encoders.encode_base64(mime) # 添加到MIMEMultipart: msg.attach(mime) try : server = smtplib.SMTP(SendEmail.smtp_server, 25 ) # 構造smtp服務器連接 # server.set_debuglevel(1) # debug輸出模式 默認關閉 server.login(SendEmail.outbox, SendEmail.password) # 登錄smtp服務器 server.sendmail(SendEmail.outbox, [SendEmail.inbox], msg.as_string()) # 發送郵件 server.quit() print "郵件發送成功" except Exception, e: print str (e) print "郵件發送失敗" |
以上就是python郵件發送功能的具體實現代碼,希望對大家的學習有所幫助。