How to test an email Worker locally?

I am trying to make an email Worker, I am using an example, but I have no idea how to test it. index.js:
const PostalMime = require("postal-mime");

async function streamToArrayBuffer(stream, streamSize) {
let result = new Uint8Array(streamSize);
let bytesRead = 0;
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
result.set(value, bytesRead);
bytesRead += value.length;
}
return result;
}

export default {
async email(event, env, ctx) {
const rawEmail = await streamToArrayBuffer(event.raw, event.rawSize);
const parser = new PostalMime.default();
const parsedEmail = await parser.parse(rawEmail);
console.log("Mail subject: ", parsedEmail.subject);
console.log("Mail message ID", parsedEmail.messageId);
console.log("HTML version of Email: ", parsedEmail.html);
console.log("Text version of Email: ", parsedEmail.text);
if (parsedEmail.attachments.length == 0) {
console.log("No attachments");
} else {
parsedEmail.attachments.forEach((att) => {
console.log("Attachment: ", att.filename);
console.log("Attachment disposition: ", att.disposition);
console.log("Attachment mime type: ", att.mimeType);
console.log("Attachment size: ", att.content.byteLength);
});
}
},
};
const PostalMime = require("postal-mime");

async function streamToArrayBuffer(stream, streamSize) {
let result = new Uint8Array(streamSize);
let bytesRead = 0;
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
result.set(value, bytesRead);
bytesRead += value.length;
}
return result;
}

export default {
async email(event, env, ctx) {
const rawEmail = await streamToArrayBuffer(event.raw, event.rawSize);
const parser = new PostalMime.default();
const parsedEmail = await parser.parse(rawEmail);
console.log("Mail subject: ", parsedEmail.subject);
console.log("Mail message ID", parsedEmail.messageId);
console.log("HTML version of Email: ", parsedEmail.html);
console.log("Text version of Email: ", parsedEmail.text);
if (parsedEmail.attachments.length == 0) {
console.log("No attachments");
} else {
parsedEmail.attachments.forEach((att) => {
console.log("Attachment: ", att.filename);
console.log("Attachment disposition: ", att.disposition);
console.log("Attachment mime type: ", att.mimeType);
console.log("Attachment size: ", att.content.byteLength);
});
}
},
};
1 Reply
DaniFoldi
DaniFoldi4w ago
unfortunately local dev is not supported currently :/ you will have to deploy it to be able to receive emails what you can try to do is capture the event from a real email, and then reconstruct it locally* for testing it with a wrapper, or vitest *it won't be exactly identical because classes, but close enough for a postal-mime parse

Did you find this page helpful?