python中文附件发送
# -*- coding: utf-8 -*-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 = 'postmaster@test.org'
to_addr = 'test@test.org'
subject_header = 'Subject: 员工手册'
attachment = u'd:/员工手册.doc' #文件访问时候要求u,否则不能通过
body = '''欢迎加入******有限公司,请查阅附件'''
m = MIMEMultipart()
m["To"] = "wangzhenyu@spolo.org"
m["From"] = "postmaster@spolo.org"
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'))
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='员工手册.doc')
m.attach(msg)
smtp = smtplib.SMTP()
smtp.connect('mail.test.org:25')
smtp.login('postmaster@test.org', "password")
smtp.sendmail('postmaster@stest.org', 'test@test.org', m.as_string())
smtp.quit()
print '邮件发送成功'
页:
[1]