Skip to content

Instantly share code, notes, and snippets.

@jasonbyrne
Created January 20, 2023 19:23
Show Gist options
  • Save jasonbyrne/0c670614517f8713bd693beee4104dac to your computer and use it in GitHub Desktop.
Save jasonbyrne/0c670614517f8713bd693beee4104dac to your computer and use it in GitHub Desktop.
Cloudflare Workers Email Routing POC
const PostalMime = require('postal-mime');
export interface EmailMessage {
readonly from: string;
readonly to: string;
readonly headers: Headers;
readonly raw: ReadableStream;
readonly rawSize: number;
setReject(reason: String): void;
forward(rcptTo: string, headers?: Headers): Promise<void>;
}
export interface Env {
MY_BUCKET: R2Bucket;
}
export default {
async email(message: EmailMessage, env: Env) {
let rawEmail = new Response(message.raw);
let arrayBuffer = await rawEmail.arrayBuffer();
const parser = new PostalMime.default();
const email = await parser.parse(arrayBuffer);
const jsonString = JSON.stringify(email);
await env.MY_BUCKET.put('email', jsonString);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment