解决Django发送中文邮件时的编码及乱码问题

时间:2021-10-28 22:05:56

参考自---http://blog.csdn.net/clh604/article/details/9274793

#-*- coding=utf8 -*-
from email.message import Message
from email.header import Header
import smtplib msg=Message()
msg['Mime-Version']='1.0'
msg['Content-type']='text/html'
msg['From']="flow***@sina.com"
msg['To']="beckyXXX@sina.com"
msg['Subject']=Header("测试","utf8")#用Header对象处理主题乱码 msg.set_payload("<p>Hello,</p><p>content</p><p>Best Rgds</p>",'utf8')#用Message对象处理正文乱码 server=smtplib.SMTP()
server.connect(host, port) 
server.login(user, password) server.sendmail(msg['From'], msg['To'], msg.as_string())
server.close() 

More:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832745198026a685614e7462fb57dbf733cc9f3ad000