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.
1 |
import smtplib |
2 |
|
3 |
server = smtplib.SMTP(host="host_address" ,port="your_port") |
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.
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.
1 |
# import necessary packages
|
2 |
|
3 |
from email.mime.multipart import MIMEMultipart |
4 |
from email.mime.text import MIMEText |
5 |
import smtplib |
6 |
|
7 |
# create message object instance
|
8 |
msg = MIMEMultipart() |
9 |
|
10 |
|
11 |
message = "Thank you" |
12 |
|
13 |
# setup the parameters of the message
|
14 |
password = "your_password" |
15 |
msg['From'] = "your_address" |
16 |
msg['To'] = "to_address" |
17 |
msg['Subject'] = "Subscription" |
18 |
|
19 |
# add in the message body
|
20 |
msg.attach(MIMEText(message, 'plain')) |
21 |
|
22 |
#create server
|
23 |
server = smtplib.SMTP('smtp.gmail.com: 587') |
24 |
|
25 |
server.starttls() |
26 |
|
27 |
# Login Credentials for sending the mail
|
28 |
server.login(msg['From'], password) |
29 |
|
30 |
|
31 |
# send the message via the server.
|
32 |
server.sendmail(msg['From'], msg['To'], msg.as_string()) |
33 |
|
34 |
server.quit() |
35 |
|
36 |
print("successfully sent email to %s:" % (msg['To'])) |
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.
1 |
import smtplib |
2 |
from email.mime.multipart import MIMEMultipart |
3 |
from email.mime.image import MIMEImage |
4 |
from email.mime.text import MIMEText |
5 |
# create message object instance
|
6 |
msg = MIMEMultipart() |
7 |
|
8 |
|
9 |
# setup the parameters of the message
|
10 |
password = "your_password" |
11 |
msg['From'] = "your_address" |
12 |
msg['To'] = "to_address" |
13 |
msg['Subject'] = "Photos" |
14 |
|
15 |
|
16 |
file = "Python.pdf" |
17 |
# attach image to message body
|
18 |
|
19 |
msg.attach(MIMEText(open(file).read())) |
20 |
|
21 |
# create server
|
22 |
server = smtplib.SMTP('smtp.gmail.com: 587') |
23 |
|
24 |
server.starttls() |
25 |
|
26 |
# Login Credentials for sending the mail
|
27 |
server.login(msg['From'], password) |
28 |
|
29 |
|
30 |
# send the message via the server.
|
31 |
server.sendmail(msg['From'], msg['To'], msg.as_string()) |
32 |
|
33 |
server.quit() |
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.
1 |
<html>
|
2 |
<head>
|
3 |
|
4 |
<title>Tutsplus Email Newsletter</title> |
5 |
<style type="text/css"> |
6 |
a {color: #d80a3e;} |
7 |
body, #header h1, #header h2, p {margin: 0; padding: 0;} |
8 |
#main {border: 1px solid #cfcece;} |
9 |
img {display: block;} |
10 |
#top-message p, #bottom p {color: #3f4042; font-size: 12px; font-family: Arial, Helvetica, sans-serif; } |
11 |
#header h1 {color: #ffffff !important; font-family: "Lucida Grande", sans-serif; font-size: 24px; margin-bottom: 0!important; padding-bottom: 0; } |
12 |
#header p {color: #ffffff !important; font-family: "Lucida Grande", "Lucida Sans", "Lucida Sans Unicode", sans-serif; font-size: 12px; } |
13 |
h5 {margin: 0 0 0.8em 0;} |
14 |
h5 {font-size: 18px; color: #444444 !important; font-family: Arial, Helvetica, sans-serif; } |
15 |
p {font-size: 12px; color: #444444 !important; font-family: "Lucida Grande", "Lucida Sans", "Lucida Sans Unicode", sans-serif; line-height: 1.5;} |
16 |
</style>
|
17 |
</head>
|
18 |
<body>
|
19 |
<table width="100%" cellpadding="0" cellspacing="0" bgcolor="e4e4e4"><tr><td> |
20 |
<table id="top-message" cellpadding="20" cellspacing="0" width="600" align="center"> |
21 |
<tr>
|
22 |
<td align="center"> |
23 |
<p><a href="#">View in Browser</a></p> |
24 |
</td>
|
25 |
</tr>
|
26 |
</table>
|
27 |
|
28 |
<table id="main" width="600" align="center" cellpadding="0" cellspacing="15" bgcolor="ffffff"> |
29 |
<tr>
|
30 |
<td>
|
31 |
<table id="header" cellpadding="10" cellspacing="0" align="center" bgcolor="8fb3e9"> |
32 |
<tr>
|
33 |
<td width="570" align="center" bgcolor="#d80a3e"><h1>Evanto Limited</h1></td> |
34 |
</tr>
|
35 |
<tr>
|
36 |
<td width="570" align="right" bgcolor="#d80a3e"><p>November 2017</p></td> |
37 |
</tr>
|
38 |
</table>
|
39 |
</td>
|
40 |
</tr>
|
41 |
|
42 |
<tr>
|
43 |
<td>
|
44 |
<table id="content-3" cellpadding="0" cellspacing="0" align="center"> |
45 |
<tr>
|
46 |
<td width="250" valign="top" bgcolor="d0d0d0" style="padding:5px;"> |
47 |
<img src="https://thumbsplus.tutsplus.com/uploads/users/30/posts/29520/preview_image/pre.png" width="250" height="150" /> |
48 |
</td>
|
49 |
<td width="15"></td> |
50 |
<td width="250" valign="top" bgcolor="d0d0d0" style="padding:5px;"> |
51 |
<img src="https://cms-assets.tutsplus.com/uploads/users/30/posts/29642/preview_image/vue-2.png" width ="250" height="150" /> |
52 |
</td>
|
53 |
</tr>
|
54 |
</table>
|
55 |
</td>
|
56 |
</tr>
|
57 |
<tr>
|
58 |
<td>
|
59 |
<table id="content-4" cellpadding="0" cellspacing="0" align="center"> |
60 |
<tr>
|
61 |
<td width="200" valign="top"> |
62 |
<h5>How to Get Up and Running With Vue</h5> |
63 |
<p>In the introductory post for this series we spoke a little about how web designers can benefit by using Vue. In this tutorial we’ll learn how to get Vue up..</p> |
64 |
</td>
|
65 |
<td width="15"></td> |
66 |
<td width="200" valign="top"> |
67 |
<h5>Introducing Haiku: Design and Create Motion</h5> |
68 |
<p>With motion on the rise amongst web developers so too are the tools that help to streamline its creation. Haiku is a stand-alone..</p> |
69 |
</td>
|
70 |
</tr>
|
71 |
</table>
|
72 |
</td>
|
73 |
</tr>
|
74 |
</table>
|
75 |
<table id="bottom" cellpadding="20" cellspacing="0" width="600" align="center"> |
76 |
<tr>
|
77 |
<td align="center"> |
78 |
<p>Design better experiences for web & mobile</p> |
79 |
<p><a href="#">Unsubscribe</a> | <a href="#">Tweet</a> | <a href="#">View in Browser</a></p> |
80 |
</td>
|
81 |
</tr>
|
82 |
</table><!-- top message --> |
83 |
</td></tr></table><!-- wrapper --> |
84 |
|
85 |
</body>
|
86 |
</html>
|
The template will finally look like this when complete:
Below is the script for sending an email with HTML content. The content of the template will be our email message.1 |
import smtplib |
2 |
import email.message |
3 |
server = smtplib.SMTP('smtp.gmail.com:587') |
4 |
|
5 |
email_content = """ |
6 |
<html>
|
7 |
|
8 |
<head>
|
9 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
10 |
|
11 |
<title>Tutsplus Email Newsletter</title>
|
12 |
<style type="text/css">
|
13 |
a {color: #d80a3e;}
|
14 |
body, #header h1, #header h2, p {margin: 0; padding: 0;}
|
15 |
#main {border: 1px solid #cfcece;}
|
16 |
img {display: block;}
|
17 |
#top-message p, #bottom p {color: #3f4042; font-size: 12px; font-family: Arial, Helvetica, sans-serif; }
|
18 |
#header h1 {color: #ffffff !important; font-family: "Lucida Grande", sans-serif; font-size: 24px; margin-bottom: 0!important; padding-bottom: 0; }
|
19 |
#header p {color: #ffffff !important; font-family: "Lucida Grande", "Lucida Sans", "Lucida Sans Unicode", sans-serif; font-size: 12px; }
|
20 |
h5 {margin: 0 0 0.8em 0;}
|
21 |
h5 {font-size: 18px; color: #444444 !important; font-family: Arial, Helvetica, sans-serif; }
|
22 |
p {font-size: 12px; color: #444444 !important; font-family: "Lucida Grande", "Lucida Sans", "Lucida Sans Unicode", sans-serif; line-height: 1.5;}
|
23 |
</style>
|
24 |
</head>
|
25 |
|
26 |
<body>
|
27 |
|
28 |
|
29 |
<table width="100%" cellpadding="0" cellspacing="0" bgcolor="e4e4e4"><tr><td>
|
30 |
<table id="top-message" cellpadding="20" cellspacing="0" width="600" align="center">
|
31 |
<tr>
|
32 |
<td align="center">
|
33 |
<p><a href="#">View in Browser</a></p>
|
34 |
</td>
|
35 |
</tr>
|
36 |
</table>
|
37 |
|
38 |
<table id="main" width="600" align="center" cellpadding="0" cellspacing="15" bgcolor="ffffff">
|
39 |
<tr>
|
40 |
<td>
|
41 |
<table id="header" cellpadding="10" cellspacing="0" align="center" bgcolor="8fb3e9">
|
42 |
<tr>
|
43 |
<td width="570" align="center" bgcolor="#d80a3e"><h1>Evanto Limited</h1></td>
|
44 |
</tr>
|
45 |
<tr>
|
46 |
<td width="570" align="right" bgcolor="#d80a3e"><p>November 2017</p></td>
|
47 |
</tr>
|
48 |
</table>
|
49 |
</td>
|
50 |
</tr>
|
51 |
|
52 |
<tr>
|
53 |
<td>
|
54 |
<table id="content-3" cellpadding="0" cellspacing="0" align="center">
|
55 |
<tr>
|
56 |
<td width="250" valign="top" bgcolor="d0d0d0" style="padding:5px;">
|
57 |
<img src="https://thumbsplus.tutsplus.com/uploads/users/30/posts/29520/preview_image/pre.png" width="250" height="150" />
|
58 |
</td>
|
59 |
<td width="15"></td>
|
60 |
<td width="250" valign="top" bgcolor="d0d0d0" style="padding:5px;">
|
61 |
<img src="https://cms-assets.tutsplus.com/uploads/users/30/posts/29642/preview_image/vue-2.png" width ="250" height="150" />
|
62 |
</td>
|
63 |
</tr>
|
64 |
</table>
|
65 |
</td>
|
66 |
</tr>
|
67 |
<tr>
|
68 |
<td>
|
69 |
<table id="content-4" cellpadding="0" cellspacing="0" align="center">
|
70 |
<tr>
|
71 |
<td width="200" valign="top">
|
72 |
<h5>How to Get Up and Running With Vue</h5>
|
73 |
<p>In the introductory post for this series we spoke a little about how web designers can benefit by using Vue. In this tutorial we will learn how to get Vue up..</p>
|
74 |
</td>
|
75 |
<td width="15"></td>
|
76 |
<td width="200" valign="top">
|
77 |
<h5>Introducing Haiku: Design and Create Motion</h5>
|
78 |
<p>With motion on the rise amongst web developers so too are the tools that help to streamline its creation. Haiku is a stand-alone..</p>
|
79 |
</td>
|
80 |
</tr>
|
81 |
</table>
|
82 |
</td>
|
83 |
</tr>
|
84 |
|
85 |
|
86 |
</table>
|
87 |
<table id="bottom" cellpadding="20" cellspacing="0" width="600" align="center">
|
88 |
<tr>
|
89 |
<td align="center">
|
90 |
<p>Design better experiences for web & mobile</p>
|
91 |
<p><a href="#">Unsubscribe</a> | <a href="#">Tweet</a> | <a href="#">View in Browser</a></p>
|
92 |
</td>
|
93 |
</tr>
|
94 |
</table><!-- top message -->
|
95 |
</td></tr></table><!-- wrapper -->
|
96 |
|
97 |
</body>
|
98 |
</html>
|
99 |
|
100 |
|
101 |
"""
|
102 |
|
103 |
msg = email.message.Message() |
104 |
msg['Subject'] = 'Tutsplus Newsletter' |
105 |
|
106 |
|
107 |
msg['From'] = 'youraddress' |
108 |
msg['To'] = 'to_address' |
109 |
password = "yourpassword" |
110 |
msg.add_header('Content-Type', 'text/html') |
111 |
msg.set_payload(email_content) |
112 |
|
113 |
s = smtplib.SMTP('smtp.gmail.com: 587') |
114 |
s.starttls() |
115 |
|
116 |
# Login Credentials for sending the mail
|
117 |
s.login(msg['From'], password) |
118 |
|
119 |
s.sendmail(msg['From'], [msg['To']], msg.as_string()) |
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.
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.
1 |
python3.8 -m venv my_env
|
2 |
source my_env/bin/activate
|
Install Flask and Flask-Mail using pip
:
1 |
pip install Flask
|
2 |
pip install Flask-Mail
|
Also, create a file app.py. In app.py, let's start by defining our imports.
1 |
from flask import Flask |
2 |
from flask_mail import Mail |
Next, define a Flask app instance.
1 |
from flask import Flask |
2 |
from flask_mail import Mail |
3 |
app = Flask(__name__) |
Since we are using the SMTP protocol to send emails with Flask, let's add a few configurations as shown below.
1 |
from flask import Flask |
2 |
from flask_mail import Mail |
3 |
|
4 |
app = Flask(__name__) |
5 |
|
6 |
app.config['MAIL_SERVER']='smtp.gmail.com' |
7 |
app.config['MAIL_PORT'] = 465 |
8 |
app.config['MAIL_USE_TLS'] = False |
9 |
app.config['MAIL_USE_SSL'] = True |
10 |
app.config['MAIL_USERNAME'] = 'email@gmail.com' |
11 |
app.config['MAIL_PASSWORD'] = 'gmailpassword' |
Flask-Mail comes with the Mail
instance, which manages emails. Let's initialize the Mail
instance.
1 |
mail = Mail(app) |
Next, create a message instance and send the intended message.
1 |
msg = Message('Introducing Haiku', sender = 'haiku@mail.io', recipients = ['your_gmail']) |
2 |
msg.body = "Configuration Test message" |
3 |
mail.send(msg) |
4 |
|
5 |
|
6 |
|
7 |
if __name__ == '__main__': |
8 |
app.run(debug = True) |
The full code for app.py should look like this:
1 |
from flask import Flask |
2 |
from flask_mail import Mail,Message |
3 |
|
4 |
app = Flask(__name__) |
5 |
|
6 |
app.config['MAIL_SERVER']='smtp.gmail.com' |
7 |
app.config['MAIL_PORT'] = 465 |
8 |
app.config['MAIL_USE_TLS'] = False |
9 |
app.config['MAIL_USE_SSL'] = True |
10 |
app.config['MAIL_USERNAME'] = 'email@gmail.com' |
11 |
app.config['MAIL_PASSWORD'] = 'password' |
12 |
|
13 |
|
14 |
mail = Mail(app) |
15 |
msg = Message('Introducing Haiku', sender = 'haiku@mail.io', recipients = ['your_gmail']) |
16 |
msg.body = "Configuration Test message" |
17 |
mail.send(msg) |
18 |
|
19 |
|
20 |
if __name__ == '__main__': |
21 |
app.run(debug = True) |
Conclusion
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.