How to configure and secure your Postfix email server

Once you have your application server up and running, you are going to need a good email server to deliver your emails. I have been using postfix for all my servers and below is the configuration I generally use.

Installation of Postfix on CentOS 6

yum install postfix

Sendmail is installed by default, so it is better to stop and remove it

service sendmail stop
yum remove sendmail

Postfix contains two configuration files main.cf and master.cf, you will need to modify main.cf for basic configuration. Also, postfix parameters can be defined like shell variables and can be used with a dollar sign preceding them. They do not need to be defined before they are used. Postfix will only look for a parameter when it is needed at rumtime.

Configuring postfix

vim /etc/postfix/main.cf

Uncomment the lines below

#Add the hostname of your machine
myhostname = yourhostname.com
#From Domain to be used when mail is sent from this linux machine
myorigin = $myhostname
#The network interface to receive mail on, I prefer localhost as I only want emails from this system to be delivered
inet_interfaces = localhost
# The protocol to use when postfix will make or accept a connection. You can use “all” if you want to enable IPv6 support
inet_protocols = ipv4
#Domains to receive email for
mydestination = $myhostname, localhost.$mydomain, localhost
#Only forward emails for the local machine and not machines on the network.
mynetworks_style = host

Start postfix

service postfix start

This basic postfix configuration should enable your machine to send emails. You can verify the same by sending an email and checking “maillog” log file.

But this configuration is not enough, as your emails will mostly end up in spam. You will need to add an SPF, PTR and DKIM record. You may still get emails delivered in spam due to your IP address being blacklisted, mostly due to a previous abuse of your vps.

An alternative or a better way would be to use a third party provider like Gmail or even Mailgun.
I use Mailgun as they give you 10,000 emails free every month as compared to Gmails 100 or so per day.

In “/etc/postfix/main.cf” you will need to add “smtp.mailgun.com” as your “relayhost”, enable “SASL” authentication so postfix can connect and authenticate to the remote Mailgun server.

Postfix does not implement “SASL” authentication by itself, hence you will need to install “cyrus-sasl-plain”.

sudo yum install cyrus-sasl-plain

If you do not install this package on Centos 6 then you will get an error “SASL authentication failed; cannot authenticate to server smtp.mailgun.org[50.56.21.176]: no mechanism available)”

Restart postfix

sudo service postfix restart

Securing Postfix with TLS
Postfix supports TLS a successor to SSL which allows you to encrypt data using key based authentication. I recommend reading http://www.postfix.org/TLS_README.html on how tls works with postfix.

In order to use TLS you will need to generate a private key and a certificate which is signed by a Certificate Authority. In this example, I will be using a Self Signed Certificate.

Security level “may” means announce STARTTLS support to remote SMTP clients, but clients do no need to use encryption., I have used it here as per mailgun docs, but you can use “encrypt” if you want to force TLS encryption.

I’ll argue that using a 3rd party server is not better than rolling your own mail server, of course if you know what you are doing.

Even if you’re not that tech savy, there are many great articles on the Internet regarding this, including yours, but anyway, I highly recommend setting up your own mail server, for example by following this great article written by RoseHosting.com.

It has always worked for me,

thanks,

chris

http://techarena51.com Leo G

Chris, How do you tackle email delivery? Most of the time I have setup my email server and emails have landed up in spam, no matter if you have added your own dkim, spf etc. It could be a variety of reasons including a new IP address, I prefer a third party service dealing with the same and concentrating on my app

Jeff

static I.P. – reverse DNS and time to garner a decent reputation on the internet all go toward a solid mail server. without reverse dns most mail server will not accept your mail. without a static IP (i.e. an I.P. in a DHCP pool of an upstream provider) most sites will not accept your email.
DKIM and SPF go a long way toward proving you are who you say you are but with out the DNS and static I.P. you wont get far enough for them to matter.

I have been using postfix as it is easier to troubleshoot, plus more the softwares more the vulnerabilities, hence i prefer to use postfix as compared to using a combination of exim and dovecot as in your link.