Skip to content

Commit ca9a0de

Browse files
committed
ZMS-42: Fix bounce message, add recipient in subject
1 parent ee40254 commit ca9a0de

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

plugins/core/email-bounce.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
const os = require('os');
44
const MimeNode = require('nodemailer/lib/mime-node');
55

6+
function getSingleRecipient(value) {
7+
return (
8+
[]
9+
.concat(value || [])
10+
.map(entry => ((entry && entry.address) || entry || '').toString().trim())
11+
.find(entry => entry) || ''
12+
);
13+
}
14+
15+
function getFailedRecipient(bounce) {
16+
return getSingleRecipient(bounce.recipient || bounce.envelope?.to);
17+
}
18+
19+
function getBounceSubject(bounce, isDelayed) {
20+
const failedRecipient = getFailedRecipient(bounce);
21+
const status = isDelayed ? 'Delay' : 'Failure';
22+
return failedRecipient ? `Delivery Status Notification (${status}: ${failedRecipient})` : `Delivery Status Notification (${status})`;
23+
}
24+
625
module.exports.title = 'Email Bounce Notification';
726
module.exports.init = function (app, done) {
827
// generate a multipart/report DSN failure response
@@ -21,6 +40,8 @@ module.exports.init = function (app, done) {
2140
let from = cfg.mailerDaemon || app.config.mailerDaemon;
2241
let to = bounce.from;
2342
let sendingZone = cfg.sendingZone || app.config.sendingZone;
43+
let failedRecipient = getFailedRecipient(bounce);
44+
let originalRecipientsText = `\nOriginal message recipients:\n ${bounce.to}\n`;
2445

2546
let rootNode = new MimeNode('multipart/report; report-type=delivery-status');
2647

@@ -32,15 +53,15 @@ module.exports.init = function (app, done) {
3253
rootNode.setHeader('X-Sending-Zone', sendingZone);
3354
rootNode.setHeader('X-Failed-Recipients', bounce.to);
3455
rootNode.setHeader('Auto-Submitted', 'auto-replied');
35-
rootNode.setHeader('Subject', `Delivery Status Notification (${isDelayed ? 'Delay' : 'Failure'})`);
56+
rootNode.setHeader('Subject', getBounceSubject(bounce, isDelayed));
3657

3758
if (messageId) {
3859
rootNode.setHeader('In-Reply-To', messageId);
3960
rootNode.setHeader('References', messageId);
4061
}
4162

4263
let bounceContent = `Delivery to the following recipient failed permanently:
43-
${bounce.to}
64+
${failedRecipient}${originalRecipientsText}
4465
4566
Technical details of permanent failure:
4667
@@ -51,7 +72,8 @@ ${bounce.response}
5172
if (isDelayed) {
5273
bounceContent = `Delivery incomplete
5374
54-
There was a temporary problem delivering your message to ${bounce.to}.
75+
There was a temporary problem delivering your message to:
76+
${failedRecipient}${originalRecipientsText}
5577
5678
Delivery will be retried. You'll be notified if the delivery fails permanently.
5779
@@ -73,7 +95,7 @@ X-ZoneMTA-Queue-ID: ${bounce.id}
7395
X-ZoneMTA-Sender: rfc822; ${bounce.from}
7496
Arrival-Date: ${new Date(bounce.arrivalDate).toUTCString().replace(/GMT/, '+0000')}
7597
76-
Final-Recipient: rfc822; ${bounce.to}
98+
Final-Recipient: rfc822; ${failedRecipient}
7799
Action: ${isDelayed ? 'delayed' : 'failed'}
78100
Status: ${isDelayed ? '4.0.0' : '5.0.0'}
79101
` +

0 commit comments

Comments
 (0)