Set Up Postfix Mail Relay
Here are the general steps to configure Postfix to use an external SMTP server:
-
Install Postfix if it's not already installed. Depending on your server's operating system, the command might be
sudo apt-get install postfix
orsudo yum install postfix
. -
Open the Postfix configuration file. The main configuration file for Postfix is usually located at
/etc/postfix/main.cf
. You can open it with a text editor likevi
ornano
:sudo nano /etc/postfix/main.cf
. -
Set relayhost. In the configuration file, find the line that begins with
relayhost =
and set it to your SMTP server, like so:
relayhost = [mail.australianpawn.com.au]:587
Replace 587
with the correct port if your SMTP server uses a different one. The brackets around the hostname tell Postfix to not perform MX lookup and to instead use the specified host as is.
eg: relayhost = mail.australianpawn.com.au:465
- Configure SMTP Authentication. If your SMTP server requires authentication, add the following lines to the end of the
main.cf
file:
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
- Create the sasl_passwd file. Now you'll need to create the file
/etc/postfix/sasl_passwd
that was referenced in the previous step. In this file, you'll specify your SMTP server's credentials:
[mail.australianpawn.com.au]:587 username:password
Replace username
and password
with the actual username and password for your SMTP server.
- Hash the sasl_passwd file. Postfix does not read the
sasl_passwd
file directly. Instead, it reads a .db file generated fromsasl_passwd
. Generate this file using thepostmap
command:
sudo postmap /etc/postfix/sasl_passwd
This will create a file named sasl_passwd.db
in the same directory.
- Secure the sasl_passwd files. For security reasons, restrict access to the
sasl_passwd
andsasl_passwd.db
files:
sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
- Reload or restart Postfix. Finally, you need to reload or restart Postfix to apply the changes:
sudo systemctl restart postfix
After these steps, Postfix should be configured to relay all outgoing mail through your SMTP server. You can test this by sending a test email through the command line:
echo "Test email body" | mail -s "Test email subject" your@email.com
Remember to replace your@email.com
with your actual email address.
Please note, these instructions are for a general SMTP server. If you are using a specific email service provider, they may require additional configurations such as using encrypted connections (TLS) or specific security methods. Refer to your email service provider's documentation for these details.
Additionally, be aware that some ISPs block outbound SMTP traffic (port 25) as an anti-spam measure. If you find that you are unable to send mail, you may need to check with your ISP to see if this is the case.
Since port 465 is used for SMTP over SSL, you'll need to update your Postfix configuration to use SSL when talking to the relay host. Add or update these lines in your /etc/postfix/main.cf
file:
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_wrappermode = yes
smtp_tls_security_level = encrypt
smtp_sasl_tls_security_options = noanonymous
The smtp_sasl_password_maps
directive points to a file that will contain your SMTP server username and password. You'll need to create this file (e.g., /etc/postfix/sasl_passwd
) with the following format:
mail.australianpawn.com.au:465 username:password
Replace username:password
with your actual username and password for your SMTP server.
After you've done that, you'll need to create a Postfix lookup table from the sasl_passwd file:
sudo postmap /etc/postfix/sasl_passwd
This will create a .db
file (sasl_passwd.db) in the same directory. Postfix will use this file for looking up the username/password.
Finally, you should secure the sasl_passwd
and sasl_passwd.db
files since they contain sensitive information:
sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
After all these changes, you need to reload or restart Postfix for the changes to take effect:
sudo systemctl restart postfix
Remember, these steps require that your SMTP server is configured to accept mail from your Magento server's IP address and that the username and password you provided are correct.
This will send an email with the subject "Test Subject" and the body "This is a test email" to example@example.com
.
Just make sure that the mailutils package is installed on your server. If it is not, you can install it with
sudo apt install mailutils
To test:
echo "This is a test email" | mail -s "Test Subject" example@example.com
-
Check the mail logs: The mail logs will show you the communication between your mail server and the other mail server. You can find these logs in
/var/log/mail.log
or/var/log/mail.err
.You can tail the mail log in real time as you send a test email with the command
tail -f /var/log/mail.log
.You should see entries for your test email going out. If you see error messages, they may give you a clue as to what's going wrong.
-
Check the mail queue: You can check the mail queue to see if your emails are stuck. You can do this with the command
mailq
orpostqueue -p
.
If for some reason outgoing connections are not allowed, you can allow them with:
sudo ufw default allow outgoing
This will allow your server to make outgoing connections to any external server, including your SMTP server.
To clear the mail queue in Postfix, you can use the postsuper
command with the -d
option followed by ALL
. Be aware that this will delete all messages currently in the queue:
sudo postsuper -d ALL
This command will prompt you for a password because it requires superuser (root) permissions. After running this command, all emails in the Postfix queue will be deleted.
ou can use the tail
command to view the most recent entries in the log, for example:
tail -n 50 /var/log/mail.log