24/7/365 Support

Sending e-mails with Postfix in Ubuntu

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.

Getting ready

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.

How to do it…

Follow these steps to send e-mails with Postfix:

Install Postfix and mailutils with the following commands:

$ sudo apt-get update

$ sudo apt-get install postfix mailutils -y

The installation process will prompt you to enter some basic configuration details. When asked for General type of mail configuration:, select Internet Site and then click on <Ok>:

On the next screen, enter your domain name, for example, mail.example.com, and answer the other questions. You can leave them with default values:

After installation completes, we need to modify the Postfix configuration under /etc/postfix/main.cf:

$ sudo nano /etc/postfix/main.cf

Set myhostname to point to your domain name:

myhostname = mail.example.com

Ensure 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

Also check mydestination. It should contain your domain name:

mydestination = example.com, ubuntu, localhost.localdomain, localhost

Change the mail storage format to Maildir from the default mbox. Search and uncomment the following line:

home_mailbox = Maildir/

Optionally, you can change the TLS keys used by Postfix. Find the TLS parameters section and point the variables to your key path:

Save the configuration file and exit.

Now, reload Postfix for the changes to take effect:

$ 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:

Now, send your first e-mail from this server. Type sendmail user@domain and press Enter. Then, type your message, and when done with that press Ctrl + D to send an e-mail.

To read your e-mails, log in with the user you send e-mails to. Start the mail program with the command mail. This should show you a list of e-mails received by this user account. The output should look similar to following screenshot:

To read any e-mail, type in the mail number and press Enter. Type q followed by Enter to quit the mail reader.

How it works…

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.

See also

An article by Jeff Atwood on sending e-mails through code. This may help you get your e-mails out of spam: http://blog.codinghorror.com/so-youd-like-to-send-some-email-through-code/

Mailbox formats: http://wiki.dovecot.org/MailboxFormat

The difference between port 465 and 587: http://stackoverflow.com/questions/15796530/what-is-the-difference-between-ports-465-and-587

Help Category:

What Our Clients Say