打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python 发送邮件 - 代码分享 - 开源中国社区
Python 发送邮件
Sephiroth 发布于 2010年11月15日 19时 (0评) 2人收藏此代码, 我要收藏(?)
标签: 邮件

代码片段(1)

[代码] [Python]代码

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'
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
云景代码:邮件发送Python代码
python3发送邮件出现的问题(转)
批量发邮件方法
邮箱号码大全
Python3 SMTP发送邮件
Send Email in pcDuino by Script and Python | pcDuino: mini PC+Arduino
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服