136 lines
3.4 KiB
JavaScript
136 lines
3.4 KiB
JavaScript
const path = require("path");
|
|
require("dotenv").config({
|
|
path: path.resolve(
|
|
process.cwd(),
|
|
`.env.${process.env.NODE_ENV || "development"}`
|
|
),
|
|
});
|
|
|
|
const mailjet = require("node-mailjet").connect(
|
|
process.env.email_api,
|
|
process.env.email_secret
|
|
);
|
|
|
|
let nodemailer = require("nodemailer");
|
|
let aws = require("aws-sdk");
|
|
|
|
const ses = new aws.SES({
|
|
apiVersion: "2010-12-01",
|
|
region: "ca-central-1",
|
|
});
|
|
|
|
let transporter = nodemailer.createTransport({
|
|
SES: { ses, aws },
|
|
});
|
|
|
|
exports.sendEmail = async (req, res) => {
|
|
if (process.env.NODE_ENV !== "production") {
|
|
console.log("[EMAIL] Incoming Message", req.body.from.name);
|
|
}
|
|
|
|
transporter.sendMail(
|
|
{
|
|
from: `${req.body.from.name} <${req.body.from.address}>`,
|
|
replyTo: req.body.ReplyTo.Email,
|
|
to: req.body.to,
|
|
cc: req.body.cc,
|
|
subject: "Message",
|
|
attachments:
|
|
(req.body.attachments &&
|
|
req.body.attachments.map((a) => {
|
|
return {
|
|
path: a,
|
|
};
|
|
})) ||
|
|
null,
|
|
html: req.body.html,
|
|
ses: {
|
|
// optional extra arguments for SendRawEmail
|
|
Tags: [
|
|
{
|
|
Name: "tag_name",
|
|
Value: "tag_value",
|
|
},
|
|
],
|
|
},
|
|
},
|
|
(err, info) => {
|
|
console.log(err || info);
|
|
if (info) {
|
|
console.log("[EMAIL] Email sent: " + info);
|
|
res.json({ success: true, response: info });
|
|
} else {
|
|
console.log("[EMAIL] Email send failed. ", err);
|
|
res.json({ success: false, error: err });
|
|
}
|
|
}
|
|
);
|
|
|
|
// const inlinedCssHtml = await inlineCssTool(req.body.html, {
|
|
// url: "https://imex.online",
|
|
// });
|
|
|
|
// console.log("inlinedCssHtml", inlinedCssHtml);
|
|
|
|
// Create the promise and SES service object
|
|
|
|
// const request = mailjet.post("send", { version: "v3.1" }).request({
|
|
// Messages: [
|
|
// {
|
|
// From: {
|
|
// Email: req.body.from.address,
|
|
// Name: req.body.from.name,
|
|
// },
|
|
// To:
|
|
// req.body.to &&
|
|
// req.body.to.map((i) => {
|
|
// return { Email: i };
|
|
// }),
|
|
// CC:
|
|
// req.body.cc &&
|
|
// req.body.cc.map((i) => {
|
|
// return { Email: i };
|
|
// }),
|
|
// ReplyTo: {
|
|
// Email: req.body.ReplyTo.Email,
|
|
// Name: req.body.ReplyTo.Name,
|
|
// },
|
|
// Subject: req.body.subject,
|
|
// // TextPart:
|
|
// // "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!",
|
|
// HTMLPart: req.body.html,
|
|
// Attachments: req.body.attachments || null,
|
|
// },
|
|
// ],
|
|
// });
|
|
|
|
// request
|
|
// .then((result) => {
|
|
// console.log("[EMAIL] Email sent: " + result);
|
|
// res.json({ success: true, response: result });
|
|
// })
|
|
// .catch((err) => {
|
|
// console.log("[EMAIL] Email send failed. ", err);
|
|
// res.json({ success: false, error: err.message });
|
|
// });
|
|
|
|
// transporter.sendMail(
|
|
// {
|
|
// ...req.body,
|
|
// from: {
|
|
// name: req.body.from.name ,
|
|
// address: "noreply@bodyshop.app",
|
|
// },
|
|
// },
|
|
// function (error, info) {
|
|
// if (error) {
|
|
// console.log("[EMAIL] Email send failed. ", error);
|
|
// res.json({ success: false, error: error });
|
|
// } else {
|
|
// console.log("[EMAIL] Email sent: " + info.response);
|
|
// res.json({ success: true, response: info.response });
|
|
// }
|
|
// }
|
|
// );
|
|
};
|