Created
January 20, 2023 19:23
-
-
Save jasonbyrne/0c670614517f8713bd693beee4104dac to your computer and use it in GitHub Desktop.
Cloudflare Workers Email Routing POC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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