Sending Email with Python

Sending emails with tools such as Thunderbird or Outlook is easy. You can integrate services with many providers such as Gmail, Hotmail, Yahoo! Mail, etc. Unfortunately, Python doesn't have a beautiful Graphical User Interface (GUI). Although, it has a complet set of tools which allows you to send emails quickly.

You built a command line software or you simply need to receive an email with results when your script is done, this tutorial will help you master the concept of sending emails with Python.

The basics

The first thing you need is to connect to the SMTP server you want to use to send your email. It requires you to have a valid username and password to that server (i.e. you need a Google Account to use Gmail).

To connect to the server, you need to find the server's domain name. A quick trick normally you only need to add stmp. in front of the email service provider. For Google's Gmail it's stmp.gmail.com.

ProviderSMTP server domain name
Gmailsmtp.gmail.com
Outlook/Hotmailsmtp-mail.outlook.com
Yahoo! Mailsmtp.mail.yahoo.com
Virginsmtp.virgin.net
Comcastsmtp.comcast.net
Verizonsmtp.verizon.net
Bell Aliantsmtpa.bellaliant.net

Once you have the domain name and the outgoing port associated with that domain you are ready to follow the following steps.

Using the port 465 (SSL)

The following code will create a SSL connection to the smtp server.

import smtplib
smtp_obj = smtplib.SMTP('smtp.provider.com', 465)
smtp_obj.ehlo()

Let's import the build-in Python library that will help us manage all the smtp protocol for us. Then, we specify the domain and the port we want to use. The last step is to send the message Hello to the server. This step is very important so make sure you don't forget it

Using the port 587 (TLS)

The following code will create an TLS connection to the smtp server.

import smtplib
smtp_obj = smtplib.SMTP_SSL('smtp.provider.com', 587)
smtp_obj.ehlo()
smtp_obj.starttls()

Let's import the build-in Python library that will help us manage all the smtp protocol for us. Then, we specify the domain and the port we want to use. The last step is to send the message Hello to the server. This step is very important so make sure you don't forget it. The last line is to start a TLS Encryption.

Log in to your account

We are almost ready to send the email. We simply need to authenticate our account with the smtp server.

smtp_obj.login('my.sexy.email.address@provider.com', 'CrazyLong_SECRET_P@ssw0rd')

Sending the email

Once again, let's use a simple function sendmail()to prepare and send the email.

smtp_obj.sendmail('my.sexy.email.address@provider.com', 'the.recipient@some.provider.net', 'Subject: Hello World\nDear recipient, I went arround the world to listen to Daft Punk latest mix. Sincerely, House-Addict')

This function require 3 arguments

  1. Your email address as a string
  2. The recipient's email address as a string or a list of strings (for multiple recipients).
  3. The email body as a string

To insert a subject, you need to start the email body with 'Subject:' and to end the subject simply add '\n'. After the end line character you can continue the rest of your email.

And the last step you should always do, is to disconnect from the smtp server.

smtp_obj.quit()