Setting up Postfix on a Ubuntu VM for sending emails
Postfix is an open-source Mail Transfer Agent for Linux systems.
Ubuntu VM Settings
First of all we update your local
sudo apt update
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
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
/etc/hostname contains your domain you want to mail from.
Also update your
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
# 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.
For example IP we use: 126.96.36.199
DNS Type: Hostname - Value
v=spf1 a mx ip4:188.8.131.52 ~all
!! 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 🥳
If you have any questions, I'm @WardPoel on Twitter.