mail.py.txt 这个可以发送带附件,带发送人姓名的邮件,邮件头,邮件正文都可以正常显示中文,但附件中的中文还不知道怎么让它正常显示,如果哪位高人知道怎么可以让附件中的中文内容正常显示,麻烦给我指点一下,学习学习。
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import email
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.header import Header
from email import encoders
import smtplib
import mimetypes
##邮件头信息
from_addr = Header('小马哥', 'utf-8')
from_addr.append('
', 'ascii')
to_addr = 'XXX@gmail.com'
subject_header = '主题: Sending PDF Attachemt'
###要发送的附件
attachment = 'test.py.tar.gz'
###邮件正文
body = ''''This message sends a PDF 附件 created with Report
Lab'''
m = MIMEMultipart()
m["To"] = to_addr
m["From"] = from_addr
m["Subject"] = Header(subject_header, 'utf8')
ctype, encoding = mimetypes.guess_type(attachment)
print ctype, encoding
maintype, subtype = ctype.split('/', 1)
print maintype, subtype
##### 构建附件####
m.attach(MIMEText(body, 'plain', '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=attachment)
m.attach(msg)
s = smtplib.SMTP("smtp.gmail.com")
s.set_debuglevel(1)
s.sendmail(from_addr, to_addr, m.as_string())
s.quit()
阅读(1453) | 评论(0) | 转发(0) |