118314d0d7
Worker now extracts both text/plain and text/html MIME parts and sends htmlBody in the webhook payload. Server stores it on the submission. Admin inbox renders htmlBody in a sandboxed iframe when present, falling back to plain text with whitespace preserved. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
144 lines
4.6 KiB
JavaScript
144 lines
4.6 KiB
JavaScript
/**
|
|
* Cloudflare Email Worker — versebyversewithnate.us
|
|
*
|
|
* Receives inbound emails to hello@ and nate@, forwards to Gmail,
|
|
* and POSTs a parsed copy to the site admin inbox.
|
|
*
|
|
* CLOUDFLARE SETUP:
|
|
* 1. Go to Email > Email Routing > Routes in your Cloudflare dashboard
|
|
* 2. Add two "Custom address" rules:
|
|
* hello@versebyversewithnate.us → Send to Worker → this worker
|
|
* nate@versebyversewithnate.us → Send to Worker → this worker
|
|
* 3. Make sure nmemmert@gmail.com is listed as a verified destination address
|
|
* (Email Routing > Destination addresses)
|
|
*
|
|
* WORKER ENVIRONMENT VARIABLES (Workers & Pages > this worker > Settings > Variables):
|
|
* GMAIL_FORWARD_ADDRESS = nmemmert@gmail.com
|
|
* WEBHOOK_URL = https://versebyversewithnate.us/api/inbound-email
|
|
* WEBHOOK_SECRET = <a long random string — must match INBOUND_EMAIL_SECRET on the server>
|
|
*
|
|
* SERVER ENVIRONMENT VARIABLE (set in your server host / .env):
|
|
* INBOUND_EMAIL_SECRET = <same long random string as WEBHOOK_SECRET above>
|
|
*/
|
|
|
|
export default {
|
|
async email(message, env, ctx) {
|
|
const forwardPromise = message.forward(env.GMAIL_FORWARD_ADDRESS)
|
|
|
|
const rawEmail = await streamToText(message.raw)
|
|
|
|
const parsed = parseEmail(rawEmail, message)
|
|
|
|
const webhookPromise = fetch(env.WEBHOOK_URL, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Webhook-Secret': env.WEBHOOK_SECRET,
|
|
},
|
|
body: JSON.stringify(parsed),
|
|
}).catch(err => console.error('[email-worker] webhook failed:', err.message))
|
|
|
|
await Promise.all([forwardPromise, webhookPromise])
|
|
},
|
|
}
|
|
|
|
async function streamToText(stream) {
|
|
const reader = stream.getReader()
|
|
const chunks = []
|
|
while (true) {
|
|
const { done, value } = await reader.read()
|
|
if (done) break
|
|
chunks.push(value)
|
|
}
|
|
const bytes = new Uint8Array(chunks.reduce((acc, c) => acc + c.length, 0))
|
|
let offset = 0
|
|
for (const chunk of chunks) {
|
|
bytes.set(chunk, offset)
|
|
offset += chunk.length
|
|
}
|
|
return new TextDecoder().decode(bytes)
|
|
}
|
|
|
|
function parseEmail(raw, message) {
|
|
const lines = raw.split(/\r?\n/)
|
|
|
|
// Parse headers (everything before the first blank line)
|
|
const headers = {}
|
|
let bodyStart = 0
|
|
for (let i = 0; i < lines.length; i++) {
|
|
if (lines[i].trim() === '') {
|
|
bodyStart = i + 1
|
|
break
|
|
}
|
|
const colon = lines[i].indexOf(':')
|
|
if (colon > 0) {
|
|
const key = lines[i].slice(0, colon).trim().toLowerCase()
|
|
const val = lines[i].slice(colon + 1).trim()
|
|
if (!headers[key]) headers[key] = val
|
|
}
|
|
}
|
|
|
|
const subject = decodeHeaderValue(headers['subject'] ?? '(no subject)')
|
|
const from = message.from ?? headers['from'] ?? ''
|
|
const to = message.to ?? headers['to'] ?? ''
|
|
const date = headers['date'] ?? new Date().toISOString()
|
|
const messageId = headers['message-id'] ?? ''
|
|
|
|
const bodyLines = lines.slice(bodyStart)
|
|
const plainText = extractPart(raw, bodyLines, 'text/plain')
|
|
const htmlBody = extractPart(raw, bodyLines, 'text/html')
|
|
|
|
return {
|
|
from,
|
|
to,
|
|
subject,
|
|
body: plainText,
|
|
htmlBody: htmlBody || null,
|
|
date,
|
|
messageId,
|
|
source: 'inbound-email',
|
|
}
|
|
}
|
|
|
|
function extractPart(raw, bodyLines, contentType) {
|
|
const escaped = contentType.replace('/', '\\/')
|
|
const re = new RegExp(`Content-Type: ${escaped}[^\\r\\n]*\\r?\\n(?:[^\\r\\n]+\\r?\\n)*\\r?\\n([\\s\\S]*?)(?=--|$)`, 'i')
|
|
const match = re.exec(raw)
|
|
if (match) return decodeEmailBody(match[1]).trim()
|
|
|
|
if (contentType === 'text/plain') {
|
|
const joined = bodyLines.join('\n')
|
|
if (/<[a-z][\s\S]*>/i.test(joined)) {
|
|
return joined.replace(/<[^>]+>/g, '').replace(/ /g, ' ').replace(/&/g, '&').replace(/\s{3,}/g, '\n\n').trim()
|
|
}
|
|
return joined.trim()
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
function decodeEmailBody(text) {
|
|
// Handle quoted-printable encoding (=XX hex sequences and soft line breaks)
|
|
return text
|
|
.replace(/=\r?\n/g, '')
|
|
.replace(/=([0-9A-Fa-f]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)))
|
|
}
|
|
|
|
function decodeHeaderValue(value) {
|
|
// Handle encoded words: =?UTF-8?B?...?= or =?UTF-8?Q?...?=
|
|
return value.replace(/=\?([^?]+)\?([BQ])\?([^?]*)\?=/gi, (_, charset, encoding, encoded) => {
|
|
try {
|
|
if (encoding.toUpperCase() === 'B') {
|
|
const binary = atob(encoded)
|
|
return new TextDecoder(charset).decode(Uint8Array.from(binary, c => c.charCodeAt(0)))
|
|
}
|
|
if (encoding.toUpperCase() === 'Q') {
|
|
return encoded.replace(/_/g, ' ').replace(/=([0-9A-Fa-f]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)))
|
|
}
|
|
} catch {
|
|
return encoded
|
|
}
|
|
return encoded
|
|
})
|
|
}
|