IoT Bytes

Bits and Bytes of IoT

Send Email from Raspberry Pi using Python Script and Gmail SMTP

Pradeep Singh | 21st Feb 2017

email

For your IoT Projects, you may need to send automated notifications or some information over the email. For this, you can use Gmail service with a very simple Python script and automate your email tasks easily.

Before you begin with this article, make sure you have a working Gmail account. To be on a safer side I would recommend creating a new email account for your automated email tasks (to avoid any security concerns with your primary email account).

1. Allowing App Access to your Gmail Account (SMTP Server):

By default, Gmail will not allow your apps to send emails using your account details. However, if you know want you are doing and want to permit your app to access Gmail’s SMTP server with your ID; you can configure it in Security settings (How?  – We will discuss it shortly).

Generally, Gmail accounts are configured with two different authentication methods –

  1. Standard Authentication (traditional Username / Password based authentication)
  2. Two Step Verification (Username / Password and Auto-generated Auth/Verification Code)

Based on the authentication method used for your Gmail account; use one of the following methods to enable SMTP server access to your account –

1.1 Allowing Gmail SMTP Access for Accounts with Standard Authentication:

To allow access to Gmail’s SMTP server from your app you can follow these steps –

  1. Login to your Gmail account using your username and password.
  2. From the top right corner go to “My Account
  3.  Under “Sign-in & security” section locate “Connected apps & sites” and click on it.
  4. Locate “Allow less secure apps” setting and turn it “On

Once “Allow less secure apps” setting is turned on, you can access Gmail SMTP server with your Gmail account username and password (Later in this article we will use it in Python script to send email).

1.2 Allowing Gmail SMTP Access for the Accounts with 2-Step Authentication

  1. Login to your Gmail account using your username and password.
  2. From the top right corner go to “My Account
  3.  Under “Sign-in & security” section locate “Signing in to Google” and click on it.
  4. Locate “App Passwords” and click on it.
  5. Gmail will ask your account password for authentication. Enter the password and click on “Sign in“. After sign in, you will be presented with options to create App Password.
  6. From the “Select App” drop down select “Mail” and from “Select Device” drop down select “Other“.
  7. On selecting “Other” from”Select Device” drop down, Gmail will ask you to enter Device name; type “Python_Script” in the text box. After this Click on “Generate” button to get the Password.
  8. Save this password for later use. (You will use this password along with your mail id (username) in Python script for account authentication. For your mail access from the computer or mobile, you will continue using the old password)

2. The Code (Python Script):

You can download the code from GitHub Repository Octocat. Make sure you download all the files are in the same directory on Raspberry Pi.

1. settings.ini : This file will hold all the settings related to email account and SMTP server. Before executing the code make sure you fill the values in this file. Use any plain text editior to edit this file.

2. email_handler.py : This file will handle all the complexities of forming the email message and sending it out using the settings defined in “settings.ini” file. No need to make any change in this file.

3. demo.py : This is a demo file and explains how to leverage code written in “email_handler.py” file to send email with minimum lines of code. Set “To_Email_ID” filed with the email address, where you want to send the test email and execute this file.

To send Plain Text email all you need to do is – import Class_eMail and use send_Text_Mail methods with variables for To Mail Address, Subject and Mail Body. Following is an example to send plain text email –

# import Class_eMail from email_handler.py file
from email_handler import Class_eMail

var_To_Email_ID = "your_mail_id@gmail.com"
var_SUBJECT = "Test Email"
var_EMAIL_BODY = "This is test email."

# create class object
email = Class_eMail()

# send email
email.send_Text_Mail(var_To_Email_ID, var_SUBJECT, var_EMAIL_BODY)

# delete class object
del email

Similarly, to send HTML based email you can use “send_HTML_Mail” class method as shown below –

email.send_HTML_Mail(var_To_Email_ID, var_SUBJECT, '<html><h1>This is sample HTML test email body</h1></html>')

Conclusion:

With this script, you can automate your email tasks easily without worrying about low-level implementation. This code will work with most SMTP servers (with basic authentication) and on any system that can run Python 2.7. In case you have any questions, please feel free to post them here.

6 thoughts on “Send Email from Raspberry Pi using Python Script and Gmail SMTP

  1. have an error when trying to run. file “/home/pi/demo.py”, line 19 in email = Class_eMail. I am running python 2.7.9 any help would be great. Thanks Brian

    Like

Leave a comment