If You are using your Raspberry Pi as a Web Server, then the main challenge you may face is that how to send Email using Raspberry Pi. But in this post, I’ll explain to you that how can you setup your own Gmail SMTP Server using Raspberry Pi. If you don’t know how can you use your Pi as Web Server, then check this post.

So in this post, We will use the Gmail SMTP server to send Email with our Raspberry Pi. we will use ssmtp, a program which delivers email from a local computer to a configured mailhost (mailhub). It is not a mail server and does not receive mail, expand aliases or manage a queue. So let’s start

What You Need for this Tutorial

1.  Raspberry Pi with SSH Access

2. A Gmail Account

1. Steps to Setup SMTP server on Raspberry Pi

Step 1: Login to your Gmail account, then in navigate to setting ->Accounts and Import ->Other Google Account settings -> Security and turn on the Less secure app Access as 

Gmail SMTP Server using Raspberry Pi

Gmail SMTP Server using Raspberry Pi

Step 2: Now SSH into your Raspberry Pi using the following command. (Don’t forget to replace credentials with your own)

ssh pi@192.168.0.100

Step 3: After that, install the SSMTP by the following command

sudo apt-get install ssmtp

Step 4: Now we need to backup the original configuration file i.e ssmtp.conf. You can do that by the following command

sudo mv /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.bak

Step 5: In this step, create a new conf file with nano editor by the following command

sudo nano /etc/ssmtp/ssmtp.conf

Step 6: Now Copy and Paste the following code into that ssmtp.conf file

[email protected]
mailhub=smtp.gmail.com:587

FromLineOverride=YES
[email protected]
AuthPass=your_password
UseSTARTTLS=YES
UseTLS=YES

# Debug=Yes

Note that, AuthUser and AuthPass is your Gmail Id Credentials. also replace root with your Gmail address

Step 7: Now you can save and quit out of the file by pressing Ctrl+X then Y and then Enter 

Step 8: In this step, secure the configuration file by the following command

sudo groupadd ssmtp
sudo chown :ssmtp /etc/ssmtp/ssmtp.conf

Step 8: Once the configuration is completed, you can now send the Email using the command line.for that type the following command

echo "Hello world email body" | mail -s "Test Subject" [email protected]

Now you can check your Inbox and verify that Email is received or not

2. Steps to configure PHP file 

The main challenge in this project is that we need to specify the path for the SMTP server so that we can send the Email from our Raspberry Pi using  PHP mail() function. But don’t worry, I have simplified that step too. we just need to change a line in the php.ini file. For that, follow the steps below

Step 1: Open the php.ini file using the following command

sudo nano /etc/php/7.0/apache2/php.ini

Please note that, In my case I’m using Php 7.0 . In your scenario there may be diffrent version of PHP. so you can loacate the php.ini file by whereis command

Step 2: Once the php.ini file is opened, locate the SMTP server line in nano editor. You can search it by Ctrl+W and type smtp and then hit enter

Step 3: Now you will find a line ; For Unix only. In that there will be a line sendmail_path. Now set the path, after that complete line will be 

sendmail_path = /usr/sbin/ssmtp -t -i

Gmail SMTP Server using Raspberry Pi

Step 4: Now you can save and quit out of the file by pressing Ctrl+X then Y and then Enter

After that you can send Email using PHP mail() function. The Email will be delivered using your Gmail Account

If you facing any issue then you can ping me here. I’ll get to you soon as the system notifies us.


Rajesh Ranjan

I'm a tech enthusiast currently pursuing B.Tech in Computer Science & Engineering. trying to make things as simple as possible

0 Comments

Leave a Reply