How to send email with pdf attachment in Python? [duplicate]

You create a message with an email package in this case – from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.MIMEImage import MIMEImage msg = MIMEMultipart() msg.attach(MIMEText(open(“/home/myuser/sample.pdf”).read())) and then send the message. import smtplib mailer = smtplib.SMTP() mailer.connect() mailer.sendmail(from_, to, msg.as_string()) mailer.close() Several examples here – http://docs.python.org/library/email-examples.html UPDATE Updating the link since the above … Read more

Failing to send email with the Python example

The following code works for me: import smtplib FROMADDR = “[email protected]” LOGIN = FROMADDR PASSWORD = “my.real.password” TOADDRS = [“[email protected]”] SUBJECT = “Test” msg = (“From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n” % (FROMADDR, “, “.join(TOADDRS), SUBJECT) ) msg += “some text\r\n” server = smtplib.SMTP(‘smtp.gmail.com’, 587) server.set_debuglevel(1) server.ehlo() server.starttls() server.login(LOGIN, PASSWORD) server.sendmail(FROMADDR, TOADDRS, msg) server.quit() I’m using Python … Read more

How to send an email using python after Google’s policy update on not allowing just username and password?

Here is a more precise answer with all the main steps. I hope it will help other people. Log in into your email account: https://myaccount.google.com Then go to the security part Be sure that you have turn on two steps verification and click on “App password” After select email and the corresponding device It will … Read more

Python smtplib proxy support

Use SocksiPy: import smtplib import socks #’proxy_port’ should be an integer #’PROXY_TYPE_SOCKS4′ can be replaced to HTTP or PROXY_TYPE_SOCKS5 socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, proxy_host, proxy_port) socks.wrapmodule(smtplib) smtp = smtplib.SMTP() …

smtplib sends blank message if the message contain certain characters

The problem is that smtplib is not putting a blank line between the message header and the message body as shown by in the “Show Original” form of my test: Return-Path: <[email protected]> Received: **REDACTED** Fri, 03 Aug 2012 06:56:20 -0700 (PDT) Message-ID: <[email protected]> Date: Fri, 03 Aug 2012 06:56:20 -0700 (PDT) From: [email protected] http: //www.example.com … Read more

The smtplib.server.sendmail function in python raises UnicodeEncodeError: ‘ascii’ codec can’t encode character

smtplib.server‘s sendmail method expects a bytes instance; if it gets a str it tries to encode it to ASCII, resulting in a UnicodeEncodeError if the str contains any non-ASCII characters. You can workaround this by encoding the message yourself: >>> msg = ‘Hello Wørld’ >>> from_ = ‘[email protected]’ >>> to_ = ‘[email protected]’ >>> subject=”Hello” >>> … Read more

How to send an email with Python?

I recommend that you use the standard packages email and smtplib together to send email. Please look at the following example (reproduced from the Python documentation). Notice that if you follow this approach, the “simple” task is indeed simple, and the more complex tasks (like attaching binary objects or sending plain/HTML multipart messages) are accomplished … Read more