Chinaunix首页 | 论坛 | 博客
  • 博客访问: 440975
  • 博文数量: 126
  • 博客积分: 35
  • 博客等级: 民兵
  • 技术积分: 1262
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-19 16:39
文章分类

全部博文(126)

文章存档

2017年(2)

2016年(20)

2015年(64)

2014年(24)

2013年(16)

我的朋友

分类: Python/Ruby

2016-01-26 15:56:50

闪电邮打开正文,中文显示正常。
微信的腾讯企业邮打开正文,中文是乱码。

正文用:
MIMEText(body, 'plain', 'utf-8')

附件用:
        data = open(file_name, 'rb')
        part.set_payload(data.read().decode('utf-8').encode('gbk'))


  1. #!/usr/bin/env python2.6
  2. #coding: utf-8
  3. # Filename: Email.py
  4. # FileDesc: 发送邮件功能

  5. import smtplib
  6. from email.mime.text import MIMEText
  7. from email.mime.multipart import MIMEMultipart
  8. from email.MIMEBase import MIMEBase
  9. from email import Encoders
  10. import time
  11. import datetime

  12. test_mailto=["cat@qq.com","lin@qq.com"]

  13. def send_mail(mailto, body, subject, mail_user="linkwin",
  14.     mail_pwd="Linkwinisbest1", user_alias="linkwin",
  15.     mail_server="smtp.exmail.qq.com", mail_postfix="richba.com", format='plain'):
  16.     sender = mail_user+"@"+mail_postfix
  17.     send_mail_with_file(mailto,body,subject,format,sender,mail_user,mail_pwd, user_alias,mail_server)

  18. def send_mail_with_file( mailto, body, subject, format='plain',
  19.     sender="linkwin@richba.com", user="linkwin",
  20.     password="Linkwinisbest1", user_alias="linkwin",
  21.     mail_server="smtp.exmail.qq.com", appendix=[] ):
  22.     msg = MIMEMultipart()
  23.     msg["to"] = ";".join(mailto)
  24.     msg["from"] = user_alias+"<"+sender+">"
  25.     msg["subject"] = subject

  26.     body_msg = MIMEText(body, format, 'utf-8')
  27.     msg.attach(body_msg)

  28.     ctype='application/octet-stream'
  29.     maintype,subtype = ctype.split('/',1)
  30.     for file_name in appendix:
  31.         part = MIMEBase(maintype, subtype)
  32.         data = open(file_name, 'rb')
  33.         part.set_payload(data.read().decode('utf-8').encode('gbk'))
  34.         data.close()
  35.         Encoders.encode_base64(part)
  36.         part["Content-Type"] = ctype
  37.         desc = "attachment; filename=\"%s\"" % file_name.split("/")[-1]
  38.         part["Content-Disposition"] = desc
  39.         msg.attach(part)
  40.     try:
  41.         print("[begin to send mail ] [from:",sender,"] [to:",mailto,"] [subject:",subject,"]")
  42.         server =smtplib.SMTP(mail_server)
  43.         server.login(sender, password)
  44.         msg["Accept-Language"]="zh-CN"
  45.         msg["Accept-Charset"]="ISO-8859-1,gb2312"
  46.         server.sendmail(sender, mailto, msg.as_string())
  47.         server.quit()
  48.         print("[send mail ok! ] [from:",sender,"] [to:",mailto,"] [subject:",subject,"]")
  49.         return True
  50.     except Exception as e:
  51.         print("send_mail_with_file exception: ",str(e))
  52.         return False

  53. def test_with_attach():
  54.     print("begin to send a mail with attach.")
  55.     today = time.strftime("%Y-%m-%d", time.localtime())
  56.     mailto = test_mailto
  57.     subject = "[attach][测试邮件,有乱码吗?][%s]"%(today)
  58.     body="%s: 测试邮件,有乱码吗?" % (today)
  59.     appendix="../hk/csv/origion_hk_factor_201601260100.csv"
  60.     send_mail_with_file(mailto, body, subject, appendix=[appendix],user_alias="python_email")

  61. def test_no_attach():
  62.     print("begin to send a mail with attach.")
  63.     today = time.strftime("%Y-%m-%d", time.localtime())
  64.     mailto = test_mailto
  65.     subject = "[no attach][测试邮件,有乱码吗?][%s]"%(today)
  66.     body="%s: 测试邮件,有乱码吗?" % (today)
  67.     send_mail(mailto, body, subject)

  68. if __name__ == '__main__':
  69.     open_log("./test.log")
  70.     print("begin to test Email.")
  71.     test_with_attach()
  72.     #test_no_attach()
2.6的print不好用,自己改。

阅读(1873) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~