If you have a homelab consisting of various services running on an assortment of random hardware, it often proves essential to have a simple SMTP relay for sending out emails. The relay server can consolidate email traffic and push them through a single authenticated email account provided by your hosting provider.
In this article, we will walk you through setting up an SMTP relay server using Ubuntu and Postfix. This setup will allow you to gather email from your local network and deliver them via an SMTP-enabled account from your hosting provider.
Prerequisites
- A fresh Ubuntu server or vm. This guide is using an Ubuntu 22.04 vm running in a VMware environment.
- Fully patched
- Static IP or DHCP Reservation (best-practice for most services).
- An account with your hosting provider, with SMTP-enabled service.
- The HomeBLab Project’s List of Hosting Services
- Basic knowledge of Linux command-line operations.
Step 1: Install Postfix
First, we’ll need to install Postfix, a free and open-source mail transfer agent that routes and delivers electronic mail. To do this, run the following commands:sudo apt-get update
sudo apt-get install postfix mailutils
During the installation process, a configuration window will appear:
- Choose “Internet Site” for the type of mail configuration.
- For the system mail name, enter the domain name of the email address you’re using, usually everything to the right of the ‘@’ symbol.
Step 2: Configure Postfix
Open the main Postfix configuration file with your preferred text editor. We will use nano in this guide:
sudo nano /etc/postfix/main.cf
Find and modify the following lines in the file to match the information below. If a line isn’t found, add it and make it match the information below.:
inet_interfaces = all
mydestination = localhost
relayhost = [smtp.yourprovider.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
mynetworks = [safe networks]
Replace “safe networks” with a comma-separated list of subnets in CIDR format. Clients sending mail from these subnets will be allowed to do so without authentication. Set this carefully!
Replace “smtp.yourprovider.com” with the SMTP server of your hosting provider.
Once complete, write out your changes and exit nano.
Step 3: Set Up Authentication
Now, we need to set up the SMTP username and password that Postfix will use to send emails.
First, create a new file:
sudo nano /etc/postfix/sasl_passwd
Next, add the following line, replacing the SMTP server, username, and password with your information:
[smtp.yourprovider.com]:587 yourusername:yourpassword
Once complete, write out your changes and exit nano.
Now, create a hash db file for Postfix by running the following command:
sudo postmap /etc/postfix/sasl_passwd
Then, for security reasons, delete the unencrypted sasl_passwd file:
sudo rm /etc/postfix/sasl_passwd
Step 4: Update File Permissions
You need to set the appropriate permissions for the sasl_passwd.db file to ensure the Postfix user can read it:
sudo chown root:root /etc/postfix/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl_passwd.db
Step 5: Create a Mapping File
This map tells Postfix that all email relayed through this server should be sent as if it originated from the account from our hosting provider.:
sudo nano /etc/postfix/sender_canonical
Then, add your mapping rule.:
@local.domain.example.local yourusername@yourprovider.com
Once complete, write out your changes and exit nano.
Step 6: Create a Postfix Lookup Table
Now you need to create the Postfix lookup table from the file created in the last step:
sudo postmap /etc/postfix/sender_canonical
This command will create a file called ‘sender_canonical.db’ that Postfix will use. Now we need to edit the Postfix configuration again.:
sudo nano /etc/postfix/main.cf
Add the following line:
sender_canonical_maps = hash:/etc/postfix/sender_canonical
Once complete, write out your changes and exit nano.
Step 7: Restart Postfix
Finally, apply all the changes by restarting or reloading Postfix:
sudo systemctl restart postfix
Step 8: Test Your Setup
To make sure your SMTP relay works properly, you can send a test email to any of your active email accounts. To do this, use the ‘mail’ command:
echo "Test mail body" | mail -s "Test mail subject" your-email@example.com
If everything works correctly, you should receive this test email.