Introduction
If you’re managing your own Linux server with Postfix and want to route all outgoing mail through a specific SMTP relay (also known as a smarthost), you’ll need to configure Postfix accordingly to use authentication and encryption.
Prerequisites
- Hostname of the external SMTP relay — in this case:
relay.stacksmtp.com
- SMTP authentication credentials (username and password)
- Port 587 or 465 must be open in your server firewall
- This guide assumes you’re using a Debian/Ubuntu or CentOS/RHEL-based system
- Postfix and
libsasl2-modules
installed on your server
Step 1: Install Required Packages
Run the following command to install required packages:
sudo apt update
sudo apt install mailutils libsasl2-modules -y
For RHEL/CentOS systems, use:
sudo yum install postfix cyrus-sasl-plain mailx -y
Step 2: Edit Postfix Main Configuration
Edit the Postfix configuration file:
sudo nano /etc/postfix/main.cf
Add or update the following lines:
relayhost = [relay.stacksmtp.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Step 3: Provide SMTP Credentials
Create the credentials file:
sudo nano /etc/postfix/sasl_passwd
Add this line:
[relay.stacksmtp.com]:587 your_username:your_password
Then generate the hash database and set permissions:
sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
Step 4: Restart Postfix
Apply the changes by restarting Postfix:
sudo systemctl restart postfix
Step 5: Test Your SMTP Relay
You can test sending an email using:
echo "This is a test message" | mail -s "Test Email" recipient@example.com
Conclusion
You have now successfully configured your Linux server running Postfix to relay all outgoing mail through an external SMTP server with authentication and TLS encryption. This setup improves deliverability and centralizes your mail traffic through relay.stacksmtp.com
.