Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从发送电子邮件的网站附加pdf文件_Python_Email_Pdf_Smtp_Messages - Fatal编程技术网

Python 从发送电子邮件的网站附加pdf文件

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')

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'))
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)