Skip to content

Commit

Permalink
fix(webhooks): include network routing information in messageSent, me…
Browse files Browse the repository at this point in the history
…ssageDeliveryError and messageFailed webhooks
  • Loading branch information
andris9 committed Feb 28, 2024
1 parent 472642c commit 16bd82d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
31 changes: 28 additions & 3 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2052,7 +2052,8 @@ class Connection {
proto: 'SMTP',
address,
name,
selector
selector,
requestedLocalAddress: data.localAddress
});

let smtpLogger = {};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -2192,7 +2211,8 @@ class Connection {
originalMessageId,
response: info.response,
queueId,
envelope
envelope,
networkRouting
});

// clean up possible cached SMTP error
Expand Down Expand Up @@ -2370,7 +2390,8 @@ class Connection {
response: err.response,
responseCode: err.responseCode,
code: err.code,
command: err.command
command: err.command,
networkRouting
},
smtpStatus
);
Expand Down Expand Up @@ -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;
}
}
Expand Down
9 changes: 7 additions & 2 deletions workers/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ const submitWorker = new Worker(
message: err.message,
code: err.code,
statusCode: err.statusCode
}
},
networkRouting: err.info?.networkRouting
});
} catch (err) {
// ignore
Expand Down Expand Up @@ -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
});
}
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 16bd82d

Please sign in to comment.