email worker example not working

im trying to make a email worker to handle a contact form, i tried deploying the example worker from the email workers page but it doesnt work and gives a js runtime error Cannot read properties of undefined (reading 'send') im using the example from here https://developers.cloudflare.com/email-routing/email-workers/send-email-workers/ i have modified the sender and recipient emails to be one from my domain with email routing active i have added the node js compat flag needed by mimetext and deployed via wrangler from local development it deploys with no errors but doesn't work when accessed, be it via the browser or by sending a POST directly to the worker via curl, i only get the error as a response
Cloudflare Docs
Send emails from Workers | Cloudflare Email Routing docs
You can send an email about your Worker’s activity from your Worker to an email address verified on Email Routing. This is useful for when you want to know about certain types of events being triggered, for example.
7 Replies
Jürgen Leschner
Hi @Quollveth Usernames, can you confirm that you have a binding in your wrangler.toml with a name that matches your code (SEB if you followed the docs exactly) I tested the behavior myself, and found that in addition to the binding, the to and from addresses must match custom and destination addresses configured in the email routing setup for your domain.
Jürgen Leschner
Writeup added here (see #4), and code here.
fry69
fry692w ago
Thank you, the bindings/SEB part was the missing piece for me, that is not exactly clear from the docs IMHO.
Quollveth Usernames
thanks, i was missing the binding too it works now ok i now have a new problem, it works when i manually send a post via curl, but sending from the site i get a cors error but i guess thats not a problem with workers, this can be marked as solved
Jürgen Leschner
yeah, posting the form cross-origin from a browser requires cors headers. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS hono has built-in cors middleware if you're looking for something lightweight to run in the worker. Another workaround would be to proxy those requests through a worker on the form website's domain so that they are no longer cross-origin.
Towa
Towa3d ago
do you know how to get mail content? text or html message.Content or message.text dont work
Jürgen Leschner
The message body is available in message.raw which you have to parse in your code using a library like postal-mime
import PostalMime from 'postal-mime';

export default {
async email(message, env, ctx) {
const email = await PostalMime.parse(message.raw);

console.log('Subject: ', email.subject);
console.log('HTML: ', email.html);
console.log('Text: ', email.text);
}
};
import PostalMime from 'postal-mime';

export default {
async email(message, env, ctx) {
const email = await PostalMime.parse(message.raw);

console.log('Subject: ', email.subject);
console.log('HTML: ', email.html);
console.log('Text: ', email.text);
}
};
Want results from more Discord servers?
Add your server