- Published on
Recieve Emails with Code
- Authors
- Name
- elliot plant
- @plant_elliot
I was messing around with my email settings when I found that you can use Cloudflare to route emails on any of your domains hosted there. That means that I can set up me@elliotplant.com to go directly to my gmail inbox.
And in the same docs where I figured out how to do that, I found that you can handle emails with workers. I've always expected application flows that start with an inbound email to be really hard to do. Once again, Cloudflare makes it super easy.
How do you do it?
Setting up a worker to recieve emails is as simple as:
- Log in to the Cloudflare dashboard and select your account and the domain you want to receive emails on.
- Go to Email > Email Routing > Email Workers.
- Select "Get started"
- Give it a good name (this isn't necessarily the email address that recieves the email)
- Select any starter. If you want to use the body of the email, try this, and note that the contents will have a lot more than what appears in your gmail inbox:
export default {
async email(message, env, ctx) {
console.log('from', message.from);
console.log('to', message.to);
const stringContents = await new Response(message.raw).text();
console.log('stringContents', stringContents);
},
};
- Hit "Save and deploy"
- Finally, hit "Create route" to add an email address on your domain to trigger the worker.
This body is useless. Can I just get the body of the email as text?
If you want to interpret your body in a cleaner way, try using a tool like MailParser. You can get the text()
or html()
of the email pretty simply that way.