I just started exploring email routing and email workers look amazing. However, I am struggling to get it working. I need to use email workers to forward the email data to a webhook URL. I need to post the plain text (or HTML content) of the email but I am struggling to get it. Can anyone help how can I get the email plain text content or the HTML from the message object?
export default {
async email(message, env, ctx) {
console.log(message.plainBody);
const webhookUrl = 'https://example.com/store-email';
const webhookPayload = JSON.stringify({
subject: message.headers.get('subject'),
mail_from: message.from,
rcpt_to: message.to,
plain_body: message.content // Need help here
});
try {
const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: webhookPayload,
});
if (response.ok) {
console.log('Webhook request successful');
} else {
console.error('Webhook request failed:', response.status);
}
} catch (error) {
console.error('Error sending webhook request:', error);
}
}
}