python发送email

第一种方法: 
Python代码   收藏代码
  1. # -*- coding: utf-8 -*-  
  2.   
  3. import email  
  4. import mimetypes  
  5. from email.MIMEMultipart import MIMEMultipart  
  6. from email.MIMEText import MIMEText  
  7. from email.MIMEImage import MIMEImage  
  8. import smtplib  
  9.   
  10. def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText):  
  11.   
  12.         strFrom = fromAdd  
  13.         strTo = ', '.join(toAdd)  
  14.   
  15.         server = authInfo.get('server')  
  16.         user = authInfo.get('user')  
  17.         passwd = authInfo.get('password')  
  18.   
  19.         if not (server and user and passwd) :  
  20.                 print 'incomplete login info, exit now'  
  21.                 return  
  22.   
  23.         # 设定root信息  
  24.         msgRoot = MIMEMultipart('related')  
  25.         msgRoot['Subject'] = subject  
  26.         msgRoot['From'] = strFrom  
  27.         msgRoot['To'] = strTo  
  28.         msgRoot.preamble = 'This is a multi-part message in MIME format.'  
  29.   
  30.         # Encapsulate the plain and HTML versions of the message body in an  
  31.         # 'alternative' part, so message agents can decide which they want to display.  
  32.         msgAlternative = MIMEMultipart('alternative')  
  33.         msgRoot.attach(msgAlternative)  
  34.   
  35.         #设定纯文本信息  
  36. #        msgText = MIMEText(plainText, 'plain', 'utf-8')  
  37. #        msgAlternative.attach(msgText)  
  38.   
  39.         #设定HTML信息  
  40.         msgText = MIMEText(htmlText, 'html''utf-8')  
  41.         msgAlternative.attach(msgText)  
  42.   
  43.        #设定内置图片信息  
  44. #        fp = open('test.jpg', 'rb')  
  45. #        msgImage = MIMEImage(fp.read())  
  46. #        fp.close()  
  47. #        msgImage.add_header('Content-ID', '<image1>')  
  48. #        msgRoot.attach(msgImage)  
  49.   
  50.        #发送邮件  
  51.         smtp = smtplib.SMTP()  
  52.        #设定调试级别,依情况而定  
  53.         smtp.set_debuglevel(1)  
  54.         smtp.connect(server)  
  55.         smtp.login(user, passwd)  
  56.         smtp.sendmail(strFrom, strTo, msgRoot.as_string())  
  57. #        smtp.sendmail(strFrom, strTo, msgRoot.as_string())  
  58.         smtp.quit()  
  59.         return  
  60.   
  61. if __name__ == '__main__' :  
  62.         authInfo = {}  
  63.         authInfo['server'] = 'smtp.***.cn'  
  64.         authInfo['user'] = '***@***.cn'  
  65.         authInfo['password'] = '***'  
  66.         fromAdd = '***@***.cn'  
  67.         toAdd = ['***@163.com''***@gamil.com']  
  68.         subject = 'title'  
  69.         plainText = '这里是普通文本'  
  70.         htmlText = '<B>HTML文本</B>'  
  71.         sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText)  

第二种方法: 
Python代码   收藏代码
  1. #coding=utf-8  
  2.   
  3. import smtplib,email,sys  
  4. from email.Message import Message  
  5.   
  6.   
  7. smtpserver='smtp.***.cn'  
  8. smtpuser='***@***.cn'  
  9. smtppass='***'  
  10. smtpport='25'  
  11.   
  12. def connect():  
  13.     "connect to smtp server and return a smtplib.SMTP instance object"  
  14.     server=smtplib.SMTP(smtpserver,smtpport)  
  15.     server.ehlo()  
  16.     server.login(smtpuser,smtppass)  
  17.     return server  
  18.       
  19. def sendmessage(server,to,subj,content):  
  20.     "using server send a email"  
  21.     msg = Message()  
  22.     msg['Mime-Version']='1.0'  
  23.     msg['From']    = smtpuser  
  24.     msg['To']      = to  
  25.     msg['Subject'] = subj  
  26.     msg['Date']    = email.Utils.formatdate()          # curr datetime, rfc2822  
  27.     msg.set_payload(content)  
  28.     try:      
  29.         failed = server.sendmail(smtpuser,to,str(msg))   # may also raise exc  
  30.     except Exception ,ex:  
  31.         print Exception,ex  
  32.         print 'Error - send failed'  
  33.     else:  
  34.         print "send success!"  
  35.   
  36. if __name__=="__main__":  
  37.     #frm=raw_input('From? ').strip()  
  38. #    to=raw_input('To? ').strip()  
  39. #    subj=raw_input('Subj? ').strip()   
  40.     to='***@***.com'  
  41.     subj='title1111'      
  42.     print 'Type message text, end with line="."'  
  43.     text = 'content'  
  44. #    while True:  
  45. #        line = sys.stdin.readline()  
  46. #        if line == '. ': break  
  47. #        text += line  
  48.     server=connect()  
  49.     sendmessage(server,to,subj,text)      

<iframe style="font-size: 12px; line-height: 18px;" src="http://canofy.iteye.com/iframe_ggbd/187" frameborder="0" scrolling="no" width="680" height="90"></iframe>

 
第一种方法 line 56 有个bug:收件人为多人时,只有第一个收件人能收到。
Python代码   收藏代码
  1. smtp.sendmail(strFrom, strTo, msgRoot.as_string())    

改为
Python代码   收藏代码
  1. smtp.sendmail(strFrom, toAdd, msgRoot.as_string())    

即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值