Setting up postfix as a relay on Ubuntu 24
published on
Introduction
When setting up a server you may want it to be able to send email with alerts or other messages that you might like to see.
This article will show you how to quickly set up postfix to forward email like this to an email service which will do the actual sending for you.
Setup
First install postfix by running sudo apt install postfix mailutils libsasl2-modules
. At the prompt choose Internet Site
and enter the domain name of the email account you wish to send email from.
Once you return to the command line we now need to configure postfix. Edit the /etc/postfix/main.cf
file and either change or add the following lines:
myhostname = [domain entered earlier]
# Enable auth
smtp_sasl_auth_enable = yes
# Set username and password
smtp_sasl_password_maps = static:[email_name]:[email_password]
smtp_sasl_security_options = noanonymous
# Turn on tls encryption
smtp_tls_security_level = encrypt
header_size_limit = 4096000
# Set external SMTP relay host here along with a port number.
relayhost = [email_server_hostname]:587
# accept email from our web-server only
inet_interfaces = 127.0.0.1
Make sure to add your own hostname and email account details for the service that will send the emails on this server's behalf.
Now restart postfix and make sure to allow the port you provided above through your firewall:
sudo systemctl restart postfix && sudo systemctl enable postfix
sudo ufw allow 587
Finished
You have now finished setting up postfix to relay emails through another service. This can be tested by running the following command and updating the two email addresses below:
echo "This is a test email body." | mail -s "Test email subject" -a "From: [[email protected]]" [[email protected]]