python mail 关于 gmail 163 实例程序 支持正文汉字 和发送附件
2013-06-23 17:22
666 查看
作者:朝阳_tony
邮箱:linzhaolover@gmail.com
2013年6月23日 17:21:59 星期日
转载请注明出处:http://blog.csdn.net/linzhaolove
文章最后程序时间: 2013年6月23日 17:22:10 星期日
学习pyton mail 模块 记录了一个实例程序,支持gmail 和163区分账户发送;支持发送多人,抄送,正文汉字不乱码,发送人汉字姓名;还支持发送附件;
说一下编写这个程序遇到的问题:
1、163 邮件不能发送
目前发送163 的邮件需要ssl加密方式登录:因此登录时要用下面的方式登录;
smtp = smtplib.SMTP_SSL(server) #163 mail
而登录gmail要用下面这种方式
smtp = smtplib.SMTP(server)
smtp.starttls() #gmail mail
2、正文汉字乱码
正文乱码,需要添加下面的信息,默认的正文编码是us-acsii 格式,我们在linux下需要设置为utf-8msg["Accept-Language"]="zh-CN"
msg["Accept-Charset"]="ISO-8859-1,utf-8"
if isinstance(text,unicode):
text = str(text)
msg.attach( MIMEText(text,'plain','utf-8'))
整个实例代码:
#!/usr/bin/env python #-*- encoding: UTF-8 -*- #gmail_report.py import sys, smtplib, base64, StringIO, os, string, time import re from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email.Utils import COMMASPACE, formatdate from email import Encoders from email.header import Header def send_mail(send_people, send_from, send_to, cc_to, subject, text, files=[], server="localhost", user = None, password = None): assert type(send_to)==list assert type(files)==list msg = MIMEMultipart() if send_people : me= ("%s<"+send_from+">") % (Header(send_people,'utf-8'),) msg['From'] = me else : msg['From'] = send_from #print "from pepole",send_people msg['To'] = COMMASPACE.join(send_to) msg['Cc'] = COMMASPACE.join(cc_to) send_to = send_to + cc_to # add cc_to to send_to if not isinstance(subject,unicode): subject = unicode(subject) msg['Subject'] = subject msg['Date'] = formatdate(localtime=True) msg["Accept-Language"]="zh-CN" msg["Accept-Charset"]="ISO-8859-1,utf-8" if isinstance(text,unicode): text = str(text) msg.attach( MIMEText(text,'plain','utf-8')) #add attach file for file in files: part = MIMEBase('application', "octet-stream") part.set_payload( open(file,"rb").read() ) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file)) msg.attach(part) print "connect to %s" % server # check server pattern = re.compile(r'smtp.163.com') match = pattern.match(server) if match: smtp = smtplib.SMTP_SSL(server) #163 mail else : smtp = smtplib.SMTP(server) smtp.starttls() #gmail mail #print server if (user != None): smtp.ehlo() smtp_userid64 = base64.encodestring(user) smtp.docmd("auth", "login " + smtp_userid64[:-1]) if password != None: smtp_pass64 = base64.encodestring(password) smtp.docmd(smtp_pass64[:-1]) print "send mail form %s \n to %s" % (send_from,send_to) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close() print "send mail ok" # # try: # s = smtplib.SMTP(server) # #s = smtplib.SMTP_SSL(server) #163 mail # s.starttls() #gmail mail # s.login(user,password) # s.sendmail(me, send_to, msg.as_string()) # s.close() # return True # except Exception, e: # print str(e) # return False # # # #gmail # Credentials (if needed) #username = 'mymail' #password = 'mypass' #server='smtp.gmail.com:587' # 163 username='mymail' password='mypass' server= 'smtp.163.com:465' #send_people='' send_people="钢铁侠" #send_from='myemail@gmail.com' send_from='myemail@163.com' # send to sb send_to=['test1@gmail.com','test2@gmail.com'] # copy to sb cc_to=['test3@163.com'] # mail title subject = u'钢铁侠发送邮件gmail test report' #text = u'this my test mail, you can ignore' text = """ this my test mail, you can ignore, 测试中文显示部分 """ # add file #file=[r'gmail_report.py',r'readme'] file=[] send_mail(send_people, send_from,send_to,cc_to, subject, text, file, server, username, password)
相关文章推荐
- python发送邮件的实例代码(支持html、图片、附件)
- python发送邮件的实例代码(支持html、图片、附件)
- python发送邮件的实例代码(支持html、图片、附件)
- 基于python的图形化邮件发送程序(支持添加附件)
- C#写的邮箱发送程序源代码---支持主题、正文、附件的编辑和添加!
- 利用asp.net自带邮件组件 发送邮件,支持126,163,gmail,qq等邮箱
- 用Python实现在Linux环境发送带附件的邮件,支持文本/html格式
- Python实现Smtplib发送带有各种附件的邮件实例
- Java MailUtils邮件发送工具类 支持附件
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板
- Python绘制excel表格到邮件正文区并发送脚本实例
- Spring代码实例系列-10:通过Spring Mail发送简单邮件、附件和HTML邮件
- 二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)
- 利用asp.net自带邮件组件 发送邮件,支持126,163,gmail,qq等邮箱
- Windows下python发送邮件_CustomEmail.py[text、html、附件、读取文本到正文]
- python发送邮件,含有正文,附件,正文中含有图片(图片直接在邮件内容中显示)
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板
- Python发送多个附件和支持HTML及纯文本内容的 Email 实现
- 【转】c# 发送email,正文支持html格式,包含附件