有人能指出以下代码有什么问题吗?
我正在尝试使用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()