send email in python

How to Send Email Using Python

Very often you may need to send emails to your users in your python application. Luckily there are numerous built-in libraries to do this. In this article, we will look at how to send email using python.


How to Send Email Using Python

Here are the steps to send email using Python. We will be using smtplib to send emails. We will look at different use cases for sending emails.


1. Send plain text email

Create a blank python file.

$ sudo vi send_email.py

Add the following lines to import smtplib, and also set certain parameters required for sending emails. Let us say you want to send email from company@website.com to user@example.com

#!/usr/bin/python

import smtplib

sender = 'company@website.com'
receivers = ['user@example.com']

message = """From: From Person <company@website.com>
To: To Person <user@example.com>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

Let us look at the above code. First we set the execution environment for python. Then we import smtplib. Then we set sender, receivers and message variables. At the very least, you need to specify the send email address, receiver email address and email message for sending email. Inside the message, you need to specify to email, from email, subject and email body.

Next, add the following code that does the actual sending of email.

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

In the above code we create an SMTP server on localhost and use sendmail function to email, In that function we send 3 parameters – sender, receiver and message.

Here is the complete code.

#!/usr/bin/python

import smtplib

sender = 'company@website.com'
receivers = ['user@example.com']

message = """From: From Person <company@website.com>
To: To Person <user@example.com>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

If you want to send email via public email service like Gmail, modify the above code as shown below. Replace

smtpObj = smtplib.SMTP('localhost')

with the following. Replace email and password below with your GMail email address and password.

smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', 465)
smtpObj.login(email, password)


2. Send HTML email

If you want to send HTML email, just update the message variable used above as shown. Add MIME-Version and Content-Type to tell smtp server that you are sending an html message.

message = """From: From Person <company@website.com>
 To: To Person <user@example.com>
 MIME-Version: 1.0
 Content-type: text/html
 Subject: SMTP HTML e-mail test
 This is an HTML e-mail message
 <b>This is HTML message.</b>
 <h1>This is headline.</h1>
 """

Here is the full code.

#!/usr/bin/python
import smtplib

sender = 'company@website.com'
receivers = ['user@example.com']

message = """From: From Person <company@website.com>
 To: To Person <user@example.com>
 MIME-Version: 1.0
 Content-type: text/html
 Subject: SMTP HTML e-mail test
 This is an HTML e-mail message
 <b>This is HTML message.</b>
 <h1>This is headline.</h1>
 """

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

In this article, we have looked at various use cases to send emails in python – plain text and HTML emails.

Also read:

How to Restrict SSH Access to Specific IP Addresses
How to Find Users Logged in Linux
How to Check Uptime in Linux
How to Resize Partition in Ubuntu
How to Update Key in Python Dictionary

Leave a Reply

Your email address will not be published. Required fields are marked *