Emailing New Posts From a Hugo Deploy With Amazon SES built the sending side of the new-post email. When it first worked, the subscriber list had one contact on it, and that contact was my own test address. Nobody else could join. Ghost used to handle this with a “Sign up” link in its secondary navigation, and I dropped that link along with members when the blog moved to Hugo.
A Hugo site is a folder of files, so there’s nowhere for a form to post. And an address typed into a box doesn’t prove who owns it, so I wanted a confirmation step before anything else gets sent: double opt-in.
I don’t think Ghost was double opt-in, but a lot of it ran where I couldn’t see it. Now that I’m hosting and running things myself, I wanted to be sure.
The subscribe page is live, and the code quoted below is the sanitized copy in Hugo/new-post-email in my companion repo, the same folder the first post draws on, so you’ll see example.com and “Example Blog” where my own names would be.
A signup, start to finish
You type an address into the form and press Subscribe. The form posts to /api/subscribe, which adds the address to the SES contact list as a pending contact and sends one confirmation email from news@techbyjeff.net. You land on a “Check your inbox” page.
The email has a link to /subscribe/confirm/. That page shows a single button. Pressing it posts to /api/confirm, which opts the contact in to the new-posts topic and records when. You land on a “You’re Subscribed” page, and the next post I publish goes to you.
Until you press that button, the confirmation is the only email the address gets.
Functions beside a static site
Azure Static Web Apps can host a small API next to the static files. You put an Azure Functions project in a folder, tell the deploy where it is, and SWA serves it under /api/. Mine is api/, on Node 22. The deploy step gained one line, and the site’s config names the runtime:
| |
The whole entry point, api/src/functions/signup.js, is this:
| |
Both Functions are POST only, and anonymous means no function key is needed to call them, as you’d expect for a public form. The handlers come from makeHandlers, which takes everything it depends on as arguments: the SES calls, the token functions, a throttle, the Turnstile verifier. That shape is for the tests further down.
SWA calls these managed Functions, and Microsoft’s comparison with a Functions app you bring yourself lists what they give up. HTTP triggers are the only kind, which is fine for a pair of POST handlers. There’s also no managed identity, so the Functions have no Azure identity to federate into AWS the way the sending job uses GitHub’s. This side holds a stored key.
The key lives in the SWA app settings, along with a TOKEN_SECRET and the region, and it belongs to an IAM user, swa-signup, that can do very little. It can send only as news@techbyjeff.net through the blog-posts configuration set, and it can get, create and update contacts on the one list. It can’t list contacts and it can’t delete them. Whoever stole it could send as news@ and look up one address at a time. They couldn’t dump my subscriber list.
api also joined the reserved slug list in Test-Post.ps1. Post URLs sit at the site root here, so a post named api.md would collide with the Functions.
Pending as a missing preference
The first post covered the list’s one topic and its OPT_OUT default: a contact with no preference recorded isn’t mailed. I used that gap as the pending state. A signup creates the contact and says nothing about the topic, in api/src/lib/ses.js:
| |
savePending writes attributes only. optIn is the one call that sets a topic preference, and only /api/confirm makes it. The comment at the top is from a surprise on the live list: an update that left AttributesData out wiped it.
Unsubscribing through the SES link records an opt-out on the contact, and that is a preference. Subscribed, unsubscribed and unconfirmed each look different on the contact, and I didn’t have to add a status field to tell them apart.
The form gives one answer
The handlers are in api/src/lib/handlers.js. Every signup that passes the basic checks ends at the same page:
New, pending, already subscribed, or asked an hour ago: every address gets “Check your inbox”. The text on that page is worded to stay true in each case:
If the form said “you’re already subscribed”, anyone could type in an address and learn whether it’s on my list.
A sealed token after the
The confirmation email is short and plain text:

