Lesson 46:

Sending Emails

Python can also be used send emails. This can be used for a variety of purposes, like updating a user at the end of a script completion, logging results, processing data, batch emailing, etc.

The standard protocol for sending email is 'Simple Mail Transfer Protocol' (SMTP), and one Python module designed to interact with is smtplib.


In [2]:
import smtplib

The first step in setting up email is creating a connection object to interact with an email/SMTP server.

This function gets a domain name and a port number. The port number for SMTP is typically 587; it is part of the internet address you are sending data to. The domain name is the address at which an SMTP server is hosted (typically the domain name with 'smtp' appended to it). Here are some additional examples.


In [17]:
conn = smtplib.SMTP('smtp.gmail.com', 587)

We will now use the .ehlo() method to test and connect.


In [18]:
conn.ehlo()


Out[18]:
(250,
 b'smtp.gmail.com at your service, [216.46.12.2]\nSIZE 35882577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8')

The returned variable is the response code (anything starting with a 2 is good), followed by a byte sting.

Since we have a positive response, we can now start an encrypted TLS connection (which is the newer version of SSL technology, both of which are based on public cryptography). Most modern email services will require TLS to begin interaction.


In [19]:
conn.starttls()


Out[19]:
(220, b'2.0.0 Ready to start TLS')

With the positive response, we can now pass in our login paramaters via the .login() method.

With Google specifically, there is an option to create app specific password, to avoid using your primary login for every device. This protects you from changing your password for every device in case of an error, which may be useful.


In [20]:
# Real values were used in testing, and removed for Github
# Due to the nature of Gmail's security, you may have to allow access from 'less secure apps' (like this script)
# The setting can be changed here: https://www.google.com/settings/u/2/security/lesssecureapps

conn.login('youremail@gmail.com','yourpassword')


Out[20]:
(235, b'2.7.0 Accepted')

Once we get a good response, we can now send emails via the .sendmail() method, which takes a variety of parameters, but requires a 'from' email, a 'to' email, and some content.

Most modern email services include protections to stop users from spamming other users, so they cap the number of emails that can be sent per day (Gmail is roughly 150 emails).


In [21]:
# For this exercise, we will just send an email to and from the same address; be sure to update these values with your
# own email addresses 

from_email = 'youremail@gmail.com'
to_email = 'theiremail@gmail.com'

conn.sendmail(from_email, to_email, 'Subject: Python Test Email \n\n Look,\n script text!')


Out[21]:
{}

A blank response indicates a successful email has been sent:

We can close our connection via the .quit() method:


In [22]:
conn.quit()


Out[22]:
(221, b'2.0.0 closing connection o1sm3872631qte.36 - gsmtp')

Recap

  • The smptlib module allows use to send Python via SMTP.
  • The first step is creating a connection object with the domain and port number for the email service.
  • The next step is passing the .ehlo() method to the connection object to ensure that we can reach it.
  • We then use .starttls() to start an encrypted interaction with the email server.
  • After a secure connection is started, we use .login() method with string parameters to log into the service.
  • With a positive response we can now send emails with the .sendemail() method.
  • Once you have completed all the necessary tasks, we can quit the session with the .quit() method.