Part 3 - Setting Up Mail Server (Postfix)
This is the third post in the series of how to host your own git server on a Raspberry Pi
You have successfully configured your Raspberry Pi in the previous post. In this post we will install Postfix, one of the most widely used email servers in the Linux environment. It will be the foundation for any services in our Raspberry Pi to send email notifications, including our git server.
1. Install Postfix
The first step is to install Postfix from Linux software repository:
sudo apt install -y postfix
Upon completion, you will see the Postfix configuration screen:
Click <Ok>
to pass this screen, which presents mail server configuration options. In the next screen, we are going to select ‘Internet Site’. This will allow us to build a system that can exchange mail locally, and also with public clients by routing our emails through an external server. In the final screen, enter the Mail Name. (You can use <HOSTNAME>.<DOMAIN>
from part 2).
2. Configure Credentials
Next step is to determine the credentials you are going to use for outgoing emails. By default, your devices cannot send emails directly to the external world as it is very likely that your ISP has already blocked the port 25 of your home network (common practice among many Internet Service Providers (ISPs) to reduce the number of spam emails passing through their network).
We will set up a secure SMTP configuration to route our emails through an external email service (such as gmail). This service will then send the emails to correct destination on our behalf. This is called an email relay setup.
We need to find the SMTP domain and port settings provided by our external mail service. I am using OVH Mail, so I found the following settings from their mail configuration page.
<SMTP-DOMAIN>
— ssl0.ovh.net<SMTP-PORT>
— 465
You can find similar settings from some common email providers:
- Google Mail
- Yahoo Mail
Once you find the corresponding settings, edit Postfix’s sasl file:
sudo nano /etc/postfix/sasl_passwd
Put your information in the following format:
[<SMTP-DOMAIN>]:<SMTP-PORT> <RELAY-EMAIL>:<PASSWORD>
(eg. [ssl0.ovh.net]:465 hello@example.com:123456)
Save and exit, then run following command to propagate your changes:
sudo postmap /etc/postfix/sasl_passwd
3. Configure Postfix
Next step is to modify the configuration file for Postfix:
sudo nano /etc/postfix/main.cf
Add your SMTP domain/port configuration by the relayhost field:
relayhost = [<SMTP-DOMAIN>]:<SMTP-PORT>
Additionally, add the following lines to represent your sasl credentials:
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
If your email relay service is set up to use port 465 (SMTP over TLS), you will also need to add SSL/TLS support to your configurations:
smtp_use_tls = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_tls_security_level = encrypt
smtp_tls_wrappermode = yes
Once you are done with the main configuration file changes, you also need to edit the master configuration file.
sudo nano /etc/postfix/master.cf
Uncomment the following line to enable SMTP over TLS:
smtps inet n - y - - smtpd
4. Restart Postfix
Once you get all the configurations in, give Postfix a restart:
sudo systemctl restart postfix.service
5. Install MailUtils
Next step is to install a mail client that we can send a test email.
sudo apt install -y mailutils
6. Test Sending a Mail
Run the following command to test out sending an email:
echo “Test mail text” | mail -s “Test Subject” -aFrom:<RELAY-EMAIL> <DESTINATION-EMAIL>
Your mail will start its journey from your Raspberry Pi to your relay email. It will then get re-transmitted by the relay service, and hopefully will arrive in the inbox of your destination email.
Some Useful Commands:
mailq
: Shows mail in queuesudo postfix flush
: Attempts to send all mail in queuesudo postsuper -d ALL
: Deletes all mail in queuemail, delete *
: Delete all incoming mail
Congratulations — You have set up your mail server! Next chapter is going to talk about setting up GitLab on our Raspberry Pi.
You can follow my future articles on building Internet of Things devices and general chat regarding to life at Google at Baking Pi — An IoT Blog. :)