Setting Up Your Mail Server DNS Records

Content:

In order for mail to find your mail server, you’ll need to ensure the correct DNS records are added for your domain.

This is similar to the process required when setting up a web domain, only in this case, there’s an extra record required specifically for emails.

Choosing an Email Domain

The first thing we need to decide is whether to use a unique subdomain for mails. It is common practice to do this, but not strictly necessary – the main reason for doing so would be to direct mail to a server running on a separate IP address to the base domain.

An example of this would be hosting a website from your home network, but using AWS to host your email. Another reason for doing this would simply be personal choice, and to ease splitting the mail to another server at a later date.

A common configuration choice is to use the mail subdomain. As an example, the domain example.com would use mail.example.com as the email domain.

This full domain is also known as the Fully Qualified Domain Name (FQDN), and will be referred to as such later on. If you don’t use a subdomain, the FQDN will be example.com.

Finding Your Server IP Address

DNS records require the IP address of your server. An easy way to find your IP address is using the command

curl -4 https://ifconfig.co

on your mail server.

This will return your IPv4 address, IPv4 being the standard way of connecting to the internet.

You can also check whether your server is accessible through IPv6.

curl -6 https://ifconfig.co

If an address is returned, your device supports IPv6.

Make a note of these address(es), and the IP version they correspond to. They’ll be needed later on when adding the DNS records.

Adding DNS Records

Now we need to start adding DNS records to the domain. DNS, or Domain Name Server records are used by network devices to convert the human-readable web address into an IP address. The IP address is then used to connect to the target server.

Logging in to your domain provider, you should see an option to edit your domain DNS settings. These records will always be attached to your parent domain – so in this case, example.com. This is true regardless of whether you intend to use a mail subdomain or not.

It’s worth noting at this stage that each domain provider will have a slightly different setup for doing this. As such, you may need to double check your providers instructions if you are unsure.

You’ll need the server IP address we found earlier. This example will use 123.45.67.89 as the IP address for example.com.

Adding an A Record

A records are the most common form of DNS record. Create an A record, if one does not already exist, for your FQDN.

The command for doing this will look something like:

mail.example.com 3600 IN A 123.45.67.89

My hosting provider does not require entering this directly, instead asking for the host name (just mail, not mail.example.com), and the IP address. I can also set the TTL (Time To Live) value, which is the 3600 in the above command, but I would leave this as is.

It also shows a preview of the command that will run, to double check it looks like the above. When you’re happy, create the record.

Adding an AAAA Record

If your server has an IPv6 address, you’ll also want to add an AAAA record. This will allow connections to the server using IPv6. If you don’t have an IPv6 address, you can skip this section.

This example will be using abcd:123:456:7890::1 as the IPv6 address.

mail.example.com 3600 IN AAAA abcd:123:456:7890::1

It’s very similar to the command used to add the A record.

Testing the DNS Records

With these base records created, we should now be able to test out our domain record. We’ll be doing this using https://mxtoolbox.com/SuperTool.aspx.

Click the drop down icon next to the orange box, and select ‘DNS Lookup’. Enter your FQDN in the box, and run the tool.

DNS record result
Simulated result of a DNS lookup for mail.example.com.

You should see something similar to the output above.

In this case, the simulated domain I have used does not have an IPv6 AAAA record. As you can see, the A record has been returned, and the tool is able to successfully find the IP address. It can take a while for DNS changes to propagate through the internet, so check back a few hours later if it returns an error.

With this done, we can now be sure that our mail domain can be accessed by other servers.

Adding MX Records

For email, there’s an extra record we’ll need to create. This is known as the Mail eXchange, or MX record. An MX record, is used to define the FQDN of the mail server responsible for receiving emails sent to a certain domain.

This will ensure that mail sent to user@example.com will be handled by the domain mail.example.com. Note that the email address uses the parent domain, not the FQDN, which is why this step is necessary.

It is possible to set up multiple servers to receive mail for a domain, which is transparent to the end user.

The MX record will use our parent domain name (example.com, sometimes denoted as @), and point it to the FQDN mail.example.com. There is also a priority value assigned to the record – for this example, it is set to 10. The priority is used when multiple MX records exist, to denote which (if any) should be the main target.

example.com 3600 IN MX 10 mail.example.com

This is what our completed entry looks like, which can now be added. Remember, if you’re not using a subdomain, replace mail.example.com with example.com – it will look slightly odd pointing a domain to itself, but that would be correct in that instance.

Testing the MX Record

We can now go back to MX Toolbox, and test out our MX record. Make sure the dropdown is set to ‘MX Lookup’. This time, you will need to run this against your parent domain, rather than the FQDN.

MX record result
Simulated MX lookup for example.com. Note that the MX record returned includes our mail subdomain.

You should see your mail domain returned, along with the priority value, and IP address. The DNS record should also be returned correctly.

You’ll notice the result above is showing DMARC errors – to learn more about DMARC, check out our article.