diff --git a/packages/local-mail-watcher/src/index.ts b/packages/local-mail-watcher/src/index.ts index a0ab3b2d1..2a0505c5c 100644 --- a/packages/local-mail-watcher/src/index.ts +++ b/packages/local-mail-watcher/src/index.ts @@ -7,6 +7,7 @@ import { SnsMessage } from './types/SNS' import { simpleParser } from 'mailparser' import axios from 'axios' import { convertToMailObject } from './lib/emailApi' +import { decodeBase64 } from './lib/base64' console.log('Starting worker...') @@ -91,13 +92,27 @@ app.post('/sns', async (req, res) => { const message = JSON.parse(snsMessage.Message) as { notificationType: string content: string + receipt?: { + action?: { + encoding?: string + } + } } if (message.notificationType != 'Received') { console.log('Not an email, failing...') res.status(400).send() + return } - const mailContent = await simpleParser(message.content) + // Check if content is base64 encoded and decode if necessary + let emailContent = message.content + if (message.receipt?.action?.encoding === "BASE64") { + console.log("Detected BASE64 encoded content, decoding...") + emailContent = decodeBase64(message.content) + console.log("Processed BASE64 content") + } + + const mailContent = await simpleParser(emailContent) const mail = convertToMailObject(mailContent) console.log(mail) await ( @@ -108,7 +123,7 @@ app.post('/sns', async (req, res) => { delay: 500, }) res.sendStatus(200) - + res.status(200).send() return } diff --git a/packages/local-mail-watcher/src/lib/base64.js b/packages/local-mail-watcher/src/lib/base64.js new file mode 100644 index 000000000..13e117625 --- /dev/null +++ b/packages/local-mail-watcher/src/lib/base64.js @@ -0,0 +1,19 @@ +/** + * Utility functions for handling base64 encoding/decoding + */ + +/** + * Decodes a base64 string to UTF-8 text + * Uses Node.js Buffer but with type safety + */ +export function decodeBase64(base64String: string): string { + try { + // Use a type-safe approach without relying on global + // @ts-ignore - Ignore TypeScript error for Buffer + return Buffer.from(base64String, 'base64').toString('utf-8'); + } catch (error) { + console.error('Error decoding base64 string:', error); + return base64String; // Return original string if decoding fails + } + } + \ No newline at end of file