13. Email and Your Domain
“Anyone can write your name on an envelope. The seal is the part that is hard to fake.”
The story: the receipt that went to spam
Section titled “The story: the receipt that went to spam”On a Monday morning, fourteen parents at a dance academy in Pune wrote to the front desk with the same question. Where is my payment receipt?
The receipts had been sent. That was the maddening part. A Razorpay webhook confirmed each payment, the app wrote a row to Postgres, and a call to Resend produced an email with a PDF attached. Every log line said the same thing: accepted, 200 OK. Yet a surprising number of parents never saw the email in their inbox. Some found it in spam. One found it under a yellow banner that said this message may be suspicious.
Priya’s mother was one of them. She pays the fees from a ₹12,000 phone on a Pune tower, and she forwarded the banner with three question marks. It is the least useful screenshot you will ever see and the most useful clue you will get all week.
This is a composite, stitched together from the kind of Monday that small apps have. The academy is invented. The banner is not.
Your first instinct is to check the code. The template renders, the recipient address is right, the API call returns success. You read the logs again. Still 200 OK.
Here is what this chapter is about. A 200 OK from Resend means Resend accepted your message. It does not mean anyone’s inbox accepted it. Acceptance happens later, in a long conversation between servers you will never see, and every server in that conversation asks the same question: who is really sending this? Your app cannot answer that on its own. The answer lives in DNS, the directory you met in Chapter 10, in a few TXT and MX records that most people copy from a tutorial and never read.
By the end of this chapter you will be able to read those records. You will know what each one proves, why receivers care, and why SPF, DKIM and DMARC are really one idea told three ways.
Why your app’s mail lands in spam
Section titled “Why your app’s mail lands in spam”Your app did not send that receipt. Resend did, on your behalf. The 200 OK in your logs means Resend has your letter and will try to deliver it. It is a receipt for the first hand the letter passed through, and it says nothing about the parents’ inboxes.
From there the message takes a trip you never see. Resend connects to the receiver’s mail server, the machine that handles mail for @gmail.com or for a company’s domain, and says, in effect, “I have a message for this mailbox, from this sender.” The receiver then asks questions. Did this server have permission to send for that domain? Was the message signed by someone who controls that domain? Has this sender caused trouble before? Based on the answers, the receiver makes one of three decisions: deliver to the inbox, deliver to the spam folder, or refuse.
Look at what the receiver is judging. It is not your template, which may be perfect, and it is not your intent. It is evidence about the sender, gathered from the connection and from DNS. A correct email from a machine with no history looks exactly like spam, and that is the whole problem in one sentence.
Here is the chain you have carried since Chapter 3: Name → Address → Route → Door → Guard → Receptionist → App. Mail trouble lives mostly at the Door and the Guard. The Door is the port your app would have to use to talk to another mail server, and most clouds have closed it. The Guard is the set of checks the receiving server runs before it lets your message in. The rest of this chapter walks through both, and then through the paperwork that ties them together.
Why not just send it from the VPS?
Section titled “Why not just send it from the VPS?”You have a VPS with a public IP, a root shell, and a tutorial that will happily walk you through installing a mail server in an afternoon. It is tempting. The afternoon is the easy part. Here is why I would not do it for your app, in the order the receiving server judges you.
The door. Mail moves between servers on port 25, the SMTP port from Chapter 3. Most clouds restrict outbound port 25 on new accounts, because rented machines were the favourite place for spam to be born. As far as I know, Oracle’s defaults are no different, so check your provider’s current policy before you plan around anything. You can ask for the block to be lifted, and you may get it. Either way, you have signed up to run mail infrastructure.
The reputation. Suppose the door is open. The receiving server now decides whether to believe you using the IP address your connection came from. A fresh IP has no history, and no history looks suspicious. The address may also belong to a machine that someone else used for something unpleasant last spring, and reputation lists are not sentimental about who holds an address today. Your booking confirmations would travel in the same carriage as the spam blasted from that address before you got it.
The reverse name. When a receiving server accepts a connection from 203.0.113.10, it often looks up the name behind that address, the PTR record you met in Chapter 10. If that name is a generic hostname from your cloud provider, or it does not match the name your mail server introduces itself with, you look like a machine that was never meant to send mail. You set a PTR record in your cloud provider’s console, not in your DNS zone, and the name it points to should resolve forward to the same address.
The upkeep. None of these is hard on its own. Together they are a job that never ends. Queues need watching, blocklists need checking, bounces need handling, and abuse reports arrive at two in the morning on the nights you were hoping to sleep. A mail server gets better or worse while you are not looking at it.
So the rule for a small production VPS is short. The VPS sends no mail itself. Your app hands each message to a transactional service over HTTPS, on port 443, the one door every network already has open. That service carries the reputation, the warmed-up IPs, the bounce handling, and the people who answer when a large provider changes its mind. You keep the content, and the paperwork that proves the mail is really from you. That paperwork is the rest of this chapter.
SPF and DKIM: the postman list and the wax seal
Section titled “SPF and DKIM: the postman list and the wax seal”Picture a letter sealed in wax and handed over by a postman. The receiver can ask two questions about it. Did this postman work for the post office the sender uses? And does the seal match the ring the sender owns? Those are SPF and DKIM. Each one proves a different thing, and neither proves what you might hope on its own.
SPF is the postman list. The domain owner publishes a TXT record in DNS that starts with v=spf1 and names the servers allowed to send mail for the domain. The list can point at an IP range, at the mail records of a host, or at a provider’s own list with an include. The record ends with a verdict for everyone else. A tilde (~all) says “not on the list, but accept it and mark it suspicious.” A hyphen (-all) says “not on the list, refuse.” Use the tilde while you are still finding out who sends mail for you, and the hyphen once the list is complete.
The check happens at the door. The receiving server looks at the IP address that actually connected, and at the envelope sender, called the Return-Path, which is where bounces go. That is not the From address the reader sees in the inbox. Keep that gap in mind, because it is the gap spoofers use.
SPF has two well-known limits. First, every include and redirect costs a DNS lookup, and the receiver stops counting at ten. Go over and the result is an error, which most receivers treat as a fail. A busy sender that stacks four providers can get there quietly, and the only sign is mail that starts landing in spam one Tuesday. Second, forwarding breaks it. When a message is forwarded, the IP that connects is the forwarder’s, not the original sender’s, so SPF fails even though the letter was honest.
DKIM is the wax seal. Your sending service holds a private key, a signing ring only it can use. For each message it hashes the headers and body, signs that hash, and adds a DKIM-Signature header. The public half of the key is published in DNS under a name like selector._domainkey followed by your domain, where the selector is a label the provider chose. The receiver fetches that public key, checks the seal, and learns two things. The domain owner published the key, and the signed parts have not changed since the signing.
The seal travels with the letter, which is why DKIM survives forwarding better than SPF does, though not perfectly. A mailing list that rewrites the subject or adds a footer changes the signed body, and the seal breaks. Two practical notes. Selectors let you rotate keys without downtime, and they let two providers sign for the same domain. And the private key belongs in your provider’s dashboard, never in your app’s repository, where it would travel with every clone.
Notice what none of the three checks does. None of them looks at the words in the letter, and none of them stops a spoofer by itself. A message can pass SPF and DKIM perfectly for its own domain while claiming to be from someone else. That is the job of the third record, and it is the one most people forget to publish.
DMARC: the instruction sheet
Section titled “DMARC: the instruction sheet”SPF and DKIM are checks. Each one says “this message passed a test” and nothing more. Neither tells the receiver what to do when the test fails, and neither says which result you actually care about. Two checks, two answers, and the receiver is left to guess. DMARC closes that gap.
DMARC lives in one TXT record, always at the name _dmarc on your domain. A starter record for the lab looks like this:
_dmarc.pipeslab.in. TXT "v=DMARC1; p=none; rua=mailto:dmarc@pipeslab.in"Read it the way a receiving server does. v=DMARC1 says this is a DMARC policy and nothing else. p=none is the instruction for mail that fails. rua is the address where receivers send you aggregate reports about who is sending mail as your domain.
The p= tag has three rungs. none means “monitor, do not act”: deliver as usual, but tell me what happened. quarantine means “put failing mail in spam”. reject means “refuse it at the door.” The sensible rollout starts at none. You read the reports for a few weeks, fix whatever legitimate sender you forgot (the newsletter tool, the invoicing app, the helpdesk), and only then climb to quarantine and eventually reject. Teams that skip the reading step and jump straight to reject are the ones who discover, on a Monday morning, that their own password reset emails have stopped arriving.
The reports are the underrated part. They are XML files, usually one per receiver per day, listing every server that sent mail claiming your domain, how many messages each one sent, and whether SPF and DKIM passed. Nobody reads them raw. A reporting tool, or a short script, turns them into a table, and that table is the first honest list you will ever get of everything that speaks for your domain. Most of it will be your own tools. Some of it will be strangers, and finding strangers is exactly why you published the record.
The 2024 rules, and why the big mailboxes got strict
Section titled “The 2024 rules, and why the big mailboxes got strict”In late 2023 Google and Yahoo announced new requirements for senders who push large volumes of mail into their users’ inboxes. They began enforcing them around February 2024. If memory serves, the line sits at roughly 5,000 messages a day to Gmail addresses. For those bulk senders the requirements were roughly these:
- SPF or DKIM passes, and the passing domain matches the visible From: address.
- A DMARC record exists, at least
p=none. - Marketing mail carries one-click unsubscribe.
- The spam complaint rate stays well below the 0.3 percent figure people quote.
Your academy is not sending 5,000 a day, so these rules do not formally bind you. The signals behind them, authentication and complaint rates, are what every receiver’s filter reads at every volume. Treat the list as a checklist for everything you send. It costs an afternoon, and that afternoon keeps your parents’ receipts out of spam.
Receive with a provider, send with a service
Section titled “Receive with a provider, send with a service”Here is the opinionated part. Do not send mail from your VPS. Receive mail with a mailbox provider: Google Workspace, Zoho Mail (popular with small Indian businesses) or Fastmail are all solid. If you only need forwarding, Cloudflare Email Routing can forward addresses to a mailbox you already have. Send your app’s mail through a transactional service such as Resend, Postmark or Amazon SES, from a subdomain, send.pipeslab.in.
Why a subdomain? Reputation attaches to the domain that sends the mail. If a receipt template misfires and the bounce rate spikes, the damage lands on send. and not on the domain your team’s office mail uses. Most services insist on the subdomain anyway, which makes the choice easy.
Here is a short annotated zone for the lab domain. The names and key are placeholders, so copy the exact values from your provider’s dashboard. The shape is the lesson.
; receiving: mail for pipeslab.in goes to the mailbox providerpipeslab.in. MX 10 aspmx.provider.example.
; sending: the transactional service lives on a subdomainsend.pipeslab.in. MX 10 bounces.provider.example.send.pipeslab.in. TXT "v=spf1 include:spf.provider.example ~all"resend._domainkey.send.pipeslab.in. TXT "p=MIGfMA0GC...(truncated)"
; the policy that ties the visible From: address to both checks_dmarc.pipeslab.in. TXT "v=DMARC1; p=none; rua=mailto:dmarc@pipeslab.in"Read it top down. The first record answers “where does mail to me go?” The second block answers “who may send for send., and which key signs it?” The third is the policy. The two domains never clash, because SPF is checked against the envelope domain, which here is the send. name, while the root domain lists only the mailbox provider.
Now the alignment rule, in one sentence. The visible From: is hello@pipeslab.in, and DKIM signs with d=send.pipeslab.in. Under DMARC’s default relaxed alignment a subdomain matches its parent, so the signature aligns. Sign with the provider’s own unrelated domain and DKIM can pass while DMARC still fails.
The lab: receive, send, and prove it for pipeslab.in
Section titled “The lab: receive, send, and prove it for pipeslab.in”Goal. By the end, pipeslab.in receives mail on a provider you did not have to run yourself, your app sends from send.pipeslab.in through a transactional service, and one real message proves that all three checks pass. Figure 13.5 shows the loop you will be watching.
The steps come in this order, because each one depends on the one before it.
- Choose the inbox. Decide who receives mail for
hello@pipeslab.in. A mailbox provider such as Google Workspace or Zoho Mail gives you a real inbox. Cloudflare Email Routing gives you forwarding into a mailbox you already have. Either way, the root domain’s MX records point at the provider, not at your VPS. - Keep the root domain’s mail records small. The root name lists the mailbox provider in its MX and SPF, and nothing else. Resist the urge to add the transactional service there too. It belongs on the subdomain.
- Add the sending domain in the transactional service. Register
send.pipeslab.inin the dashboard. The service hands you a DKIM record name and value, and often a return-path record. Copy them exactly, character for character, and wait until the dashboard says the domain is verified. - Publish SPF for the subdomain. Give
send.pipeslab.inits own SPF record that names the transactional service and nothing else. Count the DNS lookups as you go, and stay well under ten. - Publish DMARC at the root with
p=none. Add aruaaddress that you really read. Leave the policy at none. For now you are watching, not blocking. - Send one real message from the app. Trigger a receipt or a test email to a Gmail address you control. Open it, choose Show original, and read the authentication block near the top.
How you’ll know it worked. There are three signals, and they count in this order.
- Show original reports SPF PASS, DKIM PASS, and DMARC PASS. Look at the DKIM signing domain too. It should match the domain in the visible From: line. A PASS with a mismatched domain is a PASS that will not save you from DMARC.
- The transactional dashboard shows the domain as verified, and the message left from the sending address the provider promised.
- Within about a day, the report mailbox receives at least one aggregate report. It is XML, often compressed, and it lists the sending servers that passed or failed for your domain. The first one looks like noise. You are looking for the transactional provider’s servers appearing with aligned passes.
When all three signals agree, you have done what most small teams never do. Your mail is no longer a rumour the big mailboxes have to guess about.
What you now know
Section titled “What you now know”You started this chapter with a dance-academy receipt in a parent’s spam folder. You now know why it landed there. The receiving mailbox could not tell that the message was really from you, so it made a guess, and the guess was wrong.
The fix is not one trick. It is three records that agree with each other, plus a sensible split of duties. MX says where mail for your domain goes, and it governs receiving only. SPF says which servers may carry your letters, judged by the envelope domain. DKIM is the wax seal: a signature from a private key, with the public half published in DNS under a selector. DMARC is the instruction sheet that ties those two checks to the From: line your reader sees, tells receivers what to do when they fail, and sends you reports so you can see what is happening.
Alignment is the step most people miss. A message can pass SPF and DKIM and still fail DMARC, if the domains that passed do not match the visible sender. You saw that in the lab, and you know to look for it.
The 2024 bulk-sender rules made DMARC a floor, not a nice-to-have, for anyone sending in volume. The recommendation from this chapter is short. Never send mail from the VPS. Receive through a provider. Send through a transactional service from a subdomain such as send., and keep the root domain for people.
Well done. You have a domain that receives its own mail, a sender that cannot be spoofed by a stranger, and reports that tell you when someone tries. Move the policy from none to quarantine only after the reports look clean for a couple of weeks, and then to reject once you are sure nothing legitimate is failing.
Self-quiz
Section titled “Self-quiz”1. Your code is correct and Resend accepted the message. Why can it still land in spam?
Acceptance means the provider took the message, not that the receiver trusts it. The receiving mailbox decides from the sending domain’s reputation and from whether SPF, DKIM and DMARC line up with the visible From address.
2. What does SPF check, and against which address?
SPF checks whether the server delivering the mail is on the list published in DNS for the domain in the envelope sender, the Return-Path. It looks at the sending IP address, not at the message content.
3. Where does a DKIM public key live, and what does the private key do?
The public key is a TXT record at the selector name under _domainkey in your zone. The private key stays with the sending service and signs the headers and body, so the receiver can check the seal against the published key.
4. Why can SPF and DKIM both pass while DMARC still fails?
DMARC needs alignment. The domain that passed SPF or DKIM must match the domain in the visible From header. A message can pass for the sending service’s own domain and still fail DMARC for yours.
5. Why roll DMARC out from p=none to quarantine to reject, in steps?
With p=none you only watch reports. That shows every legitimate sender you forgot, such as the newsletter tool or the invoice system, before you start rejecting. Jumping straight to reject breaks mail you did not know you sent.
Exercises
Section titled “Exercises”Warm-up 1. Two addresses. In one sentence each, say which address SPF checks and which address DMARC cares about.
Answer
SPF checks the envelope sender, the Return-Path domain. DMARC cares about the domain in the visible From header, and it wants SPF or DKIM to align with that domain.
Warm-up 2. Why not the VPS? Give two reasons your app should not send mail straight from its own VPS.
Answer
Most clouds block outbound port 25 by default, so the connection never leaves. Even when the port is open, a fresh IP has no reputation, and its reverse DNS (PTR) may not match your mail hostname, so receivers treat it as suspicious.
Warm-up 3. The seal. Which key signs a DKIM message, and which key does the receiver use to check it?
Answer
The sender’s private key signs. The receiver fetches the public key from DNS at the selector name under _domainkey and verifies the signature against it.
Lab 4. Publish a monitoring-only DMARC record. Add a DMARC TXT record for pipeslab.in with p=none and a rua address you can read.
Hint and model solution
Hint: the record name is _dmarc.pipeslab.in. The value starts with v=DMARC1; p=none; and adds rua=mailto: followed by your reporting address. Check it with dig TXT _dmarc.pipeslab.in +short once the change propagates.
Model solution: v=DMARC1; p=none; rua=mailto:dmarc@pipeslab.in, the same string as the zone excerpt earlier in this chapter.
Lab 5. Three PASS lines. Send a test message from your Resend-verified subdomain to a Gmail address you control, then open the message with Show original.
Hint and model solution
Hint: in Gmail, open the message menu and choose Show original. Find the SPF, DKIM and DMARC results near the top of the headers.
Model solution: all three read PASS. The DKIM signing domain is on the send subdomain, and the DMARC result is aligned with the From domain.
Lab 6. Receive without a server. Point the MX record for pipeslab.in at your chosen mailbox provider, then send a test mail to hello@pipeslab.in and confirm it reaches the provider’s inbox.
Hint and model solution
Hint: dig MX pipeslab.in +short should show the provider’s hosts. Nothing on the lab VPS should be waiting on port 25 for this domain.
Model solution: the MX answers name the provider, and the test mail sits in its inbox. The VPS stays out of the mail path.
Lab 7. Read one aggregate report. Wait a day for a DMARC aggregate report to reach your rua address, then count the sources that sent mail as your domain.
Hint and model solution
Hint: reports arrive as compressed XML attachments, often from large receivers. Each record lists a source IP, a message count, and the SPF and DKIM results.
Model solution: you should see your own provider and perhaps one or two strangers. A source you do not recognise is exactly what p=none exists to show you.
Staff 8. A rollout plan. Your app sends receipts, your team sends newsletters from a marketing tool, and a finance tool sends invoices. Design the path from p=none to reject.
Model answer
Stay on p=none until the reports show every legitimate source passing and aligned, including the marketing and finance tools. Then move to quarantine on a fraction of mail, widen it, and finally move to reject. Each step waits until the reports stay clean, because an unknown sender is what breaks at reject.
Staff 9. The lookup limit. Your SPF record includes five providers, and each of those includes two more. Why might SPF fail even though every provider is legitimate?
Model answer
SPF allows at most 10 DNS lookups during evaluation, and each include and some mechanisms count toward that. Nested includes can push the total past 10, which produces a permerror and fails the check. The fix is to flatten or trim the includes, not to add more of them.
Book 2 of a series. Book 1: The Glue.