Configuring Postfix on a VPS to Use an External SMTP Relay

Configuring Postfix to use a third-party SMTP relay is a common solution when facing restrictions on ports 25 and 465. Below are the general steps to achieve this:

1. Choose an SMTP relay provider: Select a trusted SMTP relay service provider such as SendGrid, Mailgun, Mailchannels, or Gmail. Create an account with the provider and obtain the necessary credentials to send emails through their service.

2. Install Postfix:
- For Debian/Ubuntu-based systems:
```bash
sudo apt-get update
sudo apt-get install postfix
```
- For CentOS/RHEL-based systems:
```bash
sudo yum install postfix
```
During installation, choose the "Internet Site" option and configure the outgoing mail system.

3. Configure Postfix to use the SMTP relay:
- Edit the main Postfix configuration file:
```bash
sudo nano /etc/postfix/main.cf
```
- Add or modify the following lines to configure the SMTP relay (ensure to replace the values in brackets with the actual information):
```plaintext
relayhost = [smtp.example.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
```

4. Create the authentication password file:
- Create a file `/etc/postfix/sasl_passwd` to store the SMTP relay authentication credentials:
```bash
sudo nano /etc/postfix/sasl_passwd
```
- Add the following line, replacing `<smtp_server>`, `<username>`, and `<password>` with the actual information:
```plaintext
[smtp.example.com]:587 username:password
```
- Save the file and then run the following commands to generate the hash file and secure it:
```bash
sudo postmap /etc/postfix/sasl_passwd
sudo chown root:root /etc/postfix/sasl_passwd*
sudo chmod 600 /etc/postfix/sasl_passwd*
```

5. Reload Postfix configuration:
- After making the changes, reload the Postfix configuration to apply them:
```bash
sudo systemctl reload postfix
```

6. Test email sending:
- Send a test email using the `mail` or `sendmail` command on your server to verify that emails are being correctly forwarded through the SMTP relay:
```bash
echo "This is the body of the email" | mail -s "This is the subject line" admin@test.com
```

7. Monitor the mail log for potential issues:
```bash
tail -f /var/log/mail.log
```

NOTE: If the provider blocks ports 25 and 465, it's necessary to change the ports to the following: 2525, 587.

  • relay, smtp, postfix
  • 36 Users Found This Useful
Was this answer helpful?