In this recipe, we will set up Postfix Mail Transfer Agent (MTA). This will be a very basic setup which will enable us to send and receive e-mails from our server. Postfix is an open source MTA which routes e-mails to their destination. It is a default MTA for Ubuntu and is available in Ubuntu's main package repository.
You will need access to a root account or an account with sudo
privileges.
A domain name (FQDN) is required while configuring Postfix. You can configure your local server for testing, but make sure that you set the proper host entries and hostname.
Follow these steps to send e-mails with Postfix:
mailutils
with the following commands:$ sudo apt-get update $ sudo apt-get install postfix mailutils -y
mail.example.com
, and answer the other questions. You can leave them with default values:/etc/postfix/main.cf
:$ sudo nano /etc/postfix/main.cf
myhostname
to point to your domain name:myhostname = mail.example.com
mynetworks
is set to the local network. This will secure your server from spammers:mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mydestination
. It should contain your domain name:mydestination = example.com, ubuntu, localhost.localdoma in, localhost
Maildir
from the default mbox
. Search and uncomment the following line:home_mailbox = Maildir/
TLS parameters
section and point the variables to your key path:$ sudo service postfix reload
Test if everything is working as expected. Open a telnet connection to the mail server:
$ telnet localhost 25
You should see an output similar to the following screenshot:
sendmail user@domain
and press Enter. Then, type your message, and when done with that press Ctrl + D to send an e-mail.mail
. This should show you a list of e-mails received by this user account. The output should look similar to following screenshot:q
followed by Enter to quit the mail reader.Postfix installation is quite a simple task; you need to be sure that you have configured the proper settings and then you are up and running in minutes. The Postfix installation process itself prompts for basic settings.
Other parameters include mynetworks
and mydestination
. With mynetwork
, we have restricted the uses of the mail server to the local network. Only users on the local network can use this server to send and receive e-mails. The parameter mydestination
specifies the domain names that Postfix is going to serve. For all other domains that are not listed under mydestination
, Postfix will simply act as a forwarder.
We have configured Postfix to use the Maildir
format for storing e-mails. This is a new storage format and provides various improvements over the default format, mbox
. Also, Maildir
is used by various IMAP and POP servers. With Maildir
, each new message is stored in a separate file. This avoids file locking when working with messages and provides protection against mailbox corruption.
Now if you send an e-mail to a local domain, it will be delivered to the inbox of the respective user, which can be read with mail
command. If you send e-mails to an external mail server, such as Gmail, chances are your mail gets delivered to spam. You need to include a number of different parameters in your e-mail headers and then make sure that your server IP is not blacklisted. It would be a good idea to use an external mail server such as Mail Chimp or Gmail for sending e-mails.