我使用的是python2.7.3。我有以下代码发送电子邮件与附件。在
# coding: cp1251
import os
import smtplib
from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.Utils import formatdate
def sendEmail(to_address, mail_settings, attachment_file_path, subject = 'my_subject'):
HOST = mail_settings['host']
port = mail_settings['port']
username = mail_settings['login']
password = mail_settings['pass']
msg = MIMEMultipart()
msg["From"] = mail_settings['from_address']
msg["To"] = ','.join(to_address)
msg["Subject"] = subject.decode('cp1251')
msg['Date'] = formatdate(localtime=True)
msg['Content-Type'] = "text/html; charset=cp1251"
# attach a file
part = MIMEBase('application', "octet-stream")
part.set_payload( open(attachment_file_path,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attachment_file_path))
msg.attach(part)
server = smtplib.SMTP(host=HOST, port=port)
try:
failed = server.sendmail(mail_settings['from_address'], to_address, msg.as_string())
print('sent')
server.close()
except Exception, e:
errorMsg = "Unable to send email. Error: %s" % str(e)
print(errorMsg)
我的问题是,通过这个代码接收电子邮件的exchange用户看不到附件文件名,如果它有俄文字母(例如Пример.txt),否则如果它有英文字母,那么对他们来说一切正常。 我只在使用exchange(gmail很好)的客户面前遇到这个问题。 我做错什么了?我应该在哪里更改编码?在
我终于找到了解决办法。我只是设置了标题的编码。在
相关问题 更多 >
编程相关推荐