Postfix is a Mail Transfer Agent which is used to send and receive mail on a server.
Here, we are going to install Postfix on a local server. The applications installed on this server only can use the Postfix to send the email.
The reason to do this:
You might be using the third party email service for sending and receiving mail.
If you decided to use your own mail server, the Postfix could be the best option to use it.
For instance, if you have the server with a lot of applications and most of them need to send email, then you can install Postfix on the server to be accessed locally.
It is better to use the Postfix MTA locally as an alternative to the third party email server or a full SMTP server.
Here in this tutorial, you will learn how to install send only SMTP server on Ubuntu 14.04 and Ubuntu 16.04.
- Ubuntu 14.04 server configured according to the initial server setup guide.
- You should have the domain name pointing to your server.
- Your hostname should match with the domain name. You can check the hostname using hostname command in the terminal.
To install the Postfix, you need to install the mailutils. Follow this command for that.
sudo apt-get install mailutils
When you install mailutils, Postfix and other dependencies will also be installed on your machine.
And you will get the following output.
The following NEW packages will be installed: guile-2.0-libs libgsasl7 libkyotocabinet16 libltdl7 liblzo2-2 libmailutils4 libmysqlclient18 libntlm0 libunistring0 mailutils mailutils-common mysql-common postfix ssl-cert 0 upgraded, 14 newly installed, 0 to remove and 3 not upgraded. Need to get 5,481 kB of archives. After this operation, 26.9 MB of additional disk space will be used. Do you want to continue? [Y/n]
Press Y and Enter to proceed with additional steps. At the last point of the installation, you will be getting the following screen.
Where you have to choose the internet site. Use tab to choose that option.
And then press enter.
Then a next screen will appear. In that, you have to set the email name. The mail name should be same as the server name.
If not, change it and press TAB and then press Enter.
Once you are done with the installation process, then we can proceed to next step.
You have to configure the Postfix to send mail from applications which are hosted on the same server.
For that, the Postfix has to listen on Loop Back interface which is the virtual network interface for applications to communicate internally.
To do that, open the postfix configuration file using the nano editor.
sudo nano /etc/postfix/main.cf
Look for the below lines in the configuration file.
mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all
You have to make changes in the inet_interfaces = all line. Make changes as mentioned below.
mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = loopback-only
You can also use localhost instead of loopback-only. And the line may look like below.
mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = localhost
After that, save and close the file. Then restart the Postfix server.
sudo service postfix restart
Test the SMTP server
In this step, you are going to test the Postfix SMTP server.
We will try to send a mail to the external mail from the server. Use the below command for that.
echo "This is the body of the email" | mail -s "This is the subject line" firstname.lastname@example.org
You can change the body and subject line as per your choice.
Instead of email@example.com, just use any mail ids such as gmail.com, yahoo.com and some other mail.
Make sure that is a valid email address.
Go to the email address which you have sent the email. If there is no message in the inbox, check the spam folder.
Forward System Mail
You have to set up the forwarding in the root server, then only you will receive the mail at your personal mail.
By default, the email will be sent to the root user. You can configure the Postfix to send the mail to the specific email id.
For that, you have to edit the /etc/aliases file.
sudo nano /etc/aliases
In that file, you can see the following lines on Ubuntu 16.04.
# See man 5 aliases for format postmaster: root
You have to set your email id to the root so it will be forwarded to that email.
# See man 5 aliases for format postmaster: root root: firstname.lastname@example.org
After that, save and close the file. To make the changes to work, execute the below command.
Now, again test the Postfix server using the below command.
echo "This is the body of the email" | mail -s "This is the subject line" root
After that, you will receive an email at the specified address.
We have seen the configuration of Postfix for Ubuntu 14.04. Now let us see the configuration for Ubuntu 16.04.
- You should have configured Ubuntu 16.04 server according to the initial server setup guide.
- A domain name is pointing to your server.
The server hostname should match with the domain name. Verifying the hostname is easy and you only need to use the 'hostname' command.
To install the Postfix, Install the mailutils. First, update the package index.
$ sudo apt-get update.
After that, install mailutils using the following command.
$ sudo apt install mailutils
At the end of the installation process, you will see a window like this. In that select option internet site and then press TAB.
There after, an another window will open in that you have to set the email name as same as server name which you set during the configuration.
If it shows subdomain.mydomain.com, change it to mydomain.com.
Then press TAB and enter.
After that, go to the next step.
Here, I am going to show you how to configure Postfix to only send the email.
You have to configure the postfix to listen on the loopback interface. It is virtual network face used by the server to communicate internally.
Open the postfix configuration using nano editor.
$ sudo nano /etc/postfix/main.cf
Find the following section in the file.
/etc/postfix/main.cf . . . mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all . . .
Now you have to change the inet_interfaces=all to inet_interfaces = loopback-only.
/etc/postfix/main.cf . . . mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = loopback-only . . .
You have to change the one more directive which is mydestination. There a lot of domains are specified and they are used to deliver by local_transport.
It will look like below.
/etc/postfix/main.cf . . . mydestination = $myhostname, example.com, localhost.com, , localhost . . .
Here is modified file. You also have to modify according to that.
/etc/postfix/main.cf . . . mydestination = $myhostname, localhost.$mydomain, $mydomain . . .
After that, save and close the file.
The mydestination directive lets you use postfix for multiple domains hosted on a single server.
Then, restart the postfix.
$ sudo systemctl restart postfix
Testing the SMTP server
Now, you are going to test whether the Postfix is working or not.
We will send a test email with subject line and body. Here is the command for that.
$ echo "This is the body of the email" | mail -s "This is the subject line" your_email_id
Here, you can change the Subject line and the body of the email as per your choice.
In the email id section, you can give any email id such as gmail.com, yahoo.com and any other email service provider.
After sending the email, check the email address which you have sent the email.
You can see the messages in the inbox or check it in the spam folder.
The email address for sending emails will be email@example.com.
Here the user will be referring to the Linux user. If you change the user, the email address will also be changed.
Forwarding System Email
When you send the email, those emails will be sent to the root.
In order to send it to the specific email, you have to configure the postfix for it.
To do that, you have to edit the /etc/aliases file.
$ sudo nano /etc/aliases
The file will look like below. There you have to add an additional line.
/etc/aliases # See man 5 aliases for format postmaster: root
There the postmaster would be set to root. After that line set the specific mail id for the root to forward.
/etc/aliases # See man 5 aliases for format postmaster: root root: your_email_id
Now, Replace the your_email_id with your personal email id. Save and close the file.
Execute the below command, to make the changes to take effect.
$ sudo newaliases
You can again check the email by testing it.
$ echo "This is the body of the email" | mail -s "This is the subject line" root
You will receive the email in your inbox or else check it in the spam folder.
Now you have learned how to install and configure Postfix as a send only SMTP server on Ubuntu 16.04 and Ubuntu 14.04.
Also, you have learned how to configure the Postfix to only send the email.
If you have any doubt regarding installing postfix, just let us know that in the comment.
We will help you to solve your issue. Also, don't forget to sign up for the upcoming tutorials.