Python Email应用的中文乱码问题解决方法
2010-06-21 11:43
1066 查看
或者是对python的unicode理解得不透彻,时常遇到各种各样的中文乱码问题,今天,在email应用中,中文乱码又出来捣乱了,经过一段时间的误打误撞,搞出了一个解决方法:
# -*- coding: utf-8 -*-
'''
Created on 2010-6-21
@author: Administrator
'''
import email
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import encoders
import smtplib
import mimetypes
from_addr = 'qgb@123.com'
to_addr = 'kbx@123.com'
subject_header = 'Subject: 巡检报告'
attachment = u'e:/python/数据库巡检记录表.xls' #文件访问时候要求u,否则不能通过
body = '''巡检报告'''
m = MIMEMultipart()
m["To"] = to_addr
m["From"] = from_addr
m["Subject"] = subject_header
ctype, encoding = mimetypes.guess_type(attachment)
print ctype, encoding
maintype, subtype = ctype.split('/', 1)
print maintype, subtype
m.attach(MIMEText(body, 'plain', 'utf-8')) #明确body的编码是utf-8
fp = open(attachment, 'rb')
msg = MIMEBase(maintype, subtype)
msg.set_payload(fp.read())
fp.close()
encoders.encode_base64(msg)
msg.add_header("Content-Disposition", "attachment", filename='巡检报告.xls')
#encoders.encode_base64(msg) #将http头信息再进行一次编码,否则附件的中文名不能用
#上面一句有误
m.attach(msg)
s = smtplib.SMTP('mail.123.com')
s.set_debuglevel(1)
s.sendmail(from_addr, to_addr, m.as_string())
s.quit()
参考资料:
1、Python for UNIX and Linux System Administration, Noab Gift & Jeremy M.Jones, P147-150
2、通过Google找到的网络各兄弟姐妹的Blogs(这里不一一列举,但要一块感谢-^_^-)
# -*- coding: utf-8 -*-
'''
Created on 2010-6-21
@author: Administrator
'''
import email
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import encoders
import smtplib
import mimetypes
from_addr = 'qgb@123.com'
to_addr = 'kbx@123.com'
subject_header = 'Subject: 巡检报告'
attachment = u'e:/python/数据库巡检记录表.xls' #文件访问时候要求u,否则不能通过
body = '''巡检报告'''
m = MIMEMultipart()
m["To"] = to_addr
m["From"] = from_addr
m["Subject"] = subject_header
ctype, encoding = mimetypes.guess_type(attachment)
print ctype, encoding
maintype, subtype = ctype.split('/', 1)
print maintype, subtype
m.attach(MIMEText(body, 'plain', 'utf-8')) #明确body的编码是utf-8
fp = open(attachment, 'rb')
msg = MIMEBase(maintype, subtype)
msg.set_payload(fp.read())
fp.close()
encoders.encode_base64(msg)
msg.add_header("Content-Disposition", "attachment", filename='巡检报告.xls')
#encoders.encode_base64(msg) #将http头信息再进行一次编码,否则附件的中文名不能用
#上面一句有误
m.attach(msg)
s = smtplib.SMTP('mail.123.com')
s.set_debuglevel(1)
s.sendmail(from_addr, to_addr, m.as_string())
s.quit()
参考资料:
1、Python for UNIX and Linux System Administration, Noab Gift & Jeremy M.Jones, P147-150
2、通过Google找到的网络各兄弟姐妹的Blogs(这里不一一列举,但要一块感谢-^_^-)
相关文章推荐
- Struts2之Web应用中文乱码问题的6种解决方法
- python 采集中文乱码问题的完美解决方法
- 【转】python csv 格式文件 中文乱码问题解决方法
- Python实现的json文件读取及中文乱码显示问题解决方法
- Python2.x中文乱码问题解决方法
- Python2.x中文乱码问题解决方法
- Python BeautifulSoup中文乱码问题的2种解决方法
- python 采集中文乱码问题的完美解决方法
- Python BeautifulSoup中文乱码问题的2种解决方法
- Python2.x中文乱码问题解决方法
- Python2.x中文乱码问题解决方法
- python csv 格式文件 中文乱码问题解决方法
- Java 应用显示中文乱码问题的解决方法
- Python BeautifulSoup中文乱码问题的2种解决方法
- Python BeautifulSoup中文乱码问题的2种解决方法
- Python发送Http请求时,中文乱码问题的解决方法
- Linux下将CD音乐提取为mp3的方法和中文乱码问题的解决
- struts2中文乱码问题的解决方法
- JavaWeb中3种中文乱码问题的解决方法
- redhat7中Codeblocks编译c程序乱码问题 中文乱码解决方法