0

Hi was able to send an email using this code below, I am using 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()

Then send as:

smtpserver.sendmail(from_address, to_address, message_body)

when I add attachment from web like this url: http://www.pdf995.com/samples/pdf.pdf

I added this code:

    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)

It send but doesnt attach the pdf.

I tried searching online cant seem to find a solution for this.

Bry
  • 21
  • 1
  • 2
  • Which version of Python are you using? – cardamom Jun 09 '17 at 10:23
  • @cardamom its 2.7 – Bry Jun 09 '17 at 16:29
  • Try changing 'octet-stream' to 'pdf' as per [here](https://www.sitekickr.com/snippets/python/send-html-email-attachments) if that doesn't work try this solution with MIMEText [here](https://stackoverflow.com/questions/11921188/how-to-send-email-with-pdf-attachment-in-python) – cardamom Jun 10 '17 at 15:35

0 Answers0