Sending e-mails with Postfix

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:

  1. Install Postfix and mailutils with the following commands:
    $ sudo apt-get update
    $ sudo apt-get install postfix mailutils -y
    
  2. 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>:
    How to do it…
  3. 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:
    How to do it…
  4. After installation completes, we need to modify the Postfix configuration under /etc/postfix/main.cf:
    $ sudo nano /etc/postfix/main.cf
    
  5. Set myhostname to point to your domain name:
    myhostname = mail.example.com
    
  6. 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
  7. Also check mydestination. It should contain your domain name:
    mydestination = example.com, ubuntu, localhost.localdoma
    in, localhost
    
    How to do it…
  8. Change the mail storage format to Maildir from the default mbox. Search and uncomment the following line:
    home_mailbox = Maildir/
    
  9. Optionally, you can change the TLS keys used by Postfix. Find the TLS parameters section and point the variables to your key path:
    How to do it…
  10. Save the configuration file and exit.
  11. 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:

    How to do it…
  12. 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.
  13. 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:
    How to do it…
  14. 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.

Tip

If you miss providing configuration during installation, you can always recall the same dialogue box with the reconfigure command as follows:

$ sudo dpkg-reconfigure postfix

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset