Fix base64-encoded HTML email bodies not rendering; v1.1.18
Inbound emails from some clients (Outlook, mobile) send htmlBody as base64. Decode it on the server before storing (future emails) and on the client before passing to the iframe srcDoc (existing stored emails). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,18 @@ import { MAX_CONTACT_SUBMISSIONS } from '../config.js'
|
||||
// RFC 5322 msg-id: "<" printable-ASCII-no-whitespace ">"
|
||||
const MESSAGE_ID_RE = /^<[\x21-\x7E]+>$/
|
||||
|
||||
function decodeHtmlBody(raw) {
|
||||
if (typeof raw !== 'string' || !raw.trim()) return null
|
||||
const s = raw.trim()
|
||||
if (s.startsWith('<')) return s // already plain HTML
|
||||
try {
|
||||
const decoded = Buffer.from(s.replace(/\s+/g, ''), 'base64').toString('utf8')
|
||||
return decoded.trimStart().startsWith('<') ? decoded : s
|
||||
} catch {
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
export function register(app) {
|
||||
app.post('/api/inbound-email', (req, res) => {
|
||||
const secret = process.env.INBOUND_EMAIL_SECRET
|
||||
@@ -47,7 +59,7 @@ export function register(app) {
|
||||
name: fromName || fromEmail,
|
||||
email: fromEmail,
|
||||
message: [subject ? `Subject: ${subject}` : '', body ?? ''].filter(Boolean).join('\n\n'),
|
||||
htmlBody: typeof htmlBody === 'string' && htmlBody.trim() ? htmlBody.trim() : null,
|
||||
htmlBody: decodeHtmlBody(htmlBody),
|
||||
messageType: 'general',
|
||||
subscribe: false,
|
||||
archived: false,
|
||||
|
||||
Reference in New Issue
Block a user