闪电邮打开正文,中文显示正常。
微信的腾讯企业邮打开正文,中文是乱码。
正文用:
MIMEText(body, 'plain', 'utf-8')
附件用:
data = open(file_name, 'rb')
part.set_payload(data.read().decode('utf-8').encode('gbk'))
-
#!/usr/bin/env python2.6
-
#coding: utf-8
-
# Filename: Email.py
-
# FileDesc: 发送邮件功能
-
-
import smtplib
-
from email.mime.text import MIMEText
-
from email.mime.multipart import MIMEMultipart
-
from email.MIMEBase import MIMEBase
-
from email import Encoders
-
import time
-
import datetime
-
-
test_mailto=["cat@qq.com","lin@qq.com"]
-
-
def send_mail(mailto, body, subject, mail_user="linkwin",
-
mail_pwd="Linkwinisbest1", user_alias="linkwin",
-
mail_server="smtp.exmail.qq.com", mail_postfix="richba.com", format='plain'):
-
sender = mail_user+"@"+mail_postfix
-
send_mail_with_file(mailto,body,subject,format,sender,mail_user,mail_pwd, user_alias,mail_server)
-
-
def send_mail_with_file( mailto, body, subject, format='plain',
-
sender="linkwin@richba.com", user="linkwin",
-
password="Linkwinisbest1", user_alias="linkwin",
-
mail_server="smtp.exmail.qq.com", appendix=[] ):
-
msg = MIMEMultipart()
-
msg["to"] = ";".join(mailto)
-
msg["from"] = user_alias+"<"+sender+">"
-
msg["subject"] = subject
-
-
body_msg = MIMEText(body, format, 'utf-8')
-
msg.attach(body_msg)
-
-
ctype='application/octet-stream'
-
maintype,subtype = ctype.split('/',1)
-
for file_name in appendix:
-
part = MIMEBase(maintype, subtype)
-
data = open(file_name, 'rb')
-
part.set_payload(data.read().decode('utf-8').encode('gbk'))
-
data.close()
-
Encoders.encode_base64(part)
-
part["Content-Type"] = ctype
-
desc = "attachment; filename=\"%s\"" % file_name.split("/")[-1]
-
part["Content-Disposition"] = desc
-
msg.attach(part)
-
try:
-
print("[begin to send mail ] [from:",sender,"] [to:",mailto,"] [subject:",subject,"]")
-
server =smtplib.SMTP(mail_server)
-
server.login(sender, password)
-
msg["Accept-Language"]="zh-CN"
-
msg["Accept-Charset"]="ISO-8859-1,gb2312"
-
server.sendmail(sender, mailto, msg.as_string())
-
server.quit()
-
print("[send mail ok! ] [from:",sender,"] [to:",mailto,"] [subject:",subject,"]")
-
return True
-
except Exception as e:
-
print("send_mail_with_file exception: ",str(e))
-
return False
-
-
def test_with_attach():
-
print("begin to send a mail with attach.")
-
today = time.strftime("%Y-%m-%d", time.localtime())
-
mailto = test_mailto
-
subject = "[attach][测试邮件,有乱码吗?][%s]"%(today)
-
body="%s: 测试邮件,有乱码吗?" % (today)
-
appendix="../hk/csv/origion_hk_factor_201601260100.csv"
-
send_mail_with_file(mailto, body, subject, appendix=[appendix],user_alias="python_email")
-
-
def test_no_attach():
-
print("begin to send a mail with attach.")
-
today = time.strftime("%Y-%m-%d", time.localtime())
-
mailto = test_mailto
-
subject = "[no attach][测试邮件,有乱码吗?][%s]"%(today)
-
body="%s: 测试邮件,有乱码吗?" % (today)
-
send_mail(mailto, body, subject)
-
-
if __name__ == '__main__':
-
open_log("./test.log")
-
print("begin to test Email.")
-
test_with_attach()
-
#test_no_attach()
2.6的print不好用,自己改。
阅读(1829) | 评论(0) | 转发(0) |