From 41a73b5e4f95b3b1c1871d56b85b6ba8c5ccf71c Mon Sep 17 00:00:00 2001 From: Ryahn Date: Mon, 27 Jan 2025 20:06:45 -0500 Subject: [PATCH] Fix body beings stored as [object] --- src/haraka-plugins/queue/store_message.js | 29 +++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/haraka-plugins/queue/store_message.js b/src/haraka-plugins/queue/store_message.js index 8fb5bec..0e945af 100644 --- a/src/haraka-plugins/queue/store_message.js +++ b/src/haraka-plugins/queue/store_message.js @@ -7,32 +7,36 @@ exports.register = function () { if (!transaction) return next(); try { - // Get the full message body as a string + // Get the full message body let body = ''; if (transaction.message_stream) { - // Convert message stream to string - body = transaction.message_stream.toString(); + // 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); + } } - // Get headers safely - const headers = transaction.header ? transaction.header.headers_decoded : {}; - const subject = transaction.header ? transaction.header.get('subject') : ''; + // 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(), to: transaction.rcpt_to.map((addr) => addr.address()).join(", "), - subject: subject, + subject: transaction.header ? transaction.header.get('subject') : '', body: body, - headers: headers, + headers: transaction.header ? transaction.header.headers_decoded : {}, }; // Debug logging connection.logdebug(plugin, 'Message Data:'); - connection.logdebug(plugin, `From: ${messageData.from}`); - connection.logdebug(plugin, `To: ${messageData.to}`); - connection.logdebug(plugin, `Subject: ${messageData.subject}`); - connection.logdebug(plugin, `Body length: ${body.length}`); + connection.logdebug(plugin, `Body type: ${typeof body}`); + connection.logdebug(plugin, `Body content: ${body.substring(0, 100)}...`); // First 100 chars await MessageService.store(messageData); next(); @@ -42,6 +46,5 @@ exports.register = function () { } }; - // Register hooks - use data_post instead of queue plugin.register_hook('data_post', 'store_message'); }; \ No newline at end of file