Configure Postfix to use SMPT relay when sending emails via PHP mail function

Written by Georgi Stefkoff

Background

Do you ever need to use a SMPT relay when sending email throw PHP mail() function? Probably yes. One way use to use a third party library like phpmailer/phpmailer. This is nice and everything is handled by phpmailer so you do not have to do anything else. But where is the issue? Here: Adding more and more 3rd party libraries increase the application size. In some cases this is a critical and you need to avoid this. And here, I will show you how to configure all of this, in order to save some space.

Requirements

  1. I assume that you have already have a web server with running apache2|nginx and php and the application is up-and-running.
  2. All of the above commands are valid for Ubuntu 18.04 and above
  3. Assuming that you have a mail server like PirvateEmail, MainGun, SendGrid that you want to use it as a relay.

Step 1: Install postfix

First you update packages registry and upgrade the server:

sudo apt update && sudo apt upgrade

Install the libsasl2-modules package:

sudo apt-get install libsasl2-modules

Install Postfix:

sudo apt install postfix

You will be asked to select the environment, and you will want to select Internet Site. Then you will be asked to confirm your hostname. By default it will be populated by the host machine hostname. Make sure you set it as your server FQDM, example: site.domain.tld.

Once postfix is installed, open /etc/postfix/main.cf and change myhostname variable like this:

myhostname = site.domain.tld

For now, save the file. We will return back to it later.

Configure SALS username and password

Usernames and passwords are store in the file /etc/postfix/sasl_passwd. You may not have this file on a fresh installation, but do not worry. We will create it. If it is already existing just append the following content at the end of the file.

Open /etc/postfix/sasl_passwd and add the following rows:

[mail.server.com]:587 username:password

Replace mail.server.com with your mail server. Replace username and password with the credentials that you have for the email server.

NOTE: your mail server could have different port than 587 and you have to be aware of this. By default, Outgoing SMPT port for TLS/STARTTLS is 587

Save the file and exit. Create a Hash database file for Postfix using the postmap command. This command creates a new file named sasl_passwd.db in the /etc/postfix/ directory.

sudo postmap /etc/postfix/sasl_passwd

Secure hashed password (optional)

If you are using multi-user server, and do not want the rest of the users to see your saved sasl passwords, you can follow the steps below in order to restrict the access of the password 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

Configure the Relay

Now, we will configure postfix to send the Outgoing emails via an email server (relay).

First open the main postfix configuration file /etc/postfix/main.cf

Find the variable relayhost and change it to relayhost = [mail.server.com]:587. If the variable do not exists, add it to the end of the file.

Now add the following lines to the end of the file (in the fresh installation, these variables will probably wont be configured):

# enable SASL authentication
smtp_sasl_auth_enable = yes
# disallow methods that allow anonymous authentication.
smtp_sasl_security_options = noanonymous
# where to find sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# Enable STARTTLS encryption
smtp_use_tls = yes
# where to find CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Save the file and restart postfix:

sudo systemctl restart postfix

Override the send email address (optional)

By default, if no sender email is specified, postfix will et the send to be with the user that runs the application that needs to send an email. In your case, most of the time it will be www-data. If your mail server do not have such a user, then an error will be thrown that the user do not have access to send any emails. If you need postfix to override the send email address, follow the steps below:

  1. Open psotfix main configuration file /etc/postfix/main.cfg

2.Add the following line at the end of the file:

sender_canonical_maps = static:user@server.com

  1. Save the file and restart postfix:

sudo systemctl restart postfix

Conclusion

Now you should be able so send emails from you PHP application. These emails will be forwarded to you email server and will be sent to the receiver.

You save some space by skipping third-party library :)

Feel free to add you feedback if this post was helpful to you.

Comments

  1. Markdown is allowed. HTML tags allowed: <strong>, <em>, <blockquote>, <code>, <pre>, <a>.