Email workers access to attachments, body or raw message

Last week I got access to the beta for email workers. When exploring what is possible and what not, I’m missing documentation on how to access the message body and attachments. Or alternatively the raw message (with multiple bodies for different content types and all attachments).

Is this something not documented or just not possible at all?

After some more digging, discovered there is a raw property on the message which seems to be of type ReadableStream.

Trying to put the incoming raw message on a bound R2 bucket with the put method, unfortunately results in an error: TypeError: Provided readable stream must have a known length (request/response body or readable half of FixedLengthStream).

1 Like

You can follow the code from this example GitHub - edevil/email_worker_parser: An example of a worker that parses an email to read the full email.

1 Like

Thank you, that is indeed very helpful.

The code looks exactly like what I need, but I get this error when trying to save it.
image

Do I need config to allow

const PostalMime = require(‘postal-mime’);

I am working within a domain’s email routing:
image

You should look at doing development locally. As believe, the quick editor doesn’t support module imports yet,

1 Like

How can we actually access the data in the attachment file though?

attachment.content will return an array buffer of the message that you can use.

attachment .contents seems to be empty this is the attachment wheno try to insert and image “attachments”: [
{
“filename”: “Spiny_Babbler,_Shivapuri_Nagarjun_NP,Nepal-_IMG.sgym.17.jpg”,
“mimeType”: “image/jpeg”,
“disposition”: “inline”,
“related”: true,
“contentId”: “<ii_li8ddzh60>”,
“content”: {}
}
]

If you are having issue with the library used to parse the attachment then you should on an issue on their GitHub repo

1 Like

The postal-mime module API has changed, and it now includes built-in support for Cloudflare Email Workers. You can read about using it to parse emails from here: https://docs.emailengine.app/how-to-parse-emails-with-cloudflare-email-workers/

The attachment “content” is logged as an empty object because it is an Uint8Array value and you can not convert it into a JSON string.

3 Likes

Excellent. Hidden in the corners of the world.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.