Python 从发送电子邮件的网站附加pdf文件
Hi能够使用以下代码发送电子邮件,我正在使用SMTP:Python 从发送电子邮件的网站附加pdf文件,python,email,pdf,smtp,messages,Python,Email,Pdf,Smtp,Messages,Hi能够使用以下代码发送电子邮件,我正在使用SMTP: message = MIMEMultipart('alternative') message['From'] = 'test@gmail.com' message['To'] = 'test@gmail.com' email_body = '<html><body>Hello!</body></html>' message.attach(MIMEText(email_body, 'html')
message = MIMEMultipart('alternative')
message['From'] = 'test@gmail.com'
message['To'] = 'test@gmail.com'
email_body = '<html><body>Hello!</body></html>'
message.attach(MIMEText(email_body, 'html'))
text = message.as_string()
当我从web添加附件时,如以下url:
我添加了以下代码:
from email.MIMEBase import MIMEBase
from email import encoders
pdf = 'http://www.pdf995.com/samples/pdf.pdf'
filename = 'test_pdf'
import requests
webf = requests.get(pdf)
attachment = webf.content
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment)
encoders.encode_base64(part)
part.add_header('Content-Disposition',
"attachment", filename=filename)
它发送但不附加pdf
我尝试过在线搜索,但似乎找不到解决方案。您使用的是哪一版本的Python?@cardamom its 2.7尝试将“octet stream”更改为“pdf”,如果不起作用,请使用MIMEText尝试此解决方案
from email.MIMEBase import MIMEBase
from email import encoders
pdf = 'http://www.pdf995.com/samples/pdf.pdf'
filename = 'test_pdf'
import requests
webf = requests.get(pdf)
attachment = webf.content
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment)
encoders.encode_base64(part)
part.add_header('Content-Disposition',
"attachment", filename=filename)