Setting up a SMTP service on Raspberry Pi

Akshul Goyal
5 min readFeb 28, 2021

--

Part three of my Raspberry Pi Project Series

Photo by Harrison Broadbent on Unsplash

Let’s setup our Pi so as to send mails and attachments using SMTP service and Gmail. This will be helpful in my next post where I will explain my use case.

I am not an expert in SMTP service, so I am not going into detailed theoretical explanations. I am only giving step-by-step process of setting up the service. Let’s dive in.

I am using Postfix for the setup. It is a Mail transfer Agent which can basically talks to other Agents to send and receive emails. It supports SMTP and have easy to use commands.

Installing —
sudo apt install postfix libsasl2-modules

During installation, Terminal User Interface(ncurses) will popup for Postfix configuration.

Postfix Configuration

Select Internet Site or Internet with smarthost
I have tested installation with both the options. These options are for setup where email is sent and received directly using SMTP.

Next window will are you for typing in System Mail name. Type in hostname eg. raspberrypi
You can change the hostname later on by editing etc/postfix/main.cf file.

System Mail name

Select OK to proceed. Now ncurses will close and the setup will initiate and will take some time to complete.

Setup Application password —
For integrating Postfix to Gmail, you need to get an App password. Go to your Google Accounts and then Security section and setup 2-step Verification if not already done. After this is done, click on “App passwords” under 2-step Verification. The login prompt might appear and you need to type in your password once.

Select App password

You will see App password section. Here you have to create password for postfix. Select Mail in “Select App” drop down menu. And select “Other(custom name)” in Select device drop down menu. Type in “raspberrypi” or anything else of your liking.

App password Menu

Click on Generate, and you will get this popup screen.

Password generated. (FYI: This password doesn’t exists anymore)

Copy this password as it is, and paste somewhere safe, and don’t share it with anyone. Now you can click on DONE.

Configure Postfix SASL —
For more information about this, visit this site.

Now, in order to setup Postfix with Gmail, you need to use the App password you have securely copied somewhere safe.

Type in this command
sudo nano -B /etc.postfix/sasl/sasl_passwd
This file might not exists to begin with, so you will get an empty file opening up in nano editor. Regardless, add the following line,
[smtp.gmail.com]:587 <username>@gmail.com:<password>
Type in your username and the App password in placeholders. Save the file and exit.

This basically SASL being connected to smtp.gmail.com host at port 587, using your username and password.

Now, change this file permissions
sudo chmod u=rw,go= /etc/postfix/sasl/sasl_passwd

Now, turn this file into a hash file for postfix
sudo postmap /etc/postfix/sasl/sasl_passwd

The above command will create a new file “sasl_passwd.db”. To set permissions for this file as well,
sudo chmod u=rw,go= /etc/postfix/sasl/sasl_passwd.db

Finally Configuring Postfix —
sudo cp /etc/postfix/main.cf !#$dist
sudo nano /etc/postfix/main.cf

Change the line containing relayhost to this,
relayhost = [smtp.gmail.com]:587

And add a few more lines at the end,

#Enable authentication using SASL
smtp_sasl_auth_enable=yes
#Use transport layer security (TLS) encryption
smtp_tls_security_level=encrypt
#Do not allow anonymous authentication
smtp_sasl_security_options=noanonymous
#Specify where to find the login information
smtp_sasl_password_maps=hash:/etc/postfix/sasl/sasl_passwd
#Where to find the certificate authority(CA) certificate
smtp_tls_CAfile=/etc/ssl/certs/ca_certificates.cert

Save the file and exit

Restart the Postfix service — sudo systemctl restart postfix

Sending mails —
Now Postfix setup is done. To test the service, send a dummy email to yourself, type in this command on terminal

sendmail

$ sendmail <username>@gmail.com
Press enter and keep typing,
Subject: Testing
Mail service
.
Press enter

<username> is recipients username. For testing just use your gmail id.
.(dot) is used to end the message and then pressing enter sends the mail.

Mail received

Attaching Files on mails —
The above command can only send text mails. To send attachments on mail, there is an email client called “sharutils”.
sudo apt install sharutils

uuencode for attachments

Now you have to use command called “uuencode” to attach files with emails. uuencode file.txt myfile.txt | sendmail -v <user>@gmail.com

file.txt is the file name (or path) that you want to attach.
myfile.txt is the filename that you need to type in. This name will be used to attach your file. This name can be same as your original file name, or you can type in a different name as well.
-v is for verbose.

Attachment received

I have tested sending text and mp4 files as attachments.

It took me a lot of time to try and setup attachments in Postfix. For your easy reference, here’s the link.

Though this above commands works fine, I was not able to send text in message with the attached file. You can only send either text only mails or attachments only mails.

Troubleshooting —
If you are unable to reach Gmail server, try ping
ping -c 5 smtp.gmail.com

ping to check connection

If you are getting something like this
ping: smtp.gmail.com: Name or service not known
then check your connection.

That’s a wrap. In my next post, I will explain the use case for which I have setup this SMTP service in the first place.

--

--

Akshul Goyal

Hacking the Physical World | Senior Embedded Systems Engineer @ PiRhoAlpha Research (ActiveBuildings) | I write posts about AVR and Raspberry Pi.