The Hotel Hero

Notes by a Sysadmin


Cluster | Philosophy | Stack

Mail sending from a Raspberry Pi

February 12, 2021 | Stack

Mail notifications

The reason you might want mail sending (smtp) capabillity on a Pi, is notifications. This is a nice feature, if some thing goes wrong as Pi's are often setup headless for a specific task. 

Start by setting up mailutils and postfix:

 sudo apt install mailutils
(and)
sudo apt install postfix
(and)
sudo apt install libsasl2-modules
(or in one line)
sudo apt install mailutils postfix libsasl2-modules

You will be prompted for som selection choises -> choose Internet site -> Name: could_by_backupserver.local 

Now the easiest thing to do now, is just to use a third party smtp server, like Google. Don't use your main account, add another account that you use for ex. notifications.

Go to you Gmail accounts Security Settings and choose "2-Step Verification" and enable 2FA. Then generate a specific App Password, choose "other" and use the name Postfix and generate new password. Save this password for your app access.

Add a file in /etc/postfix/sasl/ (sudo nano /etc/postfix/sasl/sasl_passwd) and fill in your information:

[smtp.gmail.com]:587            your_account@gmail.com:you_new_password

(save and)

sudo chmod 600 sasl_passwd

Add the following to the bottum of the /ets/postfix/main.cf:

relayhost = [smtp.gmail.com]:587 
# Enable SASL authentication
smtp_sasl_auth_enable = yes
# Disallow methods that allow anonymous authentication
smtp_sasl_security_options = noanonymous
# Location of sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
# Enable STARTTLS encryption
smtp_tls_security_level = encrypt
# Location of CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Make sure there are no double entry of the above and save. Restart Postfix:

sudo systemctl restart postfix

------------------------------------

Now, with a bit of luck (a lot of stuff could have changed since this post) you should be able to test your setup. Do the following (when done with the message part hit enter "." and enter)

sendmail recipient@mail.com (enter)
Subject: Some text (enter)
Your message here (enter)
. (enter)


That should be it.


About

I'm a Sysadmin, network manager and cyber security entusiast. The main purpose of this public "notebook" is for referencing repetitive tasks, but it might as well come in handy to others. Windows can not be supported! But all other OS compliant with the POSIX-standard can (with minor adjustments) apply the configs on the site. It is Mac OSX, RHEL and all the Fedora based distros and Debian based (several 100's of OS's), all the BSD distros, Solaris, AIX and HP-UX.

Links