01 | #coding=utf-8 |
02 | |
03 | import smtplib |
04 | from email.mime.text import MIMEText |
05 | from email.mime.multipart import MIMEMultipart |
06 | from email.mime.application import MIMEApplication |
07 | |
08 | class BaseMail: |
09 | def __init__( self , smtp,bSmtpAuth, sender, pwd ): |
10 | self .bSmtpAuth = bSmtpAuth |
11 | self .smtp = smtp |
12 | self .sender = sender |
13 | self .pwd = pwd |
14 | |
15 | def _parserSend( self , sSubject, sContent, lsPlugin): |
16 | return sSubject, sContent, lsPlugin |
17 | |
18 | def send ( self , sSubject, sContent, lsTo, lsCc = [], lsPlugin = [] ): |
19 | mit = MIMEMultipart() |
20 | mit[ 'from' ] = self .sender |
21 | mit[ 'to' ] = ',' .join( lsTo ) |
22 | if lsCc: mit[ 'cc' ] = ',' .join( lsCc ) |
23 | |
24 | codeSubject, codeContent, codePlugin = self ._parserSend(sSubject, sContent, lsPlugin) |
25 | mit.attach( MIMEText( codeContent, 'html' , 'utf-8' ) ) |
26 | mit[ 'subject' ] = codeSubject |
27 | for plugin in codePlugin: |
28 | mitFile = MIMEApplication( plugin[ 'content' ], ) |
29 | mitFile.add_header( 'content-disposition' , 'attachment' , filename = plugin[ 'subject' ] ) |
30 | mit.attach( mitFile ) |
31 | |
32 | server = smtplib.SMTP( self .smtp ) |
33 | #server.set_debuglevel(smtplib.SMTP.debuglevel) |
34 | if self .bSmtpAuth: server.docmd( "EHLO server" ) |
35 | server.starttls() |
36 | server.login( self .sender, self .pwd ) |
37 | server.sendmail( self .sender, lsTo , mit.as_string() ) |
38 | server.close() |
39 | |
40 | class GMail( BaseMail ): |
41 | def __init__( self , sender, pwd ): |
42 | BaseMail.__init__( self , 'smtp.gmail.com' , True , sender, pwd ) |
43 | self .__strcode = 'utf-8' |
44 | |
45 | def _parserSend( self , sSubject, sContent, lsPlugin): |
46 | for i in lsPlugin: |
47 | i[ 'subject' ] = i[ 'subject' ].encode( self .__strcode) |
48 | return sSubject.encode( self .__strcode), sContent.encode( self .__strcode), lsPlugin |
49 | |
50 | class Com63Mail( BaseMail ): |
51 | def __init__( self , sender, pwd ): |
52 | BaseMail.__init__( self , 'smtp.163.com' , False , sender, pwd ) |
53 | self .__strcode = 'utf-8' |
54 | |
55 | def _parserSend( self , sSubject, sContent, lsPlugin): |
56 | for i in lsPlugin: |
57 | i[ 'subject' ] = i[ 'subject' ].encode( 'gbk' ) |
58 | return sSubject, sContent.encode( self .__strcode), lsPlugin |
59 | |
60 | |
61 | if __name__ = = "__main__" : |
62 | |
63 | sSubject = u 'python3000邮件发送测试' |
64 | sContent = u '<font color="#FF0066">热门评论</color>' |
65 | lsPlugin = [{ 'subject' : u '附1abc.txt' , 'content' : u '内容1abc' }, { 'subject' : u '附2abc.txt' , 'content' : u '内容2abc' }] |
66 | gmail = GMail( 'x@gmail.com' , 'password' ) |
67 | lsTo = [ 'xxx@gmail.com' ] |
68 | lsCc = [] |
69 | gmail.send( sSubject, sContent, lsTo, lsCc, lsPlugin ) |
70 | print 'gmail send' |
71 | |
72 | sSubject = u 'python3000邮件发送测试' |
73 | sContent = u '<font color="#FF0066">热门评论</color>' |
74 | lsPlugin = [{ 'subject' : u '附1abc.txt' , 'content' : u '内容1abc' }, { 'subject' : u '附2abc.txt' , 'content' : u '内容2abc' }] |
75 | com163 = Com63Mail( 'x@163.com' , 'password' ) |
76 | lsTo = [ 'xxx@163.com' ] |
77 | lsCc = [] |
78 | com163.send(sSubject, sContent, lsTo, lsCc, lsPlugin) |
79 | print 'com163 send' |
联系客服