Receive our best articles in your mailbox!

How to send emails from python

In this article, we will learn how to send emails using python. You can use it for automatic mailing to real people, or to send yourself notifications.

We will use the python-emails library which is a lot easier to use than the low level API provided in the standard smtplib module. Here are the documentation and the code for this library.

Using the emails library

Install the library using pip. Open a terminal and type the following command:

pip install emails

The version installed on my PC is 0.5.15 and I use python 3.6. If the code examples do not work for you, check your version number.

Before sending emails, you must first connect to your mail provider. In this example I will connect to Yahoo!Mail. I you just want a way to send yourself notifications, my advice is to create an account on Yahoo! so that you can follow the tutorial easily.

Note: this code is also available in a Github Gist.

# Sending emails from python.
# Template provided by https://Scripting.Tips
# You may freely reuse, modify and share this piece of code

import emails

# Here goes the configuration of your email provider.
# Look online to find it.
# For instance for Yahoo!Mail:

SMTP_SERVER   = 'smtp.mail.yahoo.com'
SMTP_LOGIN    = 'linus.t@yahoo.com'
SMTP_PASSWORD = 'mysecretpassword'

# Here goes your email data
# Usually, SENDER_EMAIL must match SMTP_LOGIN

SENDER_NAME  = 'Linus T.'
SENDER_EMAIL = 'linus.t@yahoo.com'
RECIPIENT = 'james@gmail.com'
SUBJECT   = 'Learn how to automate emails!'
CONTENT   = """
Dear James, <br>
I found out about an awesome website
to learn scripting and though you'd be
interesed. Check it out: 
#Note: you can use HTML to format the content

message = emails.html(
    mail_from=(SENDER_NAME, SENDER_EMAIl)

# If you want to attach an image of a file, use this code:
# message.attach(
#     data=open('screenshot.jpg', 'rb'), 
#     filename='screenshot.jpg'
# )

config = {
    'host': SMTP_SERVER,
    'timeout': 5,
    'ssl': True,
    'user': SMTP_LOGIN,
    'password': SMTP_PASSWORD

r = message.send(to=RECIPIENT, smtp=config)

if r.success:
    print('Mail sent!')
    print('Something wrong happened')
    print('Here is the smtp status code', r.status_code)

Pretty easy, right? The library takes care of all the plumbing necessary to create requests to the SMTP server and format the mail using the proper protocol.

I added a piece of commented code in case you'd like to send a file as attachment. This is very handy if you want to send automatic screenshots of your screen, or if your script generated a file that you want to retrieve.

Find your SMTP server config

Each email provider uses something called an SMTP server to send emails. Depending on the provider you want to use, we will have to configure a different server.

You can usually find the config information pretty easily using a google search. For instance, search for "SMTP Outlook" if you want to use Outlook to send emails.

Here is the SMTP documentation for Yahoo!Mail.

Allow access for your app

Most email providers will prevent applications from using their servers by default. If you want to allow an application (such as our python script) to use your account, you will have to configure an access for it.

If you use Yahoo!Mail, the procedure to follow is explained on Yahoo!Help. Here are links to detailed instructions:

Which I copy here for your convenience. If anything seems wrong, check out the links instead:

  1. login to your Yahoo!Mail account and go to the account info page. You can find the link in the upper right-hand corner, under the user icon;
  2. click account security;
  3. enable two-steps verification;
  4. enter your mobile number when prompted;
  5. click create app passwords;
  6. choose "other app";
  7. copy the password provided and use this in 'SMTP_PASSWORD'.

And that's it!

Bonus: how to take a screenshot

You can take a screenshot using a number of python libraries. For instance, with the library PyAutoGUI:

import pyautogui

Here is the link to the relevant doc.

Combine this with the mailing script above to send automated screenshots :-)