- Add LocalStack and Adjust local Emailing Signed-off-by: Dave Richer <dave@imexsystems.ca>
50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
const { isString, isEmpty } = require("lodash");
|
|
const { defaultProvider } = require("@aws-sdk/credential-provider-node");
|
|
const { default: InstanceManager } = require("../utils/instanceMgr");
|
|
const aws = require("@aws-sdk/client-ses");
|
|
const nodemailer = require("nodemailer");
|
|
|
|
const isLocal = isString(process.env?.LOCALSTACK_HOSTNAME) && !isEmpty(process.env?.LOCALSTACK_HOSTNAME);
|
|
|
|
const sesConfig = {
|
|
apiVersion: "latest",
|
|
credentials: defaultProvider(),
|
|
region: isLocal
|
|
? "ca-central-1"
|
|
: InstanceManager({
|
|
imex: "ca-central-1",
|
|
rome: "us-east-2"
|
|
})
|
|
};
|
|
|
|
if (isLocal) {
|
|
sesConfig.endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:4566`;
|
|
console.log(`SES Mailer set to LocalStack end point: ${sesConfig.endpoint}`);
|
|
}
|
|
|
|
const ses = new aws.SES(sesConfig);
|
|
|
|
let transporter = nodemailer.createTransport({
|
|
SES: { ses, aws }
|
|
});
|
|
|
|
if (isLocal) {
|
|
// Wrap the sendMail function to log the email contents to the console in local environment
|
|
const originalSendMail = transporter.sendMail.bind(transporter);
|
|
transporter.sendMail = async (mailOptions) => {
|
|
try {
|
|
const result = await originalSendMail(mailOptions);
|
|
console.log(
|
|
`Email sent successfully - From: ${result?.envelope?.from} - To: ${result?.envelope?.to} - MessageID ${result.messageId} - Check LocalStack to see message`
|
|
);
|
|
|
|
return result;
|
|
} catch (error) {
|
|
console.error("Failed to send email:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
}
|
|
|
|
module.exports = transporter;
|