2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2020-12-29 12:43:53 -08:00
|
|
|
passwordReset,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2020-12-29 12:43:53 -08:00
|
|
|
sendInvite,
|
2021-04-21 15:57:12 +02:00
|
|
|
sendNewLoginLocation,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2020-12-29 12:43:53 -08:00
|
|
|
backupFailed,
|
2016-01-22 17:37:41 -08:00
|
|
|
|
2020-12-29 12:43:53 -08:00
|
|
|
certificateRenewalError,
|
2016-03-19 20:40:03 -07:00
|
|
|
|
2020-12-29 12:43:53 -08:00
|
|
|
sendTestMail,
|
2017-09-15 14:21:52 +02:00
|
|
|
|
2019-04-15 16:36:37 -07:00
|
|
|
_mailQueue: [] // accumulate mails in test mode
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
const assert = require('assert'),
|
2019-10-24 13:34:14 -07:00
|
|
|
BoxError = require('./boxerror.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
debug = require('debug')('box:mailer'),
|
|
|
|
|
ejs = require('ejs'),
|
2019-11-05 19:54:53 -08:00
|
|
|
mail = require('./mail.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
nodemailer = require('nodemailer'),
|
|
|
|
|
path = require('path'),
|
|
|
|
|
safe = require('safetydance'),
|
2016-10-13 11:14:17 +02:00
|
|
|
settings = require('./settings.js'),
|
2020-11-19 23:38:59 +01:00
|
|
|
translation = require('./translation.js'),
|
2021-08-19 12:32:23 -07:00
|
|
|
smtpTransport = require('nodemailer-smtp-transport'),
|
|
|
|
|
util = require('util');
|
2017-01-19 14:55:54 +01:00
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
const MAIL_TEMPLATES_DIR = path.join(__dirname, 'mail_templates');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
// This will collect the most common details required for notification emails
|
2021-08-19 12:32:23 -07:00
|
|
|
async function getMailConfig() {
|
|
|
|
|
const cloudronName = await settings.getCloudronName();
|
|
|
|
|
const supportConfig = await settings.getSupportConfig();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
cloudronName,
|
|
|
|
|
notificationFrom: `"${cloudronName}" <no-reply@${settings.dashboardDomain()}>`,
|
|
|
|
|
supportEmail: supportConfig.email
|
|
|
|
|
};
|
2016-09-03 12:02:42 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
async function sendMail(mailOptions) {
|
2019-04-15 16:36:37 -07:00
|
|
|
assert.strictEqual(typeof mailOptions, 'object');
|
2015-11-10 00:24:40 -08:00
|
|
|
|
2019-04-15 16:36:37 -07:00
|
|
|
if (process.env.BOX_ENV === 'test') {
|
|
|
|
|
exports._mailQueue.push(mailOptions);
|
2021-08-19 12:32:23 -07:00
|
|
|
return;
|
2019-04-15 16:36:37 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
const data = await mail.getMailAuth();
|
|
|
|
|
|
|
|
|
|
const transport = nodemailer.createTransport(smtpTransport({
|
|
|
|
|
host: data.ip,
|
|
|
|
|
port: data.port,
|
|
|
|
|
auth: {
|
|
|
|
|
user: mailOptions.authUser || `no-reply@${settings.dashboardDomain()}`,
|
|
|
|
|
pass: data.relayToken
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
2021-10-26 09:19:48 -07:00
|
|
|
const transportSendMail = util.promisify(transport.sendMail.bind(transport));
|
2021-08-19 12:32:23 -07:00
|
|
|
const [error] = await safe(transportSendMail(mailOptions));
|
|
|
|
|
if (error) throw new BoxError(BoxError.EXTERNAL_ERROR, error);
|
|
|
|
|
debug(`Email "${mailOptions.subject}" sent to ${mailOptions.to}`);
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-19 23:38:59 +01:00
|
|
|
function render(templateFile, params, translationAssets) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof templateFile, 'string');
|
|
|
|
|
assert.strictEqual(typeof params, 'object');
|
|
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
let content = null;
|
|
|
|
|
let raw = safe.fs.readFileSync(path.join(MAIL_TEMPLATES_DIR, templateFile), 'utf8');
|
2020-11-19 23:38:59 +01:00
|
|
|
if (raw === null) {
|
|
|
|
|
debug(`Error loading ${templateFile}`);
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof translationAssets === 'object') raw = translation.translate(raw, translationAssets.translations || {}, translationAssets.fallback || {});
|
2017-08-04 12:02:50 +02:00
|
|
|
|
|
|
|
|
try {
|
2020-11-19 23:38:59 +01:00
|
|
|
content = ejs.render(raw, params);
|
2017-08-04 12:02:50 +02:00
|
|
|
} catch (e) {
|
2017-08-04 11:15:55 -07:00
|
|
|
debug(`Error rendering ${templateFile}`, e);
|
2017-08-04 12:02:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return content;
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-27 19:58:06 +02:00
|
|
|
async function sendInvite(user, invitor, email, inviteLink) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof user, 'object');
|
2020-02-05 15:53:05 +01:00
|
|
|
assert.strictEqual(typeof invitor, 'object');
|
2021-10-27 21:25:43 +02:00
|
|
|
assert.strictEqual(typeof email, 'string');
|
2020-02-05 15:53:05 +01:00
|
|
|
assert.strictEqual(typeof inviteLink, 'string');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
const mailConfig = await getMailConfig();
|
|
|
|
|
const translationAssets = await translation.getTranslations();
|
|
|
|
|
|
|
|
|
|
const templateData = {
|
|
|
|
|
user: user.displayName || user.username || user.email,
|
|
|
|
|
webadminUrl: settings.dashboardOrigin(),
|
|
|
|
|
inviteLink: inviteLink,
|
|
|
|
|
invitor: invitor ? invitor.email : null,
|
|
|
|
|
cloudronName: mailConfig.cloudronName,
|
|
|
|
|
cloudronAvatarUrl: settings.dashboardOrigin() + '/api/v1/cloudron/avatar'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
2021-10-27 19:58:06 +02:00
|
|
|
to: email,
|
2021-08-19 12:32:23 -07:00
|
|
|
subject: ejs.render(translation.translate('{{ welcomeEmail.subject }}', translationAssets.translations || {}, translationAssets.fallback || {}), { cloudron: mailConfig.cloudronName }),
|
|
|
|
|
text: render('welcome_user-text.ejs', templateData, translationAssets),
|
|
|
|
|
html: render('welcome_user-html.ejs', templateData, translationAssets)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await sendMail(mailOptions);
|
2016-01-18 16:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
async function sendNewLoginLocation(user, loginLocation) {
|
2021-04-21 15:57:12 +02:00
|
|
|
assert.strictEqual(typeof user, 'object');
|
2021-04-30 11:32:15 -07:00
|
|
|
assert.strictEqual(typeof loginLocation, 'object');
|
|
|
|
|
|
|
|
|
|
const { ip, userAgent, country, city } = loginLocation;
|
|
|
|
|
|
2021-04-30 13:21:50 +02:00
|
|
|
assert.strictEqual(typeof ip, 'string');
|
|
|
|
|
assert.strictEqual(typeof userAgent, 'string');
|
|
|
|
|
assert.strictEqual(typeof country, 'string');
|
|
|
|
|
assert.strictEqual(typeof city, 'string');
|
2021-04-21 15:57:12 +02:00
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
const mailConfig = await getMailConfig();
|
|
|
|
|
const translationAssets = await translation.getTranslations();
|
|
|
|
|
|
|
|
|
|
const templateData = {
|
|
|
|
|
user: user.displayName || user.username || user.email,
|
|
|
|
|
ip,
|
|
|
|
|
userAgent: userAgent || 'unknown',
|
|
|
|
|
country,
|
|
|
|
|
city,
|
|
|
|
|
cloudronName: mailConfig.cloudronName,
|
|
|
|
|
cloudronAvatarUrl: settings.dashboardOrigin() + '/api/v1/cloudron/avatar'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
2021-11-17 11:53:03 -08:00
|
|
|
to: user.email,
|
2021-08-19 12:32:23 -07:00
|
|
|
subject: ejs.render(translation.translate('{{ newLoginEmail.subject }}', translationAssets.translations || {}, translationAssets.fallback || {}), { cloudron: mailConfig.cloudronName }),
|
|
|
|
|
text: render('new_login_location-text.ejs', templateData, translationAssets),
|
|
|
|
|
html: render('new_login_location-html.ejs', templateData, translationAssets)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await sendMail(mailOptions);
|
2021-04-21 15:57:12 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-27 18:36:28 +02:00
|
|
|
async function passwordReset(user, email, resetLink) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof user, 'object');
|
2021-10-27 18:36:28 +02:00
|
|
|
assert.strictEqual(typeof email, 'string');
|
|
|
|
|
assert.strictEqual(typeof resetLink, 'string');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
const mailConfig = await getMailConfig();
|
|
|
|
|
const translationAssets = await translation.getTranslations();
|
|
|
|
|
|
|
|
|
|
const templateData = {
|
|
|
|
|
user: user.displayName || user.username || user.email,
|
2021-09-16 14:34:56 +02:00
|
|
|
resetLink: resetLink,
|
2021-08-19 12:32:23 -07:00
|
|
|
cloudronName: mailConfig.cloudronName,
|
|
|
|
|
cloudronAvatarUrl: settings.dashboardOrigin() + '/api/v1/cloudron/avatar'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
2021-10-27 18:36:28 +02:00
|
|
|
to: email,
|
2021-08-19 12:32:23 -07:00
|
|
|
subject: ejs.render(translation.translate('{{ passwordResetEmail.subject }}', translationAssets.translations || {}, translationAssets.fallback || {}), { cloudron: mailConfig.cloudronName }),
|
|
|
|
|
text: render('password_reset-text.ejs', templateData, translationAssets),
|
|
|
|
|
html: render('password_reset-html.ejs', templateData, translationAssets)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await sendMail(mailOptions);
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
async function backupFailed(mailTo, errorMessage, logUrl) {
|
2019-03-10 15:53:20 -07:00
|
|
|
assert.strictEqual(typeof mailTo, 'string');
|
2021-06-24 01:18:27 -07:00
|
|
|
assert.strictEqual(typeof errorMessage, 'string');
|
|
|
|
|
assert.strictEqual(typeof logUrl, 'string');
|
2019-03-10 15:53:20 -07:00
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
const mailConfig = await getMailConfig();
|
2016-10-14 14:46:34 -07:00
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
const mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: mailTo,
|
|
|
|
|
subject: `[${mailConfig.cloudronName}] Failed to backup`,
|
|
|
|
|
text: render('backup_failed.ejs', { cloudronName: mailConfig.cloudronName, message: errorMessage, logUrl, format: 'text' })
|
|
|
|
|
};
|
2018-01-19 16:27:19 +01:00
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
await sendMail(mailOptions);
|
2016-10-14 14:46:34 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
async function certificateRenewalError(mailTo, domain, message) {
|
2019-03-10 15:53:20 -07:00
|
|
|
assert.strictEqual(typeof mailTo, 'string');
|
2016-03-19 20:40:03 -07:00
|
|
|
assert.strictEqual(typeof domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof message, 'string');
|
|
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
const mailConfig = await getMailConfig();
|
2016-03-19 20:40:03 -07:00
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
const mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: mailTo,
|
|
|
|
|
subject: `[${mailConfig.cloudronName}] Certificate renewal error`,
|
|
|
|
|
text: render('certificate_renewal_error.ejs', { domain: domain, message: message, format: 'text' })
|
|
|
|
|
};
|
2016-07-26 16:47:58 -07:00
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
await sendMail(mailOptions);
|
2016-03-19 20:40:03 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 09:40:06 -07:00
|
|
|
async function sendTestMail(domain, email) {
|
2018-01-23 16:10:23 -08:00
|
|
|
assert.strictEqual(typeof domain, 'string');
|
2017-09-15 14:21:52 +02:00
|
|
|
assert.strictEqual(typeof email, 'string');
|
|
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
const mailConfig = await getMailConfig();
|
2017-09-15 14:21:52 +02:00
|
|
|
|
2021-08-19 12:32:23 -07:00
|
|
|
const mailOptions = {
|
|
|
|
|
authUser: `no-reply@${domain}`,
|
|
|
|
|
from: `"${mailConfig.cloudronName}" <no-reply@${domain}>`,
|
|
|
|
|
to: email,
|
|
|
|
|
subject: `[${mailConfig.cloudronName}] Test Email`,
|
|
|
|
|
text: render('test.ejs', { cloudronName: mailConfig.cloudronName, format: 'text'})
|
|
|
|
|
};
|
2018-01-19 16:27:19 +01:00
|
|
|
|
2021-08-22 09:40:06 -07:00
|
|
|
await sendMail(mailOptions);
|
2017-09-15 14:21:52 +02:00
|
|
|
}
|