| |
It goes out without SES’s list management, so it carries no unsubscribe link. It’s a one-off answer to a request, and the address isn’t subscribed to anything yet.
The link in it has to carry proof that it came from my confirmation email and which address it’s for. The usual way is a signed token: the address in the clear, plus a signature. I didn’t want the address readable in a URL at all, so the token is encrypted, in api/src/lib/tokens.js:
| |
The cipher is AES-256-GCM with a random 12-byte IV, and the key is a SHA-256 of TOKEN_SECRET. GCM authenticates as well as encrypts. A token that was edited or sealed with a different secret fails at decipher.final(), lands in the catch, and comes back as null. The handler treats null the same as an expired link.
The claim inside holds the address, the time it was issued and a one-time code. The link is built like this:
The token goes after #. Your browser doesn’t send the fragment with the request, so the token stays out of Azure’s request logs. The confirm page, content/subscribe-confirm.md, reads it, copies it into a hidden form field and clears it from the address bar:
| |
A link lasts 48 hours. After that the handler sends you to a page that says the link expired and points back to the form.
Confirming takes a button press
I could have made the link itself do the confirming. Open it, you’re in. Plenty of signup flows work that way, and mail security scanners break them: a scanner that opens every link in a message before delivering it would confirm people who never clicked anything. Both Functions accept POST only, and a scanner fetching the link gets a page with a button on it.

My first commit of the handlers stopped there, and a review before the merge found a hole in it. A link worked as often as you pressed it for 48 hours. Confirming also clears UnsubscribeAll, as it has to if someone who unsubscribed is ever going to sign up again. Put those together: you subscribe, confirm, and the next day unsubscribe. The confirmation email is still in your inbox. Press that link again and you’re back on the list.
This was the confirm call at the time:
| |
Each signup now stores a one-time code on the contact and seals the same code into its link:
| |
Confirming compares the two and then removes the code from the contact:
| |
A link works once, and only while it’s the newest one issued for that address, since a later signup replaces the code. Pressing a used link has no effect. If it’s the link that just worked, you still land on the confirmed page.
Keeping scripts off the form
A public form that sends email is a way to make my domain mail strangers. Somebody could feed it a list of addresses, and SES would send each one a confirmation from news@techbyjeff.net. The form itself, in content/subscribe.md, carries a honeypot and a timer:
| |
| |
(The Turnstile widget and the Subscribe button come after these.) The website field is the honeypot. It’s positioned off the screen, hidden from screen readers and out of the tab order, and a person who somehow finds it is told to leave it empty. It’s off screen and not display: none because some bots skip a field that isn’t rendered at all. elapsed is how long the page was open, filled in by the page’s script when you submit. The Function reads both before it looks at anything else:
| |
Anything under three seconds is treated as a bot. A request that fails either check gets the normal “Check your inbox” answer and no email.
One confirmation a day
Past those checks, each address gets one confirmation a day, and there’s a ceiling across all addresses:
| |
The daily limit is the timestamp confirmSentAt on the contact. My first commit wrote it too early:
That save ran before the link was sealed and the email sent. If the send failed, the address had already used up its one confirmation for the day. You’d retry, get told to check your inbox, and no email would come. The timestamp is now written only after SES accepts the email:
| |
If that last write fails, the email has still gone out. The worst case is a second confirmation on a retry the same day, so the failure is logged and you see the normal page. The “Check your inbox” page mentions the daily limit, because otherwise a second attempt that produces no email looks broken.
The ceiling across all addresses is a counter in memory, api/src/lib/throttle.js:
Twenty confirmations per clock hour. The count is per instance and resets when an instance starts, so it slows a burst and guarantees little. The per-address limit is the real guard, and that one lives on the contact in SES.
Turnstile for what a script can fake
The honeypot and elapsed share a weakness: the sender supplies both values. A script that leaves one field empty and sets the other to 5000 passes, and the form could still be made to send confirmation mail to any address.
Cloudflare Turnstile is a different kind of check. A widget on the page runs Cloudflare’s challenge in your browser and puts a token in a hidden form field. The server sends that token to Cloudflare with a secret, and Cloudflare says whether it’s good. Cloudflare’s docs give a token five minutes and one use.

