From 16bd82d81d2643f4e721e05f1b943c191619874c Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Wed, 28 Feb 2024 14:02:21 +0200 Subject: [PATCH] fix(webhooks): include network routing information in messageSent, messageDeliveryError and messageFailed webhooks --- lib/connection.js | 31 ++++++++++++++++++++++++++++--- workers/submit.js | 9 +++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 7e028a64..3c6d5d0b 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -2052,7 +2052,8 @@ class Connection { proto: 'SMTP', address, name, - selector + selector, + requestedLocalAddress: data.localAddress }); let smtpLogger = {}; @@ -2136,6 +2137,24 @@ class Connection { smtpSettings.tls.rejectUnauthorized = false; } + const networkRouting = smtpSettings.localAddress || smtpSettings.proxy ? {} : null; + + if (networkRouting && smtpSettings.localAddress) { + networkRouting.localAddress = smtpSettings.localAddress; + } + + if (networkRouting && smtpSettings.proxy) { + networkRouting.proxy = smtpSettings.proxy; + } + + if (networkRouting && smtpSettings.name) { + networkRouting.name = smtpSettings.name; + } + + if (networkRouting && data.localAddress && data.localAddress !== networkRouting.localAddress) { + networkRouting.requestedLocalAddress = data.localAddress; + } + const transporter = nodemailer.createTransport(smtpSettings); transporter.set('proxy_socks_module', socks); try { @@ -2192,7 +2211,8 @@ class Connection { originalMessageId, response: info.response, queueId, - envelope + envelope, + networkRouting }); // clean up possible cached SMTP error @@ -2370,7 +2390,8 @@ class Connection { response: err.response, responseCode: err.responseCode, code: err.code, - command: err.command + command: err.command, + networkRouting }, smtpStatus ); @@ -2416,12 +2437,16 @@ class Connection { smtpResponseCode: err.responseCode, smtpCommand: err.command, + networkRouting, + job: jobData }); err.code = err.code || 'SubmitFail'; err.statusCode = Number(err.responseCode) || null; + err.info = { networkRouting }; + throw err; } } diff --git a/workers/submit.js b/workers/submit.js index e037b3ea..66bb619d 100644 --- a/workers/submit.js +++ b/workers/submit.js @@ -286,7 +286,8 @@ const submitWorker = new Worker( message: err.message, code: err.code, statusCode: err.statusCode - } + }, + networkRouting: err.info?.networkRouting }); } catch (err) { // ignore @@ -381,7 +382,8 @@ submitWorker.on('failed', async job => { await notify(job.data.account, EMAIL_FAILED_NOTIFY, { messageId: job.data.messageId, queueId: job.data.queueId, - error: job.stacktrace && job.stacktrace[0] && job.stacktrace[0].split('\n').shift() + error: job.stacktrace && job.stacktrace[0] && job.stacktrace[0].split('\n').shift(), + networkRouting: job.progress?.networkRouting }); } } @@ -423,6 +425,9 @@ parentPort.on('message', message => { if (message.statusCode) { err.statusCode = message.statusCode; } + if (message.info) { + err.info = message.info; + } return reject(err); } else { return resolve(message.response);