-
Notifications
You must be signed in to change notification settings - Fork 0
feat: inbound email handler for finance@chitty.cc #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,47 @@ export default { | |||||||
| if (!ok) console.warn('[cron:discovery] heartbeat failed'); | ||||||||
| })); | ||||||||
| }, | ||||||||
|
|
||||||||
| async email(message: ForwardableEmailMessage, env: Env, ctx: ExecutionContext) { | ||||||||
| const from = message.from; | ||||||||
| const to = message.to; | ||||||||
| const subject = message.headers.get('subject') || '(no subject)'; | ||||||||
| const messageId = message.headers.get('message-id') || `${Date.now()}`; | ||||||||
| const size = message.rawSize; | ||||||||
|
|
||||||||
| console.log(`[email:inbound] from=${from} to=${to} subject="${subject}" size=${size}`); | ||||||||
|
|
||||||||
| // Store raw email in R2 for document ingestion pipeline | ||||||||
| const ts = new Date().toISOString().replace(/[:.]/g, '-'); | ||||||||
| const sanitizedId = messageId.replace(/[<>]/g, '').replace(/[^a-zA-Z0-9@._-]/g, '_'); | ||||||||
| const key = `inbound-email/${ts}_${sanitizedId}.eml`; | ||||||||
|
|
||||||||
| const rawBytes = await new Response(message.raw).arrayBuffer(); | ||||||||
| await env.FINANCE_R2.put(key, rawBytes, { | ||||||||
| customMetadata: { | ||||||||
| from, | ||||||||
| to, | ||||||||
| subject, | ||||||||
| messageId, | ||||||||
| receivedAt: new Date().toISOString(), | ||||||||
| sizeBytes: String(size), | ||||||||
|
Comment on lines
+45
to
+47
|
||||||||
| }, | ||||||||
| }); | ||||||||
|
|
||||||||
| console.log(`[email:inbound] stored in R2: ${key} (${rawBytes.byteLength} bytes)`); | ||||||||
|
|
||||||||
| // Index in KV for quick lookup | ||||||||
| const kv = env.FINANCE_KV; | ||||||||
| const indexEntry = JSON.stringify({ | ||||||||
| key, | ||||||||
| from, | ||||||||
| to, | ||||||||
| subject, | ||||||||
| receivedAt: new Date().toISOString(), | ||||||||
| sizeBytes: rawBytes.byteLength, | ||||||||
| }); | ||||||||
| await kv.put(`email:inbound:${ts}`, indexEntry, { expirationTtl: 86400 * 90 }); // 90 days | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Using only Useful? React with 👍 / 👎.
|
||||||||
| await kv.put(`email:inbound:${ts}`, indexEntry, { expirationTtl: 86400 * 90 }); // 90 days | |
| const indexKey = `email:inbound:${ts}_${sanitizedId}_${crypto.randomUUID()}`; | |
| await kv.put(indexKey, indexEntry, { expirationTtl: 86400 * 90 }); // 90 days |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new Response(message.raw).arrayBuffer()buffers the entire email into memory before writing to R2. For larger messages this can be slow and risk Worker memory limits; prefer streamingmessage.rawdirectly toR2Bucket.put(or otherwise avoid holding the full payload in memory) sincemessage.rawSizeis already available for sizing.