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.

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:

Postfix Configuration Options

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).

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-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:


(eg. []:465

Save and exit, then run following command to propagate your changes:

sudo postmap /etc/postfix/sasl_passwd

Next step is to modify the configuration file for Postfix:

sudo nano /etc/postfix/

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/

Uncomment the following line to enable SMTP over TLS:

smtps     inet  n       -       y       -       -       smtpd

Once you get all the configurations in, give Postfix a restart:

sudo systemctl restart postfix.service

Next step is to install a mail client that we can send a test email.

sudo apt install -y mailutils

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.

mailq : Shows mail in queue
sudo postfix flush : Attempts to send all mail in queue
sudo postsuper -d ALL : Deletes all mail in queue
mail, 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. :)

Systems Engineer, specialized in building Internet of Things (IoT) devices and applications. Harvesting crops at Google. 💻

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store