The check is api/src/lib/turnstile.js:
| |
The handler has to tell a person from a refused token, and both from a Cloudflare that couldn’t be asked. true is a person. false is a token Cloudflare refused. A thrown error means the question went unanswered.
| |
A refused token sends you back to the form with a note that the check hadn’t finished. This is the one bot check that tells you it failed, because a real reader can fail it by submitting before the widget is done or by leaving the tab open past five minutes. If Cloudflare is unreachable, the signup fails closed with the generic error.
describe is the log formatter, and it exists because of your address. SES sometimes quotes an address back in its error text, so anything headed for the Functions’ log has the address cut out first:
| |
The page tries to save you the round trip. If the widget is on the page and hasn’t produced a token yet, the submit handler stops the form and shows the same note:
| |
The typeof window.turnstile test wasn’t in my first version, and a review caught what that meant. The widget’s <div> is written into the page by Hugo whenever the site key is set. If Cloudflare’s script is blocked, by a content blocker or a mistake in the CSP, the <div> is there and no token arrives. The form would have stopped every submit with “hadn’t finished” for as long as you kept trying, and since nothing reached the server, there’d have been no log of it either. The fix is one condition:
Now the form only waits for a token when Cloudflare’s script has loaded. If it hasn’t, the form goes through and the server answers for itself.
The site key is public and lives in hugo.toml. A shortcode, layouts/_shortcodes/turnstile.html, writes the widget into the page only when the key is set:
The secret is an SWA app setting, and with no secret makeVerifier returns null and the handler skips the check. Mind the order you set them in. A secret with no widget on the page refuses every signup, since no form can produce a token. The key has to deploy first, or in the same deploy as the code that reads the secret. Removing the secret is the rollback.
The Content Security Policy needed https://challenges.cloudflare.com in both script-src and frame-src (staticwebapp.config.example.json has a cut-down policy with both). Azure serves that header, so hugo server can’t tell you whether you got it right. The only real test is the deployed page with the console open.
The downside to turning Turnstile on was that some of my automation could no longer test signups, but it was a small price to pay.
Tests against a fake SES
makeHandlers takes its dependencies as arguments so the tests can hand it an in-memory SES, a clock they control and a Turnstile verifier that says whatever the test needs. The test runner is Node’s built-in one, node --test, with no test framework installed. There are 25 tests in api/test/handlers.test.js, and their names read like the rules. A few of them:
| |
The deploy runs them before the Hugo build, so a failing test blocks the site from deploying:
node_modules is removed afterward because the SWA deploy step installs the API’s dependencies itself, and I wanted it to start from the same tree it always had.
Links to the page
The page went live with nothing pointing at it, so I could test it on the real site first with plus-addresses. Then I added the links.
The footer of every page has one. PaperMod renders a footer.text parameter through markdownify, so it took a config value and no template override (hugo.example.toml has it with the other hugo.toml blocks):
The end of every post has a line above the Ko-fi button: “Get new posts by email, one message when the next one goes up.” It’s gated on the page being a post, separately from the kofi flag, so a post that opts out of the tip button still shows it. The About page got one sentence. That puts a signup entry point back on every page, where Ghost had one, without adding to the main menu.
Pruning unconfirmed signups
/api/subscribe saves an address before its owner has agreed to anything. It has to, since the confirm step needs a contact to find. Most pending contacts confirm or get retried. Some are typos, changes of mind or bots that got through, and after the 48-hour link expires those are addresses I’m holding with nobody’s consent.
A weekly scheduled workflow removes them a week after the last attempt. The script, scripts/Remove-PendingContact.ps1, is conservative about what counts:
| |
A contact with any preference for the topic is skipped, which covers subscribers and people who unsubscribed. After that first cut the script fetches each candidate again and checks its attributes: no confirmedAt, no record of posts sent, and a source of form. Anything it can’t read, it keeps.
The send role can’t delete contacts, so the prune runs as a second role that can. It’s off until a repo variable turns it on, and a run by hand defaults to a dry run. My first dry run reported would delete 0, kept 3 of 3 contacts, failed 0. The variable is on now, and that second role is gh-blog-prune.
If you signed up on that page, your address went through all of this: the honeypot and the timer, Turnstile, one confirmation email and a button you pressed yourself. If you never pressed it, the prune lets you go a week later.
