2018-02-07 22:16:39 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const AppConstants = require("./app-constants");
|
|
|
|
|
|
|
|
const nodemailer = require("nodemailer");
|
|
|
|
|
|
|
|
// This is set later when reading SMTP credentials from the environment.
|
|
|
|
// This exists as a variable so we can use it in the from header of emails.
|
|
|
|
let kSMTPUsername;
|
|
|
|
|
|
|
|
// 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 = {
|
|
|
|
init() {
|
|
|
|
// Allow a debug mode that will send JSON back to the client instead of sending emails.
|
2018-02-08 20:49:05 +03:00
|
|
|
// eslint-disable-next-line no-process-env
|
2018-02-07 22:16:39 +03:00
|
|
|
if (process.env.DEBUG_DUMMY_SMTP) {
|
2018-02-08 02:29:41 +03:00
|
|
|
console.log("Running in dummy SMTP mode, /user/breached will send a JSON response instead of sending emails.");
|
2018-02-07 22:16:39 +03:00
|
|
|
gTransporter = {
|
|
|
|
sendMail(options, callback) {
|
2018-02-08 02:29:41 +03:00
|
|
|
callback(null, "dummy mode");
|
2018-02-07 22:16:39 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
return Promise.resolve(true);
|
|
|
|
}
|
|
|
|
console.log("Attempting to get SMTP credentials from environment...");
|
|
|
|
kSMTPUsername = AppConstants.SMTP_USERNAME;
|
2018-02-08 02:29:41 +03:00
|
|
|
const password = AppConstants.SMTP_PASSWORD;
|
|
|
|
const host = AppConstants.SMTP_HOST;
|
|
|
|
const port = AppConstants.SMTP_PORT;
|
2018-02-07 22:16:39 +03:00
|
|
|
if (!(kSMTPUsername && password && host && port)) {
|
|
|
|
return Promise.reject("SMTP credentials could not be read from the environment");
|
|
|
|
}
|
|
|
|
|
|
|
|
gTransporter = nodemailer.createTransport({
|
2018-02-08 02:29:41 +03:00
|
|
|
host,
|
|
|
|
port,
|
2018-02-07 22:16:39 +03:00
|
|
|
secure: true,
|
|
|
|
auth: {
|
|
|
|
user: kSMTPUsername,
|
|
|
|
pass: password,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-02-08 02:29:41 +03:00
|
|
|
gTransporter.verify((error, success) => {
|
2018-02-07 22:16:39 +03:00
|
|
|
if (error) {
|
|
|
|
console.log(error);
|
|
|
|
gTransporter = null;
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
sendEmail(aRecipient, aSubject, aBody) {
|
|
|
|
if (!gTransporter) {
|
|
|
|
return Promise.reject("SMTP transport not initialized");
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-02-08 02:29:41 +03:00
|
|
|
const mailOptions = {
|
|
|
|
from: `"Firefox Breach Alerts" <${kSMTPUsername}>`,
|
2018-02-07 22:16:39 +03:00
|
|
|
to: aRecipient,
|
|
|
|
subject: aSubject,
|
|
|
|
text: aBody,
|
|
|
|
};
|
|
|
|
|
|
|
|
gTransporter.sendMail(mailOptions, (error, info) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(info);
|
|
|
|
});
|
|
|
|
});
|
2018-02-13 15:02:51 +03:00
|
|
|
},
|
2018-02-08 02:29:41 +03:00
|
|
|
};
|
2018-02-07 22:16:39 +03:00
|
|
|
|
|
|
|
module.exports = EmailUtils;
|