email_worker_parser icon indicating copy to clipboard operation
email_worker_parser copied to clipboard

Uncaught ReferenceError: require is not defined at worker.js:1:20 (Code: 10021)

Open koliwbr opened this issue 2 years ago • 5 comments

Uncaught ReferenceError: require is not defined at worker.js:1:20 (Code: 10021)

koliwbr avatar Apr 30 '23 01:04 koliwbr

did you solved it? I am also getting error from import { EmailMessage } from "cloudflare:email";

Uncaught Error: No such module "cloudflare-internal:email".
  imported from "cloudflare:email"  

boynet avatar Aug 22 '23 09:08 boynet

No

koliwbr avatar Aug 22 '23 09:08 koliwbr

so I kinda solve it change the require to import and remove all the cloudflare:email stuff

import PostalMime from "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 fetch(request, env, ctx) {
		return new Response('Hello World!');
	},

	async email(event, env, ctx) {
		const rawEmail = await streamToArrayBuffer(event.raw, event.rawSize);
		const parser = new PostalMime();
		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);
			});
		}
		await event.forward(*your email address*);

	},
};

now you can use other method if you need to send email but the mail parsing works

boynet avatar Aug 22 '23 11:08 boynet

Currently not working Captura de Tela 2024-02-24 às 13 59 55

unilogica avatar Feb 24 '24 17:02 unilogica

use wrangler to deploy from local nodejs env to worker, then you can simply npm install postal-mime

xiaopeng12138 avatar Mar 05 '24 23:03 xiaopeng12138