Configuring Postfix for sending emails via an external SMTP server is useful in many situations, such as when your ISP blocks the default SMTP port (25) or if you want to enhance the deliverability of your emails by leveraging the reputation of your external SMTP provider. This guide will walk you through the steps needed to set up Postfix on CentOS 7 to use an external SMTP server.
Prerequisites
- Clean installation of CentOS 7.
- Access to the root user or a user with sudo privileges.
- Functional external SMTP server (e.g., SendGrid, Mailgun, Amazon SES) including login credentials.
Step 1: Installing Postfix
-
Open a terminal and update the system:
sudo yum update -y
-
Install Postfix and necessary packages:
sudo yum install postfix cyrus-sasl-plain mailx -y
-
Enable and start Postfix:
sudo systemctl enable postfix sudo systemctl start postfix
Step 2: Configuring Postfix
-
Open the main Postfix configuration file in a text editor:
sudo vi /etc/postfix/main.cf
-
Configure Postfix to send emails via the external SMTP server by adding or modifying the following lines at the end of the file:
relayhost = [smtp.example.com]:587 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_security_level = encrypt smtp_tls_note_starttls_offer = yes
- Replace
smtp.example.com
with the address of your external SMTP server. - Port
587
is commonly used for secure email transmission (SMTPS), but you may need to use a different port depending on your provider.
- Replace
-
Create a file for storing the SMTP server login credentials:
sudo vi /etc/postfix/sasl_passwd
Add the following line, replacing the placeholders with your actual values:
[smtp.example.com]:587 username:password
Secure the file:
sudo chmod 600 /etc/postfix/sasl_passwd
-
Generate a hash database for the file containing login credentials:
sudo postmap /etc/postfix/sasl_passwd
Step 3: Restarting Postfix
- Restart Postfix to apply the changes:
sudo systemctl restart postfix
Testing the Configuration
Test the configuration by sending a test email:
echo "Test email from Postfix" | mail -s "Test Postfix" This email address is being protected from spambots. You need JavaScript enabled to view it.
- Replace
This email address is being protected from spambots. You need JavaScript enabled to view it.
with your actual email address where you want to receive the test email.
Congratulations, you have successfully configured Postfix on your CentOS 7 server for sending emails via an external SMTP server. This configuration helps improve the deliverability of your emails and enables you to utilize the services of external SMTP providers.