Send Emails with Python smtplib

Julian, Thu 26 January 2017, Tools

automation, code, email, pybites, python, tips, tricks

I was recently given a Raspberry Pi as a gift and figured it'd make a cool host for cron jobs. I've always wanted to set up an email notification service for myself so I started playing around with smtplib in Python.

Set up an Application Password with Gmail

It was surprisingly simple to send a basic email using my Gmail account. The complexity came from Google's 2-step Verification security settings. If using Gmail, you'll need to set up an Application Password for the machine you're running your Py script from.

Once generated, the App Password will be your Gmail Password for this script.

Sending a Basic Email

The first thing you should try is sending the simplest of emails. Just plain text.

There are a few steps, which I'll detail one by one:

  1. Import the smtplib module.

  2. Using smtplib, specify the SMTP server and port you'll be accessing. In this example I'm using Gmail's servers.

    import smtplib
    smtp_server = smtplib.SMTP('', 587)
  3. The SMTP server you're connecting to requires a sort of 'handshake' for the service to work properly. This is done using the .ehlo() function of smtplib.

  4. As Google doesn't use SSL, we need to kick off TLS Encryption manually.

  5. Now for the login. Keep in mind at this point, you'll use the App Password you obtained earlier instead of your usual Gmail password.

    smtp_server.login('[email protected]', '<App Password>')
  6. Next we send the actual email message. The first email address is the address you're emailing from, the second is the recipient.

    smtp_server.sendmail('[email protected]', '[email protected]', 'Subject: Happy Australia Day!\nHi Everyone! Happy Australia Day! Cheers, Julian')

    Things to note in the above. The \n is mandatory. It's what separates your Subject line from the body of the email. Note: if you're running this in IDLE, when the email is sent successfully, you'll see '{}' characters appear as the return message. If part of a script, you can always add a print statement or other to show this instead.

  7. Finally, disconnect from the SMTP server when complete.


And we're done! Here's the/my final code:

import smtplib

smtp_server = smtplib.SMTP('', 587)
smtp_server.login('[email protected]', '<App Password>')

smtp_server.sendmail('[email protected]', '[email protected]', 'Subject: Happy Australia Day!\nHi Everyone! Happy Australia Day! Cheers, Julian')

print('Email sent successfully')

Next Steps

Clearly this is as basic as it gets. Moving forward you'll want the ability to send more detailed emails with some essence of formatting.

To do this you'll need to import the MIME (Multipurpose Internet Mail Extensions) modules into your script.

I want to expand on this current email example and will do so my next post. It also allows us to make the code a little more Pythonic!

Keep Calm and Code in Python!

-- Julian

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book