IO-2061 Handled Bounced Emails.

This commit is contained in:
Patrick Fic
2022-09-22 14:46:08 -07:00
parent cccd025a24
commit 7fc8cbcca4
19 changed files with 165 additions and 14 deletions

View File

@@ -148,6 +148,7 @@ exports.sendEmail = async (req, res) => {
to: req.body.to,
cc: req.body.cc,
subject: req.body.subject,
messageId: info.messageId,
});
res.json({
success: true, //response: info
@@ -190,6 +191,8 @@ async function logEmail(req, email) {
useremail: req.user.email,
contents: req.body.html,
jobid: req.body.jobid,
sesmessageid: email.messageId,
status: "Sent",
},
});
} catch (error) {
@@ -202,3 +205,60 @@ async function logEmail(req, email) {
});
}
}
exports.emailBounce = async function (req, res, next) {
try {
const body = JSON.parse(req.body);
if (body.type === "SubscriptionConfirmation") {
logger.log("SNS-confirmation", "DEBUG", "api", null, {
message: body.message,
url: body.SubscribeUrl,
body: body,
});
}
if (body.Type === "Notification") {
const message = JSON.parse(body.Message);
let replyTo, subject, messageId;
message.mail.headers.forEach((header) => {
if (header.name === "Reply-To") {
replyTo = header.value;
} else if (header.name === "Subject") {
subject = header.value;
} else if (header.name === "Message-ID") {
messageId = header.value;
}
});
//If it's bounced, log it as bounced in audit log. Send an email to the user.
const result = await client.request(queries.UPDATE_EMAIL_AUDIT, {
sesid: messageId,
status: "Bounced",
context: message.bounce?.bouncedRecipients,
});
transporter.sendMail(
{
from: `ImEX Online <noreply@imex.online>`,
to: "patrick@snapt.ca", // replyTo,
bcc: "patrick@snapt.ca",
subject: `ImEX Online Bounced Email - RE: ${subject}`,
text: `
ImEX Online has tried to deliver an email with the subject: ${subject} to the intended recipients but encountered an error.
${message.bounce?.bouncedRecipients.map(
(r) =>
`Recipient: ${r.emailAddress} | Status: ${r.action} | Code: ${r.diagnosticCode}
`
)}
`,
},
(err, info) => {
console.log("***", err || info);
}
);
}
} catch (error) {
logger.log("sns-error", "ERROR", "api", null, {
error: JSON.stringify(error),
});
}
res.sendStatus(200);
};