7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. Python

Sending Emails in Python With SMTP

Scroll to top
Read Time: 9 mins

This tutorial will give an introduction to SMTP, a Python module used for sending mail. It will also demonstrate how to send different email types like simple text emails, emails with attachments, and emails with HTML content.

Later on, I'll show you how to use Flask-Mail to send emails with Flask.

Introduction to SMTP

The Simple Mail Transfer Protocol (SMTP) handles sending and routing email between mail servers.

In Python, the smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

Here is how to create an SMTP object.

Create and Send a Simple Email

The following script will allow you to send an email via the Gmail SMTP server. However, Google will not allow logging in via smtplib because it has flagged this type of login as "less secure". To solve this, go to https://www.google.com/settings/security/lesssecureapps while you're logged in to your Google account, and "Allow less secure apps". See the screenshot below.

Allow less secure appsAllow less secure appsAllow less secure apps

We will follow these steps to accomplish this process:

  • Create an SMTP object for connection to the server.
  • Log in to your account.
  • Define your message headers and login credentials.
  • Create a MIMEMultipart message object and attach the relevant headers to it, i.e. From, To, and Subject.
  • Attach the message to the MIMEMultipart object.
  • Finally, send the message.

This process is as simple, as shown below.

Note that the ‘To' and ‘From’ addresses must be included in the message headers explicitly.

Create and Send an Email With an Attachment

In this example, we are going to send an email with an image attachment. The process is similar to sending a plain text email.

  • Create an SMTP object for connection to the server.
  • Log in to your account.
  • Define your message headers and login credentials.
  • Create a MIMEMultipart message object and attach the relevant headers to it, i.e. From, To, and Subject.
  • Read and attach the image to the message MIMEMultipart object.
  • Finally, send the message.

The MIMEImage class is a subclass of MIMENonMultipart which is used to create MIME message objects of image types. Other available classes include MIMEMessage and MIMEAudio.

Create and Send HTML Emails

The first thing we are going to do is create an HTML email template.

Create an HTML Template

Here is the HTML code for the template. It contains two table columns, each with an image and preview content. If you prefer a ready-made, professional solution, grab some of the best email templates. We have a number of responsive options with easy-to-customize features to get started with.

The template will finally look like this when complete:

email templateemail templateemail template
Below is the script for sending an email with HTML content. The content of the template will be our email message.

Execute your code, and if no error occurs, then the email was successful. Now go to your inbox and you should see your email as HTML content nicely formatted.

HTML formatted emailHTML formatted emailHTML formatted email

Send Emails With Flask

This section will cover configuring and sending emails with Flask-Mail using the SMTP protocol. Flask-Mail is an extension that provides a simple interface to set up SMTP with your Flask application.

Create a project directory called flask_email. Inside the project directory, create a virtual environment and activate it.

Install Flask and Flask-Mail using pip:

Also, create a file app.py. In app.py, let's start by defining our imports.

Next, define a Flask app instance.

Since we are using the SMTP protocol to send emails with Flask, let's add a few configurations as shown below. 

Flask-Mail comes with the Mail instance, which manages emails. Let's initialize the Mail instance.

Next, create a message instance and send the intended message.

The full code for app.py should look like this:


This tutorial has covered most of what is needed to send emails for your application. There are several APIs available for sending emails, e.g. SendGrid. So you don't have to start from scratch, but it's also important to understand the basics. For more information, visit the Python docs.

Additionally, don’t hesitate to see what we have available for sale and for study on Envato Market.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.