How to Send SMTP Email using Raspberry Pi

How to send SMTP Email using Raspberry Pi

Raspberry Pi is a small sized computer mainly designed for education purpose. The company launched Raspberry pi-3 model on February 2016, with inbuilt WIFI, Bluetooth and USB boot capabilities. Due to its small size and affordable price, it is quickly adopted by makers and electronics enthusiasts for projects. The Raspberry Pi is slower than laptop or desktop but is still a complete Linux computer. Raspberry pi generally comes with installed Raspbian OS.

With the vision of IoT (internet of things) raspberry pi is the powerful tool. We have developed number of IoT projects using Raspberry Pi. It can also be used with many IoT cloud platform like IFTTT, ThingSpeak, Artik Cloud, Firebase and Particle. One of the application of raspberry-pi is to use SMTP (Simple mail transfer protocol) for sending and receiving emails.

In this project, we are going to send an email via SMTP server using raspberry pi. SMTP works by starting a session between the user and server, whereas MTA (Mail Transfer Agent) and MDA (Mail Delivery Agent) provide domain searching and local delivery services. Previously, we have send SMTP email using ESP8266 NodeMCU.


Simple Mail Transfer Protocol (SMTP)

SMTP is the standard protocol for providing email services on a TCP/IP network. This server provides the ability to receive and send email messages.

SMTP is an application-layer protocol that provides the delivery and transmission of email over the Internet. It is maintained by the Internet Engineering Task Force (IETF). SMTP is generally summed within an email client application and is composed of four key components:

  1. Local user or client-end utility known as the mail user agent (MUA)
  2. Server known as mail submission agent (MSA)
  3. Mail transfer agent (MTA)
  4. Mail delivery agent (MDA)

Getting email alerts or set of data using raspberry pi python program is very useful application. All we need is smtplib library in the python script. There are many version of python but pi is more compatible with 3.2 and 2.7 version of it. Below are the mentioned steps of sending SMTP email using pi:


Steps for Sending Email using Raspberry Pi

Step 1:- Setting up the raspberry pi module- connect the power cable and LAN cable to raspberry pi then create WIFI hotspot and connect with it.


Step 2:- After then open the terminal window on Pi. Then, open the putty software and paste the host name or ip address.

Username- pi
Password- raspberry


Step 3:- We need to update the Raspberry Pi. So, install the latest packages by using the below command.

sudo apt-get update


Updating Raspberry Pi for SMTP Server


Next we need to install python and SMTP service for our Raspberry Pi and this can be done using following commands-

1. Install Python 2.7.13 - sudo apt-get install python2

Installing Python on Pi for SMTP Server


2. Install an SMTP service - sudo apt-get install ssmtp

Installing SMTP Service on Pi for Sending Email


3. Configure the SMTP - sudo nano /etc/ssmtp/ssmtp.conf

Configuring SMTP on Pi for Sending Email


Step 4:- Then use the following command – echo “hello” | mail –s “test”

This command specifies the content, subject of our mail, as well as the mail id to which our mail will be delivered.


Step 5:- Then we need to create a new file in the python and this can be done by using the following command- nano

Alternate way to do the same step

Open the Python IDE 2.7 or above 3.2, create a new file and save it as by pressing Ctrl + x.  Here, is the name given by the user while saving the file.

Next we need to run the program, this can be done by using the following command-

sudo python


Run Python Script for Sending Email from SMTP server using Pi


Step 6:- 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,

  • Login to Gmail account using your password and username


  • From the right corner go to “My Account“.

Allowing Gmail for SMTP Access


  • Under “Sign-in & security” section locate “Connected apps & sites” and click on it.

Check Connected App and Websites  Gmail


  •  “Allow less secure apps” setting and “On” it.

Allow Less Secure Apps for SMTP Server


Step 7:- Login to your Gmail account and check the mail, if everything works correctly then a mail will be delivered to your mail id.

Received SMTP Test Email using Raspberry Pi


Therefore, we have send SMTP email via Raspberry Pi, you can also send SMTP email using ESP8266 NodeMCU.