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.
-
Bob’s Mail Server Queries DNS: Bob’s mail server (let’s say
mail.bob.com) needs to send the email toyourdomain.com. It asks a DNS resolver, "What are the MX records foryourdomain.com?" -
DNS Responds with MX Records: The DNS resolver looks up
yourdomain.comand finds its MX records. These records are like a list of mail carriers and their priority. Foryourdomain.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.comis the primary mail server (priority 10) andmail2.yourdomain.comis a backup (priority 20). -
Bob’s Server Connects to the Primary: Bob’s mail server sees
mail1.yourdomain.comhas the lowest priority number (10), so it attempts to connect tomail1.yourdomain.comon port 25 (the standard SMTP port). -
Email Delivered: If
mail1.yourdomain.comis available and accepts the connection, it receives the email from Bob’s server and stores it for Alice. Ifmail1.yourdomain.comis 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.
-
Configure Mail Server Hostnames: First, ensure
mx1.mailhost.netandmx2.mailhost.nethave 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.11If 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 -
Configure MX Records: Now, you set the MX records for
yourdomain.com.- Primary Server:
mx1.mailhost.netwith priority10. - Secondary Server:
mx2.mailhost.netwith priority20.
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.3600is the TTL (Time To Live) in seconds, meaning DNS resolvers will cache this record for 1 hour.INmeans "Internet".MXis the record type.10and20are the priorities.mx1.mailhost.net.andmx2.mailhost.net.are the hostnames of your mail servers. Crucially, they must end with a dot (.) to signify they are FQDNs.
- Primary Server:
-
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 +shortThe 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.