diff --git a/src/haraka-plugins/queue/store_message.js b/src/haraka-plugins/queue/store_message.js index 566f38e..0e945af 100644 --- a/src/haraka-plugins/queue/store_message.js +++ b/src/haraka-plugins/queue/store_message.js @@ -7,15 +7,23 @@ exports.register = function () { if (!transaction) return next(); try { - // Get the raw email content - const body = await new Promise((resolve, reject) => { - let messageContent = ''; - transaction.message_stream.on('data', chunk => { - messageContent += chunk.toString(); - }); - transaction.message_stream.on('end', () => resolve(messageContent)); - transaction.message_stream.on('error', reject); - }); + // Get the full message body + let body = ''; + + if (transaction.message_stream) { + // Convert Buffer to string and handle JSON stringification properly + if (Buffer.isBuffer(transaction.message_stream)) { + body = transaction.message_stream.toString('utf8'); + } else if (typeof transaction.message_stream === 'object') { + body = JSON.stringify(transaction.message_stream); + } else { + body = String(transaction.message_stream); + } + } + + // Debug logging + connection.logdebug(plugin, 'Message Stream Type:', typeof transaction.message_stream); + connection.logdebug(plugin, 'Message Stream:', transaction.message_stream); const messageData = { from: transaction.mail_from.address(), @@ -25,6 +33,11 @@ exports.register = function () { headers: transaction.header ? transaction.header.headers_decoded : {}, }; + // Debug logging + connection.logdebug(plugin, 'Message Data:'); + connection.logdebug(plugin, `Body type: ${typeof body}`); + connection.logdebug(plugin, `Body content: ${body.substring(0, 100)}...`); // First 100 chars + await MessageService.store(messageData); next(); } catch (error) {