使用Python发送电子邮件.pdf附件

时间:2018-10-22 22:09:19

标签: python-3.x pdf email-attachments

我正在使用Python脚本编写日志文件,并希望通过电子邮件将结果作为.pdf附件以及一些正文发送。我可以看到带有.pdf附件的电子邮件,但无法加载。像是解码问题还是其他问题?请问如何解决?这是我的代码:

msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Test Result"
body = "Dear Reader, \n\nHere is the test result! \n\nSincerely, 
\nController Test Team"
msg.attach(MIMEText(body, 'plain'))

filename = "Controller_Tests_logfile.pdf"
attachment = open("/home/smc/Controller_Tests_logfile.pdf", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % 
filename)
msg.attach(part)

0 个答案:

没有答案