Part three of my Raspberry Pi Project Series
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.
sudo apt install postfix libsasl2-modules
During installation, Terminal User Interface(ncurses) will popup for Postfix configuration.
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.
raspberrypiYou can change the hostname later on by editing
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.
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.
Click on Generate, and you will get this popup screen.
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,
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
#Use transport layer security (TLS) encryption
#Do not allow anonymous authentication
#Specify where to find the login information
#Where to find the certificate authority(CA) certificate
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 <username>@gmail.com
Press enter and keep typing,
Subject: TestingPress 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.
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
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.
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.
If you are unable to reach Gmail server, try ping
ping -c 5 smtp.gmail.com
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.