【发布时间】:2015-05-03 11:53:12
【问题描述】:
谁能指出下面的代码有什么问题?
我正在尝试使用 Python 发送多部分电子邮件。我可以显示电子邮件正文,但 PDF 显示为空白。
我可以收到一封只有正文的电子邮件,或者只有一个 PDF 的电子邮件,但如果放在一起,它就行不通了。
s = smtplib.SMTP('smtp.gmail.com',587)
s.starttls()
s.ehlo
try:
s.login(gmail, password)
except:
print 'SMTPAuthenticationError'
fp = file(attachment_path)
pdfAttachment = MIMEApplication(fp.read(), _subtype = "pdf", _encoder=encoders.encode_base64)
pdfAttachment.add_header('content-disposition', 'attachment', filename = ('utf-8', '', basename(attachment_path)))
text = MIMEMultipart('alternative')
t = open(email_body_path).read()
text.attach(MIMEText(t, "plain", _charset="utf-8"))
message = MIMEMultipart('mixed')
message.attach(text)
message.attach(pdfAttachment)
message['Subject'] = 'Test multipart message'
s.sendmail(gmail, 'me@gmail.com', message.as_string())
s.close()
【问题讨论】: