Learning objectives
After reading this article you will be able to:
- Understand what MX, SPF, DKIM and DMARC do and where each lives in DNS
- Create the TXT records within the one-SPF and 10-lookup limits
- Test the setup with dig and with a message's headers
You created hello@mysite.com with an email provider and your messages land in spam, or never arrive. A common cause is DNS missing the records that prove whoever sends as the domain is allowed to, proof that Gmail, Outlook and the other large providers check on every message.
There are four records, and they all live in the domain's DNS, wherever it is hosted (at the registrar or at a DNS provider). Your website can be somewhere else: email and website are separate services that share the same domain, as domain vs hosting explains.
What does each record do?#
| Record | Type and name | What it is for |
|---|---|---|
| MX | MX on mysite.com |
Says which servers receive the domain's email |
| SPF | TXT on mysite.com |
Lists the servers allowed to send for the domain |
| DKIM | TXT (sometimes CNAME) on selector._domainkey.mysite.com |
Publishes the public key that verifies message signatures |
| DMARC | TXT on _dmarc.mysite.com |
Sets the policy for failing messages and where reports go |
The last three work together. SPF checks the IP that delivered the message against the envelope domain (the MAIL FROM, shown as Return-Path), not against the address the reader sees in the From field. DKIM checks a signature made with the key of the domain in d=. DMARC requires at least one of the two to pass and to align with the From domain. Without that alignment, a scammer could pass SPF with their own domain and put yours in From.
If record types are new to you, read DNS records: A, CNAME, MX and TXT. Have at hand:
- Access to the domain's DNS.
dig +short NS mysite.comshows who hosts the records; that company's DNS settings are where you will work. - The list of everything that sends as your domain: the mailbox, your application's transactional service, the newsletter tool, the CRM. Each one goes into SPF and signs with DKIM.
- Each provider's values, from its documentation. The examples use made-up names in
.example.
Step 1: how do you set up MX?#
MX says where messages sent to @mysite.com go. Each record has a priority, and the sender tries the lowest number first:
mysite.com. 3600 IN MX 10 mx1.email-provider.example.
mysite.com. 3600 IN MX 20 mx2.email-provider.example.Two rules. An MX target must be a name with an A or AAAA record, never a CNAME (RFC 2181, section 10.3). And when you switch providers, delete the old MX records, or part of your mail keeps going there.
Step 2: how do you create the SPF record?#
SPF is a TXT record on the domain name that starts with v=spf1 and lists who may send. With a Google Workspace mailbox and a transactional service, it looks like this:
mysite.com. 3600 IN TXT "v=spf1 include:_spf.google.com include:spf.sending-service.example ~all"include: pulls in the IP list the provider maintains, and the all at the end says what to do with everything else. The rules from RFC 7208 that break the most setups:
- One SPF record per name. Two TXT records starting with
v=spf1on the same name givepermerror, and SPF fails. When you add a service, edit the existing record. Other TXT records on the same name, such as domain verifications, don't interfere: the limit applies only to those starting withv=spf1. - At most 10 DNS lookups. Every
include,a,mx,ptr,existsandredirectcounts, including the ones nested inside providers' includes. Go past 10 and you getpermerror. More than two "void" lookups (a name that doesn't exist or has no answer) also break the record. - No
ptr. The RFC says it should not be published, since it is slow and unreliable. - The
allqualifier.~all(softfail) marks mail as suspicious,-all(fail) says it is not authorized and?all(neutral) protects nothing. Start with~alland move to-allonce the list is complete.
SPF applies to the exact name. If your app sends from alerts.mysite.com, that name needs its own record. A domain that sends no email at all can publish v=spf1 -all, which says "nobody sends for me".
Step 3: how do you publish the DKIM key?#
The email provider generates a key pair: it keeps the private key to sign messages and shows you the public key to publish in DNS, under a name made of a selector and _domainkey:
s1._domainkey.mysite.com. 3600 IN TXT "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA..."The p= value is cut short here; copy whatever the provider shows, in full. A TXT record holds strings of up to 255 characters, so longer keys go in several quoted strings in the same record (most DNS interfaces split them for you). Some providers ask for a CNAME instead of a TXT, pointing to a key they publish and rotate: Microsoft 365 uses selector1._domainkey and selector2._domainkey this way.
After publishing, turn signing on at the provider: otherwise the key sits in DNS and no message goes out signed. Use 2048-bit RSA keys: RFC 8301 requires at least 1024 and recommends 2048. The selector exists so you can have more than one key at a time: to rotate, publish the new key under another selector, start signing with it and only then delete the old one.
Step 4: how do you roll out DMARC in stages?#
DMARC is a TXT record at _dmarc that sets the policy and asks for reports. The current specification is RFC 9989, which replaced RFC 7489, and aggregate reports are defined in RFC 9990. Start by only watching:
_dmarc.mysite.com. 3600 IN TXT "v=DMARC1; p=none; rua=mailto:dmarc@mysite.com"With p=none, delivery doesn't change, but the providers receiving your mail can send aggregate XML reports to the rua address: which IPs sent as your domain and whether they passed SPF, DKIM and alignment. Then move forward:
p=nonewhile you read the reports. Fix every legitimate sender that fails, usually a service left out of SPF or not signing DKIM with your domain.p=quarantine: you ask receivers to treat failing messages as suspicious, which usually sends them to spam.p=reject: you ask receivers to refuse them. It's the policy that best protects against spoofing, but the receiver decides: under RFC 9989, a provider must not reject onp=rejectalone and may apply quarantine.
sp= sets a different policy for subdomains, and adkim and aspf switch alignment from relaxed (the default, which accepts subdomains of the same domain) to strict (s, exact name). The pct tag you'll see in older tutorials is no longer part of the current specification. If the rua address belongs to another domain, such as a report analysis service, that domain must authorize it with a v=DMARC1 TXT record at mysite.com._report._dmarc.service-domain.example.
For bulk senders, DMARC is not optional: Google requires at least SPF or DKIM from every sender, and SPF, DKIM and DMARC (even at p=none) from anyone sending more than 5,000 messages a day to Gmail accounts, with an aligned From domain.
How do you test that it works?#
Query each record with dig (on Windows, nslookup -type=TXT name does the same):
dig +short MX mysite.com
dig +short TXT mysite.com
dig +short TXT s1._domainkey.mysite.com
dig +short TXT _dmarc.mysite.comEach command should return the value you created, and the second must not show two lines with v=spf1. If the old value still shows up, it's cache: ask one of the domain's DNS servers directly (dig +short TXT mysite.com @ns1.your-dns.example) and see DNS propagation.
Then send a message from each service to a Gmail account and open "Show original". The summary at the top shows the SPF, DKIM and DMARC results, and the Authentication-Results header has the detail, something like:
Authentication-Results: mx.google.com;
dkim=pass header.i=@mysite.com header.s=s1;
spf=pass smtp.mailfrom=mysite.com;
dmarc=pass (p=NONE) header.from=mysite.comThree pass results, with your domain in header.from, show that alignment works.
What are the most common mistakes?#
| Symptom | Likely cause | Fix |
|---|---|---|
SPF permerror |
Two v=spf1 records or more than 10 lookups |
Merge everything into one TXT and drop includes for services you no longer use |
DKIM fail or no key found |
Wrong selector, truncated key or doubled name (s1._domainkey.mysite.com.mysite.com) |
Copy the whole key and, where the interface appends the domain for you, type only s1._domainkey |
| DMARC fails while SPF passes | The service sends with its own domain in the envelope, so nothing aligns | Turn on DKIM with your domain in that service or set up your own envelope domain |
| Email stopped after a website change | A CNAME created on a name that already had MX or TXT | A CNAME can't share its name with any other record: give the site a name of its own (A record vs CNAME) |
| The credential your application uses with the sending provider is a secret like any other (where to store secrets). And if the domain also serves a website, protection on the web side comes from HTTP security headers. |
How to do this on Square Cloud#
Square Cloud doesn't host email. The mailbox lives with an email provider, and the MX, SPF, DKIM and DMARC records go to your registrar or the DNS provider you already use, under your control. The domain stays registered in your name.
To use the domain for a website, from the Standard plan, you create the records the dashboard shows in that same place: a CNAME to cname.squareweb.app on the site's name (for example, app.mysite.com) and a validation TXT record. They coexist with your email records: the CNAME sits on the site's name, while MX, SPF, DKIM and DMARC stay on theirs. The dashboard shows the validation status in real time, and the certificate is issued and renewed on its own.
If your application sends email through a sending provider, that provider is what goes into SPF and DKIM, and its credential can live in the application's environment variables.
See the domain walkthrough in custom domains.
Network
Custom domains, automatic TLS and a global CDN for your app
FAQs
Can I have two SPF records on the same domain?
No. RFC 7208 treats two v=spf1 records on the same name as a permanent error (permerror), and SPF fails. Merge every sender into a single TXT record, with one include per service.
What is the difference between ~all and -all in SPF?
~all (softfail) says a server outside the list is probably not authorized; -all (fail) says it is not. Start with ~all while you check your senders and move to -all once the list is complete.
Do I need DMARC if I already have SPF and DKIM?
Yes. Only DMARC ties SPF and DKIM to the address shown in the From field and tells receivers what to do with failures. Gmail requires DMARC from anyone sending more than 5,000 messages a day to its accounts, even at p=none.
How long do the records take to work?
It depends on the TTL and on resolver caches: a changed record can take up to the old TTL to reach everyone. Query your domain's DNS server directly with dig to check whether the new value is already published.
Why does forwarded email fail SPF?
The forwarder resends the message from its own server, which isn't in your SPF record. DKIM usually survives forwarding when the message isn't modified, which is why you should set up both.
More in Networking and domains
See all 31 articles- What is a load balancer and how does it work?A load balancer spreads incoming requests across several servers. Learn how it works, the main algorithms, layer 4 vs 7, and a working nginx example.
- DNS records explained: A, CNAME, MX, TXTDNS records are the instructions in a zone that tell the internet where a name points. Learn A, AAAA, CNAME, MX, TXT, NS and CAA with examples and dig commands.
- How to point a domain to your server or appTo point a domain to your server or app, create a CNAME (or A) record in your registrar's DNS panel. Steps, how to test it, and common mistakes.
- ERR_TOO_MANY_REDIRECTS: redirect loops explainedERR_TOO_MANY_REDIRECTS is a redirect loop, most often an app forcing HTTPS behind a proxy. Learn how to trace the chain with curl and fix each cause.