cribbble

Setting up Postfix on a Ubuntu VM for sending emails

avatar
Ward Poel

Postfix is an open-source Mail Transfer Agent for Linux systems.

Ubuntu VM Settings

Installing Postfix

First of all we update your local apt cache:

sudo apt update

Then install postfix with the following command:

sudo apt install postfix

This installation will open an interactive prompt. The first prompt will ask what type of mail configuration you want. Select Internet Site and continue.

In the second prompt you will be asked to give your system mail name. In this tutorial we will use the domain test.com. So fill in test.com as system mail name.

Note: If you ever need to return to this interactive prompt windows. You can use the following command:

sudo dpkg-reconfigure postfix

Changing the Postfix configurations

We need to tell Postfix to use the correct hostname and destination. This can be changed in the file /etc/postfix/main.cf.

sudo nano /etc/postfix/main.cf

Change the fields myhostname and mydestination:

myhostname = test.com

mydestination = $myhostname, test.com, localhost.localdomain, localhost

Change your Ubuntu VM hostname

Make sure /etc/hostname contains your domain you want to mail from.

test.com

Also update your /etc/hosts file:

127.0.1.1 ubuntu-vm ubuntu-vm
127.0.0.1 localhost ubuntu-vm test.com

ubuntu-vm is the name of the machine, you don't have to change that. Just make sure you add your domain.

Update the hostnamectl with the following command:

hostnamectl set-hostname test.com

Setting up DomainKeys Identified Mail (DKIM)

DKIM is an email authentication method designed to detect email spoofing.

The first step is to generate a public and private key. Make sure you generate these keys with openssl.

# Generate private key
openssl genrsa -out private.key 1024

# Generate public key based on private key
openssl rsa -in private.key -pubout -out public.key

You will need this public key for your DNS records.

Please keep in mind that using DKIM does not encrypt the content of your mail.

DNS Settings

For example IP we use: 100.10.10.100

DNS Type: Hostname - Value

A: mail.test.com - 100.10.10.100

MX: test.com - mail.test.com

TXT: test.com - v=spf1 a mx ip4:100.10.10.100 ~all

TXT: default._domainkey.test.com - v=DKIM1;k=rsa;p=DKIM_PUBLIC_KEY

!! Make sure you setup reverse DNS (rDNS) for you IP. rDNS is a protocal used to translate the sending server IP address into its host name (test.com). This is needed so the receiving mail server can check if you are sending mails from a server that is connected to that IP address. If you need to do this with DNS, you need to setup a PTR record. !!

When using a DigitalOcean droplet, just change the name of your droplet to your domain. In this example test.com. You can change the name of a droplet by just clicking on the droplet name.

Please keep in mind that DNS changes may take some time

That's it! Your server and domain are now configured for sending mails 🥳


Interesting posts:

If you have any questions, I'm @WardPoel on Twitter.