Mail sending from a Raspberry Pi
February 12, 2021 | StackMail 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.