Fixing "Email Delivery Failed" inside Cloudflare Workers: Migrating from MailChannels to Resend
If you have built a simple contact form using serverless tech, you probably reached for the classic Cloudflare Workers setup. For a long time, the unwritten cheat code for adding transactional email to these workers was a legacy integration with MailChannels. You could simply point a fetch request to https://mailchannels.net, and without any complex authorization headers, your contact form just worked.
But recently, my contact form on erprashant.com threw a breaking runtime exception:
"Email delivery failed. Check Mailchannels DNS on erprashant.com."
If you are seeing this error payload or getting locked out by a restrictive dashboard screen stating "Purchase MailChannels — Contact your hosting provider to ensure MailChannels is enabled for your domain," here is exactly why your serverless pipeline broke, and how to fix it by migrating to a stable, modern service like Resend.
The Root Cause: Why MailChannels Broke Your Worker
If you managed your name servers purely through the Cloudflare Dashboard, you likely added the recommended default SPF records:
v=spf1 include:_spf.mx.cloudflare.net include:relay.mailchannels.net ~all
While this syntax looks correct on paper, MailChannels officially ended its unauthenticated free relay tier for independent Cloudflare Workers.
Historically, MailChannels allowed traffic to route transparently from Cloudflare's internal edge networks. Now, their endpoints explicitly check whether your domain is associated with an active, paid enterprise hosting partner (like cPanel, Hostgator, or Plesk). If you run purely serverless applications on Workers without an underlying web host contract, MailChannels rejects the handshake with an automatic HTTP 401 Authorization Required error.
Furthermore, checking the Cloudflare Email Routing Console won't show these logs. That summary metric page tracks strictly inbound forwarded email. Outbound API requests bypass that logging interface entirely.
The Solution: Migrating to Resend
To restore email delivery with minimal code modifications, the best path forward is migrating to Resend. They offer a developer-friendly transactional API with a generous free tier (up to 3,000 emails per month), making it perfect for personal portfolio contact forms.
Step 1: Add Your Domain and Update DNS
- Sign up for a free account at Resend.com.
- Go to Domains > Add Domain and enter
erprashant.com. - Resend will provide you with specific TXT (SPF/DKIM) and MX records. Add these directly inside your Cloudflare DNS panel to verify domain ownership.
- Generate an API Key from the Resend dashboard and copy it.
Step 2: Bind Your Secret Key
To ensure your API key remains secure and isn't hardcoded into your repository, save it securely using the Wrangler CLI platform:
wrangler secret put RESEND_API_KEY
When prompted, paste your Resend API token.
Step 3: Implement the Refactored Script Logic
Replace your legacy MailChannels fetch block with this clean, direct integration to the Resend API endpoint:
async function sendMail(env, { to, from, name, email, message }) {
// Use your verified domain email for sending
const verifiedSender = "abc@example.com";
const payload = {
from: `Tools from Human <${verifiedSender}>`,
to: [to],
reply_to: `${name} <${email}>`,
subject: `Contact: ${name} — example.com`,
text: `Name: ${name}\nEmail: ${email}\n\n${message}\n\n---\nSent from example.com contact form`,
};
const res = await fetch("https://resend.com", {
method: "POST",
headers: {
"Authorization": `Bearer ${env.RESEND_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (res.ok) return { ok: true };
const detail = await res.text();
console.error("Resend API error:", res.status, detail);
return { ok: false, error: "Email delivery failed via Resend." };
}
Tracking and Debugging Serverless Exceptions
Once deployed, you can verify that your contact form parameters are routing correctly in real time by tailing your serverless logs directly from your terminal:
wrangler tail
This pipes live execution states and console errors right to your machine, allowing you to quickly spot any payload anomalies or missing headers.
By deprecating the legacy MailChannels setup and moving your outbound forms to Resend, you regain full visibility over your delivery logs, bypass host-dependent restrictions, and establish a much more reliable mail pipeline for your serverless tools.
Keep reading