blurts-server/email-utils.js

103 строки
3.0 KiB
JavaScript
Исходник Обычный вид История

2018-02-07 22:16:39 +03:00
"use strict";
const AppConstants = require("./app-constants");
const nodemailer = require("nodemailer");
const hbs = require("nodemailer-express-handlebars");
const { negotiateLanguages, acceptedLanguages } = require("fluent-langneg");
const HBSHelpers = require("./hbs-helpers");
2018-09-21 19:00:27 +03:00
const mozlog = require("./log");
2018-09-21 19:00:27 +03:00
const log = mozlog("email-utils");
const hbsOptions = {
viewEngine: {
extname: ".hbs",
layoutsDir: __dirname + "/views/email/layouts",
defaultLayout: "default_email",
partialsDir: __dirname + "/views/email/email_partials/",
helpers: HBSHelpers,
},
viewPath: __dirname + "/views/email/",
extName: ".hbs",
};
2018-02-07 22:16:39 +03:00
// The SMTP transport object. This is initialized to a nodemailer transport
// object while reading SMTP credentials, or to a dummy function in debug mode.
let gTransporter;
const EmailUtils = {
async init(smtpUrl = AppConstants.SMTP_URL) {
// Allow a debug mode that will log JSON instead of sending emails.
if (!smtpUrl) {
2018-09-21 19:00:27 +03:00
log.info("smtpUrl-empty", { message: "EmailUtils will log a JSON response instead of sending emails." });
gTransporter = nodemailer.createTransport({jsonTransport: true});
2018-02-07 22:16:39 +03:00
return Promise.resolve(true);
}
gTransporter = nodemailer.createTransport(smtpUrl);
const gTransporterVerification = await gTransporter.verify();
gTransporter.use("compile", hbs(hbsOptions));
return Promise.resolve(gTransporterVerification);
2018-02-07 22:16:39 +03:00
},
getSupportedLocales(req) {
const requestedLanguage = acceptedLanguages(req.headers["accept-language"]);
const supportedLocales = negotiateLanguages(
requestedLanguage,
req.app.locals.AVAILABLE_LANGUAGES,
{defaultLocale: "en"}
);
return supportedLocales;
},
sendEmail(aRecipient, aSubject, aTemplate, aContext) {
2018-02-07 22:16:39 +03:00
if (!gTransporter) {
return Promise.reject("SMTP transport not initialized");
}
const emailContext = Object.assign({
SERVER_URL: AppConstants.SERVER_URL,
}, aContext);
2018-02-07 22:16:39 +03:00
return new Promise((resolve, reject) => {
const emailFrom = AppConstants.EMAIL_FROM;
2018-02-08 02:29:41 +03:00
const mailOptions = {
from: emailFrom,
2018-02-07 22:16:39 +03:00
to: aRecipient,
subject: aSubject,
template: aTemplate,
context: emailContext,
headers: {
"x-ses-configuration-set": AppConstants.SES_CONFIG_SET,
},
2018-02-07 22:16:39 +03:00
};
gTransporter.sendMail(mailOptions, (error, info) => {
if (error) {
reject(error);
return;
}
if (gTransporter.transporter.name === "JSONTransport") {
2018-09-21 19:00:27 +03:00
log.info("JSONTransport", { message: info.message.toString() });
}
2018-02-07 22:16:39 +03:00
resolve(info);
});
});
},
verifyUrl (subscriber) {
return `${AppConstants.SERVER_URL}/user/verify?token=${encodeURIComponent(subscriber.verification_token)}`;
},
unsubscribeUrl (subscriber) {
return `${AppConstants.SERVER_URL}/user/unsubscribe?token=${encodeURIComponent(subscriber.verification_token)}&hash=${encodeURIComponent(subscriber.sha1)}`;
},
2018-02-08 02:29:41 +03:00
};
2018-02-07 22:16:39 +03:00
module.exports = EmailUtils;