Can't send email from a worker.

I'm trying to send emails from a worker, I'm following the example in the docs, but I can't get it to work and I don't understand so parts of the code. I'm following the example here: https://developers.cloudflare.com/email-routing/email-workers/send-email-workers/ The my main issue right now is this:
try {
await env.SEB.send(message);
} catch (e) {
return new Response(e.message);
}
try {
await env.SEB.send(message);
} catch (e) {
return new Response(e.message);
}
What is SEB and where does it come from? I assume it stands for Send Email Binding. But when I replace it with the binding defined in my wrangler.toml it throws a not a function error, which is not a surprise as the binding is just object of strings. The other examples provided in the docs, all start from message object, and simply reply or forward it. Where is message object created and then sent?
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 …
1 Reply
blindChicken
blindChickenOP7mo ago
I figured the issue: The docs are wrong. I needed to define the binding in my wrangler.toml file like this:
[[send_email]]
binding = 'CF_MAILER'
type = "send_email"
name = 'CF_MAILER'
destination_address = '[email protected]'
[[send_email]]
binding = 'CF_MAILER'
type = "send_email"
name = 'CF_MAILER'
destination_address = '[email protected]'
Not sure if the binding key is required, but it works. Previously I send_email defined like it is shown in the above docs and it was under the [vars] heading. My next problem is with mimetext library, I'm having issues with dependencies. but I think it may be the same issue as above, but with respect to how node_compat is defined. Again I had node_compat defined under [vars]. I moved it to the top of the file where I assume it should be. But this didn't fix my problem. At first when it was throwing missing dependencies errors like paths.js I installed them with npm but that was a mistake. So I deleted the installed deps from the package.json, then deleted the node_modules folder and reinstalled everything with npm i , and then voila! It worked. In conclusion, my guess is that node_compat was not correctly configure in my wrangler.toml file so could find the deps. But installing the missing deps was not solution.
Want results from more Discord servers?
Add your server