MX records are the postal workers of the internet, directing your email to the correct mail server.

Let’s watch a real email flow. Imagine bob@example.com sends an email to alice@yourdomain.com.

  1. Bob’s Mail Server Queries DNS: Bob’s mail server (let’s say mail.bob.com) needs to send the email to yourdomain.com. It asks a DNS resolver, "What are the MX records for yourdomain.com?"

  2. DNS Responds with MX Records: The DNS resolver looks up yourdomain.com and finds its MX records. These records are like a list of mail carriers and their priority. For yourdomain.com, it might look like this:

    yourdomain.com.   3600  IN  MX  10  mail1.yourdomain.com.
    yourdomain.com.   3600  IN  MX  20  mail2.yourdomain.com.
    

    Here, mail1.yourdomain.com is the primary mail server (priority 10) and mail2.yourdomain.com is a backup (priority 20).

  3. Bob’s Server Connects to the Primary: Bob’s mail server sees mail1.yourdomain.com has the lowest priority number (10), so it attempts to connect to mail1.yourdomain.com on port 25 (the standard SMTP port).

  4. Email Delivered: If mail1.yourdomain.com is available and accepts the connection, it receives the email from Bob’s server and stores it for Alice. If mail1.yourdomain.com is down, Bob’s server tries the next MX record, mail2.yourdomain.com.

The problem you’re solving is ensuring that when someone sends an email to an address at yourdomain.com, it reliably reaches your mail server. This involves configuring your DNS provider with the correct MX records.

Here’s how you’d typically configure them using dig to check and a common DNS provider’s interface as an example.

The Core Components:

  • Hostname: The domain name for which you’re setting MX records (e.g., yourdomain.com).
  • Record Type: MX (Mail Exchanger).
  • Priority: A number indicating the order of preference. Lower numbers are tried first.
  • Mail Server Hostname: The fully qualified domain name (FQDN) of the mail server that will receive mail for your domain. This must be an A or AAAA record.

Example Configuration:

Let’s say your mail servers are mx1.mailhost.net and mx2.mailhost.net.

  1. Configure Mail Server Hostnames: First, ensure mx1.mailhost.net and mx2.mailhost.net have A records pointing to their IP addresses.

    # For mx1.mailhost.net
    dig A mx1.mailhost.net +short
    # Should return an IP like 192.0.2.10
    
    # For mx2.mailhost.net
    dig A mx2.mailhost.net +short
    # Should return an IP like 192.0.2.11
    

    If they don’t exist, you’d add them in your DNS zone file or provider’s interface:

    mx1.mailhost.net.   3600  IN  A   192.0.2.10
    mx2.mailhost.net.   3600  IN  A   192.0.2.11
    
  2. Configure MX Records: Now, you set the MX records for yourdomain.com.

    • Primary Server: mx1.mailhost.net with priority 10.
    • Secondary Server: mx2.mailhost.net with priority 20.

    In a typical DNS zone file format, this looks like:

    yourdomain.com.   3600  IN  MX  10  mx1.mailhost.net.
    yourdomain.com.   3600  IN  MX  20  mx2.mailhost.net.
    
    • 3600 is the TTL (Time To Live) in seconds, meaning DNS resolvers will cache this record for 1 hour.
    • IN means "Internet".
    • MX is the record type.
    • 10 and 20 are the priorities.
    • mx1.mailhost.net. and mx2.mailhost.net. are the hostnames of your mail servers. Crucially, they must end with a dot (.) to signify they are FQDNs.
  3. Verification: After saving these changes with your DNS provider (e.g., GoDaddy, Namecheap, Cloudflare, or your own BIND/PowerDNS server), you can verify them:

    dig MX yourdomain.com +short
    

    The output should show:

    10 mx1.mailhost.net.
    20 mx2.mailhost.net.
    

    The order might not be guaranteed in the output of dig, but the priorities will be correct.

What Most People Don’t Know:

The mail server hostname specified in an MX record must resolve to an A or AAAA record. You cannot have an MX record pointing to a CNAME record. If you try to point your MX record to mail.example.com and mail.example.com is a CNAME for servers.cdn.com, mail delivery will fail because the sending mail server cannot resolve the ultimate IP address for the mail exchange. This is a common mistake when using services that provide CNAMEs for their mail servers.

The next thing you’ll likely encounter is troubleshooting why mail is being rejected by your mail server, which often involves checking your mail server’s firewall rules, spam filters, or its own DNS resolution.

Want structured learning?

Take the full Dns course →