IO-557 Send documents in emails.

This commit is contained in:
Patrick Fic
2021-06-07 12:27:14 -07:00
parent 784c58e295
commit 979ba1c142
22 changed files with 218 additions and 164 deletions

View File

@@ -5,12 +5,7 @@ require("dotenv").config({
`.env.${process.env.NODE_ENV || "development"}`
),
});
const mailjet = require("node-mailjet").connect(
process.env.email_api,
process.env.email_secret
);
const axios = require("axios");
let nodemailer = require("nodemailer");
let aws = require("aws-sdk");
@@ -28,21 +23,41 @@ exports.sendEmail = async (req, res) => {
console.log("[EMAIL] Incoming Message", req.body.from.name);
}
let downloadedMedia = [];
if (req.body.media && req.body.media.length > 0) {
downloadedMedia = await Promise.all(
req.body.media.map((m) => {
try {
return getImage(m);
} catch (error) {
console.log(error);
}
})
);
}
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",
subject: req.body.subject,
attachments:
(req.body.attachments &&
req.body.attachments.map((a) => {
[
...((req.body.attachments &&
req.body.attachments.map((a) => {
return {
path: a,
};
})) ||
[]),
...downloadedMedia.map((a) => {
return {
path: a,
};
})) ||
null,
}),
] || null,
html: req.body.html,
ses: {
// optional extra arguments for SendRawEmail
@@ -65,71 +80,10 @@ exports.sendEmail = async (req, res) => {
}
}
);
// 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 });
// }
// }
// );
};
async function getImage(imageUrl) {
let image = await axios.get(imageUrl, { responseType: "arraybuffer" });
let raw = Buffer.from(image.data).toString("base64");
return "data:" + image.headers["content-type"] + ";base64," + raw;
}