Send email with Python and smtplib

 
 

Send email with Python and smtplib package is just another quick tutorial from Tufora where we will show you in just a few steps how easy is to use Python for sending emails from your applications. This solution uses no more than five lines of code and can be easily implemented within your Python application anytime being defined as a Python module using import statement or simply directly to your main application code. The code below has been tested using Python 3.7 but it can be easily adapted and used on older or newer versions as long the smtplib module is present and doesn’t comes with major changes.

Table of contents

Import smtplib Python module
Server details and user credentials
Compose email message
Close SMTP server connection

Import smtplib Python module

Like any other Python module smtplib makes no exception and it needs to be installed first via pip or using any other Python package manager before importing it into our Python application. On our first step, after the module has been installed, we have to make use of import statement in order to use this module.

import smtplib

Server details and user credentials

Next we will define a variable named server that will make use of smtplib module and we will pass server details and also some existing credentials like smtp_username and smtp_password needed for SMTP authentication.

server = smtplib.SMTP_SSL('smtp.dummy-domain.com', 465)
server.login("smtp_username", "smtp_password")

Compose email message

On this step we have to specify the sender of the email message, the receiver of it and also message body.

server.sendmail(
  "from@dummy-domain.com",
  "to@dummy-domain.com",
  "This message has been sent using Python and smptlib")

Close SMTP connection

Once the email message has been sent out we would like to close the connection otherwise we will leave too many open connections to that specific SMTP server / service and we may hit a possible limit or even be rejected to open a new connection if the session timeout is set too high.

server.quit()

For a more advanced user case we can use the following code:

#!/usr/local/bin/python

# import necessary packages
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib, ssl
 
# create message object instance
msg = MIMEMultipart()
 
# setting up the message to be sent
message = "Finally it looks like this SMTP script is now working..."
 
# setup the parameters of the message
serveraddress = "smtp.dummy-domain.com"
serverport = "465"
password = "theLongSafePassword"
msg['From'] = "from@dummy-domain.com"
msg['To'] = "to@dummy-domain.com"
msg['Subject'] = "My test email message"
 
# add in the message body
msg.attach(MIMEText(message, 'plain'))
 
#create server
server = smtplib.SMTP_SSL(serveraddress, serverport)
 
# Login Credentials for sending the mail
server.login(msg['From'], password)
 
 
# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())
 
server.quit()
 
print("Successfully sent email to %s:" % (msg['To']))

Video

No video posted for this page.

Screenshots

No screenshots posted for this page.

Source code

No code posted for this page.

About this page

Article
Send email with Python and smtplib
Author
Category
Published
14/11/2018
Updated
22/07/2019
Tags

Share this page

If you found this page useful please share it with your friends or colleagues.