Use postfix with an external SMTP

postfix Postfix for what?

If you want to send emails from your sever (using sendmail with bash or mail() with PHP) you will need a Mail Transfert Agent (MTA). In my case, I use Postfix, which is free, easy to configure and can be configure with an external SMTP.

Why using an external SMTP?

Or course, you can configure you own internal SMTP to send email (and there is plenty of tutorials online to do that), but depending on your IP, your mail server could be black listed if someone have used it before you to send spam. So its easier to use the SMTP of your regular mail adress, and it should avoid this problem.

Install Postfix

First, you need to install Postfix:

aptitude install postix

You should keep the default configuration Internet Web Site and fill it with your own domain name DOMAIN.TLD.

Get credentials

To send email to an external SMTP you need to stop your credidentials in a file to avoid to send them in clear at each mail. We will create a file to store them in a secure location with a restricted access.

touch /etc/postfix/sasl/passwd chmod 600 /etc/postfix/sasl/passwd

Then, you need to add into this file the following content:


For example, you should have []:587 for Gmail, or []:587 for OVH.

Finally, you need to store then in postfix:

postmap /etc/postfix/sasl/passwd
This will create a passwd.db database in the same folder.

Configure Postfix

We need to edit the default configuration file to add our new credidentials. Open the file /etc/postfix/ and edit on the line mydestination. Leave it empty if you will not need to receive mail on this server:

# The mydestination parameter specifies the list of domains that this
# machine considers itself the final destination for.
mydestination =

Then, you should change the relayhost for you external SMTP:


Finally, you will add the authentification parameters:

smtp_sasl_auth_enable = yes                          
smtp_sasl_password_maps = hash:/etc/postfix/sasl/passwd
smtp_sasl_security_options = noanonymous
Add SSL certificate [Optional]

You can also add you SSL certificate if you have one:

# TLS server parameters
## This section is necessary if you're going to be using your Postfix install
## to relay from other LAN hosts
smtpd_use_tls = yes
smtpd_tls_cert_file = /PATH/TO/YOUR/CERTIFICATE.CRT
smtpd_tls_key_file = /PATH/TO/YOUR/PRIVATE.KEY  
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache  
smtpd_tls_loglevel = 1  
smtpd_tls_received_header = yes  
smtpd_tls_session_cache_timeout = 3600s  
smtpd_tls_req_ccert = no  
smtpd_tls_ask_ccert = yes

And then, restart Postfix:

service postfix restart

Check if it works...

Now it is time to try to see if you can send mail from you server. Try that:

sendmail To: MAIL_ADRESS_TO From: MAIL_ADRESS_FROM Subject: Testing! Hi there! Checking to see if Postfix works! .

Refresh your mailbox, wait a couple of minutes, ... If you don't receive your mail, you should check your log file:

tail n100 -f /var/log/maillog

Then... Google should be able to help you.