Today I am going to show you how to install and configure Postfix SMTP server so that it could be use to send emails by other server applications. Note that, the Postfix server configuration in this certain tutorial is only for sending emails from the server and not for handling incoming mails.
At the moment, you may use a third party service to send emails on behalf of your server or not using such service at all. By following this tutorial, you’ll be able to run your own local mail server as a send-only-SMTP server to send email notifications from your applications.
- You should have a VPS / droplet with root access.
- You should be able to access your server with a SSH client like PuTTY (tutorial).
- A valid domain name (which is used to send emails) point to your server.
Note that, your server hostname should match this domain name or a sub-domain. You can see current hostname by issuing hostname command. Follow this tutorial, if you are in need of changing server hostname.
In this tutorial, I am going to use an Ubuntu 16.04 VPS server and zeedr.cf Freenom domain to send emails using my server.
Firstly, update your system packages:
sudo apt-get update
Then install mailutils package. This will install Postfix SMTP server and other necessary programs for Postfix to function properly.
sudo apt-get install mailutils
Then after sometime, you’ll an output as follows.
Press “TAB” and then “ENTER”.
Then you’ll obtain a screen as follows.
The default option is Internet Site. Keep it as it is and in order to click “Ok”; Press “TAB” and then “ENTER”.
After that you’ll get another window asking for System mail name. Provide the domain name from which you are willing to send emails; in this tutorial, I am using zeedr.cf. Finally press “TAB” and then “ENTER”.
Note: It is essential to have a properly configured MX record for your domain. An example MX record is as follows.
As in the figure, there should be an A record to specify the server which is responsible for sending emails with the corresponding MX record.
Open main Postfix configuration file using your favourite text editor:
sudo nano /etc/postfix/main.cf
Go down till you see the following section,
. . . mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all inet_protocols = all . . .
Change the lines:
- < inet_interfaces = all > to < inet_interfaces = loopback-only >
- < inet_protocols = all > to <inet_protocols = ipv4 >
And mydestination directive as follows:
mydestination = $myhostname, zeedr.cf, localhost.zeedr.cf, , localhost
- Remember to change zeedr.cf with your domain name.
- If you’re hosting multiple domains on a single server, the other domains can also be passed to Postfix using the mydestination directive.
Finally those lines should look as follows.
Then save and close the file.
Finally, restart Postfix for changes to take effect.
sudo service postfix restart
Testing Postfix SMTP Server
Now I am going to send an email to my Gmail address using the mail command provided by mailutils package.
Issue following command :
echo "This is the body of the email" | mail -s "This is the subject line" your_email_address
If you didn’t receive your email, even as a spam, then probably it’s better to contact your VPS provider and asking them to unblock default SMTP port (Pot 25) because majority of server providers like Vultr and DigitalOcean etc. block the port 25 to prevent possible mail spamming by using their servers.
As in the above image, the mail send from firstname.lastname@example.org using my server is treated as spam by Gmail and it may be a problem in your case too. There are several reasons which cause such problem, and I have provided tutorials to give solutions for some of resolvable problems. I hope that those tutorials will help you to make your server-send-emails being not flagged as spam by other email clients.
- The IP address of sending mail server is blacklisted.
You can check whether your server IP address is blacklisted by using this tool: Click here.
- The server doesn’t have a FQDN (Fully Qualified Domain Name) and a PRT record (tutorial).
- The SPF record of the domain you are using to send emails is missing or not properly configured.
Setting up a SPF record for your domain is discussed in my previous tutorial. “Configure SPF Record for your Domain”.
- DomainKeys Identified Mail (DKIM) is not available or not properly configured for relevant domains that used to send emails. Setting up DKIM is discussed in my tutorial “Install and Configure DKIM for your Domains“.