2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
userAdded: userAdded,
|
|
|
|
|
userRemoved: userRemoved,
|
|
|
|
|
adminChanged: adminChanged,
|
|
|
|
|
passwordReset: passwordReset,
|
2017-01-27 08:04:03 -08:00
|
|
|
boxUpdateAvailable: boxUpdateAvailable,
|
2015-07-20 00:09:47 -07:00
|
|
|
appUpdateAvailable: appUpdateAvailable,
|
2017-07-21 16:13:44 +02:00
|
|
|
sendDigest: sendDigest,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-01-18 16:11:00 +01:00
|
|
|
sendInvite: sendInvite,
|
2016-04-19 18:39:44 -07:00
|
|
|
unexpectedExit: unexpectedExit,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-08-04 14:31:40 +02:00
|
|
|
appDied: appDied,
|
2017-01-07 13:52:32 -08:00
|
|
|
oomEvent: oomEvent,
|
2015-08-04 14:31:40 +02:00
|
|
|
|
2016-01-22 17:37:41 -08:00
|
|
|
outOfDiskSpace: outOfDiskSpace,
|
2016-10-14 14:46:34 -07:00
|
|
|
backupFailed: backupFailed,
|
2016-01-22 17:37:41 -08:00
|
|
|
|
2016-07-26 16:20:31 -07:00
|
|
|
certificateRenewalError: certificateRenewalError,
|
2016-03-19 20:40:03 -07:00
|
|
|
|
2017-09-15 14:21:52 +02:00
|
|
|
sendTestMail: sendTestMail,
|
|
|
|
|
|
2016-01-18 14:00:35 +01:00
|
|
|
_getMailQueue: _getMailQueue,
|
|
|
|
|
_clearMailQueue: _clearMailQueue
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
2017-11-14 20:34:25 -08:00
|
|
|
var assert = require('assert'),
|
2015-07-20 00:09:47 -07:00
|
|
|
async = require('async'),
|
|
|
|
|
config = require('./config.js'),
|
|
|
|
|
debug = require('debug')('box:mailer'),
|
2015-10-19 11:24:21 -07:00
|
|
|
docker = require('./docker.js').connection,
|
2015-07-20 00:09:47 -07:00
|
|
|
ejs = require('ejs'),
|
2018-01-24 11:33:09 -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'),
|
2017-01-27 08:07:57 -08:00
|
|
|
showdown = require('showdown'),
|
2015-07-20 00:09:47 -07:00
|
|
|
smtpTransport = require('nodemailer-smtp-transport'),
|
2018-04-29 10:58:45 -07:00
|
|
|
users = require('./users.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
util = require('util'),
|
|
|
|
|
_ = require('underscore');
|
|
|
|
|
|
2017-02-07 09:18:43 -08:00
|
|
|
var NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
2017-01-19 14:55:54 +01:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
var MAIL_TEMPLATES_DIR = path.join(__dirname, 'mail_templates');
|
|
|
|
|
|
2017-11-24 13:58:40 -08:00
|
|
|
var gMailQueue = [ ];
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2017-01-26 13:03:36 -08:00
|
|
|
function splatchError(error) {
|
|
|
|
|
var result = { };
|
|
|
|
|
Object.getOwnPropertyNames(error).forEach(function (key) {
|
|
|
|
|
var value = this[key];
|
|
|
|
|
if (value instanceof Error) value = splatchError(value);
|
|
|
|
|
result[key] = value;
|
|
|
|
|
}, error /* thisArg */);
|
|
|
|
|
|
|
|
|
|
return util.inspect(result, { depth: null, showHidden: true });
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
// This will collect the most common details required for notification emails
|
|
|
|
|
function getMailConfig(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
getAdminEmails(function (error, adminEmails) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
settings.getCloudronName(function (error, cloudronName) {
|
|
|
|
|
// this is not fatal
|
|
|
|
|
if (error) {
|
|
|
|
|
debug(error);
|
|
|
|
|
cloudronName = 'Cloudron';
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 14:37:52 -07:00
|
|
|
mail.getDomains(function (error, domains) {
|
2018-01-19 17:44:12 +01:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
if (domains.length === 0) return callback('No domains configured');
|
|
|
|
|
|
2018-01-24 11:33:09 -08:00
|
|
|
const defaultDomain = domains[0];
|
|
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
callback(null, {
|
|
|
|
|
adminEmails: adminEmails,
|
|
|
|
|
cloudronName: cloudronName,
|
2018-01-24 11:33:09 -08:00
|
|
|
notificationDomain: defaultDomain.domain,
|
2018-02-03 18:27:55 -08:00
|
|
|
notificationFrom: `"${cloudronName}" <no-reply@${defaultDomain.domain}>`
|
2018-01-19 17:44:12 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-09-03 12:02:42 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
function processQueue() {
|
2015-11-10 00:24:40 -08:00
|
|
|
sendMails(gMailQueue);
|
|
|
|
|
gMailQueue = [ ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// note : this function should NOT access the database. it is called by the crashnotifier
|
|
|
|
|
// which does not initialize mailer or the databse
|
2017-01-19 14:55:54 +01:00
|
|
|
function sendMails(queue, callback) {
|
2015-11-10 00:24:40 -08:00
|
|
|
assert(util.isArray(queue));
|
2017-01-19 14:55:54 +01:00
|
|
|
callback = callback || NOOP_CALLBACK;
|
2015-11-10 00:24:40 -08:00
|
|
|
|
2016-05-16 08:18:33 -07:00
|
|
|
docker.getContainer('mail').inspect(function (error, data) {
|
2017-01-19 14:55:54 +01:00
|
|
|
if (error) return callback(error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-06-20 18:26:22 -05:00
|
|
|
var mailServerIp = safe.query(data, 'NetworkSettings.Networks.cloudron.IPAddress');
|
2017-01-19 14:55:54 +01:00
|
|
|
if (!mailServerIp) return callback('Error querying mail server IP');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
var transport = nodemailer.createTransport(smtpTransport({
|
|
|
|
|
host: mailServerIp,
|
2016-09-27 10:27:44 -07:00
|
|
|
port: config.get('smtpPort')
|
2015-07-20 00:09:47 -07:00
|
|
|
}));
|
|
|
|
|
|
2016-05-16 12:52:36 -07:00
|
|
|
debug('Processing mail queue of size %d (through %s:2525)', queue.length, mailServerIp);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-11-10 00:24:40 -08:00
|
|
|
async.mapSeries(queue, function iterator(mailOptions, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
transport.sendMail(mailOptions, function (error) {
|
2017-02-07 09:18:43 -08:00
|
|
|
if (error) return debug(error); // TODO: requeue?
|
2015-07-20 00:09:47 -07:00
|
|
|
debug('Email sent to ' + mailOptions.to);
|
|
|
|
|
});
|
|
|
|
|
callback(null);
|
|
|
|
|
}, function done() {
|
|
|
|
|
debug('Done processing mail queue');
|
2017-01-19 14:55:54 +01:00
|
|
|
|
|
|
|
|
callback(null);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enqueue(mailOptions) {
|
|
|
|
|
assert.strictEqual(typeof mailOptions, 'object');
|
|
|
|
|
|
2017-02-07 09:18:43 -08:00
|
|
|
if (!mailOptions.from) debug('sender address is missing');
|
|
|
|
|
if (!mailOptions.to) debug('recipient address is missing');
|
2016-01-18 16:11:00 +01:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
debug('Queued mail for ' + mailOptions.from + ' to ' + mailOptions.to);
|
|
|
|
|
gMailQueue.push(mailOptions);
|
|
|
|
|
|
2017-11-24 13:58:40 -08:00
|
|
|
if (process.env.BOX_ENV !== 'test') processQueue();
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function render(templateFile, params) {
|
|
|
|
|
assert.strictEqual(typeof templateFile, 'string');
|
|
|
|
|
assert.strictEqual(typeof params, 'object');
|
|
|
|
|
|
2017-08-04 12:02:50 +02:00
|
|
|
var content = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
content = ejs.render(safe.fs.readFileSync(path.join(MAIL_TEMPLATES_DIR, templateFile), 'utf8'), params);
|
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getAdminEmails(callback) {
|
2016-01-15 16:15:21 +01:00
|
|
|
users.getAllAdmins(function (error, admins) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2016-04-05 12:23:27 -07:00
|
|
|
if (admins.length === 0) return callback(new Error('No admins on this cloudron')); // box not activated yet
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
var adminEmails = [ ];
|
2018-01-21 14:50:24 +01:00
|
|
|
adminEmails.push(admins[0].fallbackEmail);
|
2015-07-20 00:09:47 -07:00
|
|
|
admins.forEach(function (admin) { adminEmails.push(admin.email); });
|
|
|
|
|
|
|
|
|
|
callback(null, adminEmails);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mailUserEventToAdmins(user, event) {
|
|
|
|
|
assert.strictEqual(typeof user, 'object');
|
|
|
|
|
assert.strictEqual(typeof event, 'string');
|
|
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var adminEmails = _.difference(mailConfig.adminEmails, [ user.email ]);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: adminEmails.join(', '),
|
2018-01-21 14:50:24 +01:00
|
|
|
subject: util.format('[%s] %s %s', mailConfig.cloudronName, user.username || user.fallbackEmail || user.email, event),
|
2018-01-19 17:44:12 +01:00
|
|
|
text: render('user_event.ejs', { user: user, event: event, format: 'text' }),
|
|
|
|
|
};
|
2018-01-19 16:27:19 +01:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
enqueue(mailOptions);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-18 16:11:00 +01:00
|
|
|
function sendInvite(user, invitor) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof user, 'object');
|
|
|
|
|
assert(typeof invitor === 'object');
|
|
|
|
|
|
2016-01-18 16:11:00 +01:00
|
|
|
debug('Sending invite mail');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-10-13 11:14:17 +02:00
|
|
|
var templateData = {
|
|
|
|
|
user: user,
|
|
|
|
|
webadminUrl: config.adminOrigin(),
|
|
|
|
|
setupLink: config.adminOrigin() + '/api/v1/session/account/setup.html?reset_token=' + user.resetToken,
|
|
|
|
|
invitor: invitor,
|
2018-01-19 17:44:12 +01:00
|
|
|
cloudronName: mailConfig.cloudronName,
|
2016-10-13 11:14:17 +02:00
|
|
|
cloudronAvatarUrl: config.adminOrigin() + '/api/v1/cloudron/avatar'
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:38:52 +02:00
|
|
|
var templateDataText = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataText.format = 'text';
|
|
|
|
|
|
|
|
|
|
var templateDataHTML = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataHTML.format = 'html';
|
|
|
|
|
|
2016-10-13 11:14:17 +02:00
|
|
|
var mailOptions = {
|
2018-01-19 17:44:12 +01:00
|
|
|
from: mailConfig.notificationFrom,
|
2018-01-21 14:50:24 +01:00
|
|
|
to: user.fallbackEmail,
|
2018-01-19 17:44:12 +01:00
|
|
|
subject: util.format('Welcome to %s', mailConfig.cloudronName),
|
2016-10-13 11:38:52 +02:00
|
|
|
text: render('welcome_user.ejs', templateDataText),
|
|
|
|
|
html: render('welcome_user.ejs', templateDataHTML)
|
2016-10-13 11:14:17 +02:00
|
|
|
};
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-10-13 11:14:17 +02:00
|
|
|
enqueue(mailOptions);
|
|
|
|
|
});
|
2016-01-18 16:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-20 12:40:54 +01:00
|
|
|
function userAdded(user, inviteSent) {
|
2016-01-18 16:11:00 +01:00
|
|
|
assert.strictEqual(typeof user, 'object');
|
2016-01-20 12:40:54 +01:00
|
|
|
assert.strictEqual(typeof inviteSent, 'boolean');
|
2016-01-18 16:11:00 +01:00
|
|
|
|
2016-01-20 12:40:54 +01:00
|
|
|
debug('Sending mail for userAdded %s including invite link', inviteSent ? 'not' : '');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2016-01-20 12:40:54 +01:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var adminEmails = _.difference(mailConfig.adminEmails, [ user.email ]);
|
2016-01-20 12:40:54 +01:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var templateData = {
|
|
|
|
|
user: user,
|
|
|
|
|
inviteLink: inviteSent ? null : config.adminOrigin() + '/api/v1/session/account/setup.html?reset_token=' + user.resetToken,
|
|
|
|
|
cloudronName: mailConfig.cloudronName,
|
|
|
|
|
cloudronAvatarUrl: config.adminOrigin() + '/api/v1/cloudron/avatar'
|
|
|
|
|
};
|
2016-10-13 12:37:25 +02:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var templateDataText = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataText.format = 'text';
|
2017-01-17 15:34:50 +01:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var templateDataHTML = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataHTML.format = 'html';
|
2017-01-17 15:34:50 +01:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: adminEmails.join(', '),
|
2018-01-21 14:50:24 +01:00
|
|
|
subject: util.format('[%s] User %s added', mailConfig.cloudronName, user.fallbackEmail),
|
2018-01-19 17:44:12 +01:00
|
|
|
text: render('user_added.ejs', templateDataText),
|
|
|
|
|
html: render('user_added.ejs', templateDataHTML)
|
|
|
|
|
};
|
2016-10-13 12:37:25 +02:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
enqueue(mailOptions);
|
2016-01-20 12:40:54 +01:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-03 01:41:47 +02:00
|
|
|
function userRemoved(user) {
|
|
|
|
|
assert.strictEqual(typeof user, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-04-03 01:41:47 +02:00
|
|
|
debug('Sending mail for userRemoved.', user.id, user.email);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-04-03 01:41:47 +02:00
|
|
|
mailUserEventToAdmins(user, 'was removed');
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-08 16:53:20 -08:00
|
|
|
function adminChanged(user, admin) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof user, 'object');
|
2016-02-08 16:53:20 -08:00
|
|
|
assert.strictEqual(typeof admin, 'boolean');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
debug('Sending mail for adminChanged');
|
|
|
|
|
|
2016-02-08 16:53:20 -08:00
|
|
|
mailUserEventToAdmins(user, admin ? 'is now an admin' : 'is no more an admin');
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function passwordReset(user) {
|
|
|
|
|
assert.strictEqual(typeof user, 'object');
|
|
|
|
|
|
2016-04-03 01:41:47 +02:00
|
|
|
debug('Sending mail for password reset for user %s.', user.email, user.id);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-10-13 16:44:09 +02:00
|
|
|
var templateData = {
|
|
|
|
|
user: user,
|
|
|
|
|
resetLink: config.adminOrigin() + '/api/v1/session/password/reset.html?reset_token=' + user.resetToken,
|
2018-01-19 17:44:12 +01:00
|
|
|
cloudronName: mailConfig.cloudronName,
|
2016-10-13 16:44:09 +02:00
|
|
|
cloudronAvatarUrl: config.adminOrigin() + '/api/v1/cloudron/avatar'
|
|
|
|
|
};
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-10-13 16:44:09 +02:00
|
|
|
var templateDataText = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataText.format = 'text';
|
|
|
|
|
|
|
|
|
|
var templateDataHTML = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataHTML.format = 'html';
|
|
|
|
|
|
|
|
|
|
var mailOptions = {
|
2018-01-19 17:44:12 +01:00
|
|
|
from: mailConfig.notificationFrom,
|
2018-01-21 14:50:24 +01:00
|
|
|
to: user.fallbackEmail,
|
2018-01-19 17:44:12 +01:00
|
|
|
subject: util.format('[%s] Password Reset', mailConfig.cloudronName),
|
2016-10-13 16:44:09 +02:00
|
|
|
text: render('password_reset.ejs', templateDataText),
|
|
|
|
|
html: render('password_reset.ejs', templateDataHTML)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function appDied(app) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
|
2017-01-17 16:02:42 +01:00
|
|
|
debug('Sending mail for app %s @ %s died', app.id, app.fqdn);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: config.provider() === 'caas' ? 'support@cloudron.io' : mailConfig.adminEmails.join(', '),
|
|
|
|
|
subject: util.format('[%s] App %s is down', mailConfig.cloudronName, app.fqdn),
|
|
|
|
|
text: render('app_down.ejs', { title: app.manifest.title, appFqdn: app.fqdn, format: 'text' })
|
|
|
|
|
};
|
2018-01-19 16:27:19 +01:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
enqueue(mailOptions);
|
2017-01-27 08:04:03 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 18:31:51 -07:00
|
|
|
function boxUpdateAvailable(hasSubscription, newBoxVersion, changelog) {
|
|
|
|
|
assert.strictEqual(typeof hasSubscription, 'boolean');
|
2017-01-27 08:04:03 -08:00
|
|
|
assert.strictEqual(typeof newBoxVersion, 'string');
|
|
|
|
|
assert(util.isArray(changelog));
|
|
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2017-01-27 08:04:03 -08:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var converter = new showdown.Converter();
|
2017-01-27 08:04:03 -08:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var templateData = {
|
|
|
|
|
webadminUrl: config.adminOrigin(),
|
|
|
|
|
newBoxVersion: newBoxVersion,
|
|
|
|
|
hasSubscription: hasSubscription,
|
|
|
|
|
changelog: changelog,
|
|
|
|
|
changelogHTML: changelog.map(function (e) { return converter.makeHtml(e); }),
|
|
|
|
|
cloudronName: mailConfig.cloudronName,
|
|
|
|
|
cloudronAvatarUrl: config.adminOrigin() + '/api/v1/cloudron/avatar'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var templateDataText = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataText.format = 'text';
|
|
|
|
|
|
|
|
|
|
var templateDataHTML = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataHTML.format = 'html';
|
|
|
|
|
|
|
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: mailConfig.adminEmails.join(', '),
|
|
|
|
|
subject: util.format('%s has a new update available', mailConfig.cloudronName),
|
|
|
|
|
text: render('box_update_available.ejs', templateDataText),
|
|
|
|
|
html: render('box_update_available.ejs', templateDataHTML)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 18:31:51 -07:00
|
|
|
function appUpdateAvailable(app, hasSubscription, info) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof app, 'object');
|
2017-11-02 18:31:51 -07:00
|
|
|
assert.strictEqual(typeof hasSubscription, 'boolean');
|
|
|
|
|
assert.strictEqual(typeof info, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var converter = new showdown.Converter();
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var templateData = {
|
|
|
|
|
webadminUrl: config.adminOrigin(),
|
|
|
|
|
hasSubscription: hasSubscription,
|
|
|
|
|
app: app,
|
|
|
|
|
updateInfo: info,
|
|
|
|
|
changelogHTML: converter.makeHtml(info.manifest.changelog),
|
|
|
|
|
cloudronName: mailConfig.cloudronName,
|
|
|
|
|
cloudronAvatarUrl: config.adminOrigin() + '/api/v1/cloudron/avatar'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var templateDataText = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataText.format = 'text';
|
|
|
|
|
|
|
|
|
|
var templateDataHTML = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataHTML.format = 'html';
|
|
|
|
|
|
|
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: mailConfig.adminEmails.join(', '),
|
|
|
|
|
subject: util.format('App %s has a new update available', app.fqdn),
|
|
|
|
|
text: render('app_update_available.ejs', templateDataText),
|
|
|
|
|
html: render('app_update_available.ejs', templateDataHTML)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 16:13:44 +02:00
|
|
|
function sendDigest(info) {
|
|
|
|
|
assert.strictEqual(typeof info, 'object');
|
|
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2017-07-21 16:13:44 +02:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var templateData = {
|
|
|
|
|
webadminUrl: config.adminOrigin(),
|
|
|
|
|
cloudronName: mailConfig.cloudronName,
|
|
|
|
|
cloudronAvatarUrl: config.adminOrigin() + '/api/v1/cloudron/avatar',
|
|
|
|
|
info: info
|
|
|
|
|
};
|
2017-11-02 14:35:16 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var templateDataText = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataText.format = 'text';
|
2017-11-02 14:35:16 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var templateDataHTML = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataHTML.format = 'html';
|
2017-11-02 14:35:16 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: mailConfig.adminEmails.join(', '),
|
|
|
|
|
subject: util.format('[%s] Cloudron - Weekly activity digest', mailConfig.cloudronName),
|
|
|
|
|
text: render('digest.ejs', templateDataText),
|
|
|
|
|
html: render('digest.ejs', templateDataHTML)
|
|
|
|
|
};
|
2017-11-02 14:35:16 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
enqueue(mailOptions);
|
2017-07-21 16:13:44 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 17:37:41 -08:00
|
|
|
function outOfDiskSpace(message) {
|
|
|
|
|
assert.strictEqual(typeof message, 'string');
|
|
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2016-01-22 17:37:41 -08:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: config.provider() === 'caas' ? 'support@cloudron.io' : mailConfig.adminEmails.join(', '),
|
|
|
|
|
subject: util.format('[%s] Out of disk space alert', mailConfig.cloudronName),
|
|
|
|
|
text: render('out_of_disk_space.ejs', { cloudronName: mailConfig.cloudronName, message: message, format: 'text' })
|
|
|
|
|
};
|
2018-01-19 16:27:19 +01:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
sendMails([ mailOptions ]);
|
2016-07-26 16:43:10 -07:00
|
|
|
});
|
2016-01-22 17:37:41 -08:00
|
|
|
}
|
|
|
|
|
|
2017-01-26 13:03:36 -08:00
|
|
|
function backupFailed(error) {
|
|
|
|
|
var message = splatchError(error);
|
|
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2016-10-14 14:46:34 -07:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: config.provider() === 'caas' ? 'support@cloudron.io' : mailConfig.adminEmails.join(', '),
|
|
|
|
|
subject: util.format('[%s] Failed to backup', mailConfig.cloudronName),
|
|
|
|
|
text: render('backup_failed.ejs', { cloudronName: mailConfig.cloudronName, message: message, format: 'text' })
|
|
|
|
|
};
|
2018-01-19 16:27:19 +01:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
enqueue(mailOptions);
|
2016-10-14 14:46:34 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-26 16:20:31 -07:00
|
|
|
function certificateRenewalError(domain, message) {
|
2016-03-19 20:40:03 -07:00
|
|
|
assert.strictEqual(typeof domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof message, 'string');
|
|
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2016-03-19 20:40:03 -07:00
|
|
|
|
2016-07-26 16:47:58 -07:00
|
|
|
var mailOptions = {
|
2018-01-19 17:44:12 +01:00
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: config.provider() === 'caas' ? 'support@cloudron.io' : mailConfig.adminEmails.join(', '),
|
2016-07-26 16:47:58 -07:00
|
|
|
subject: util.format('[%s] Certificate renewal error', domain),
|
|
|
|
|
text: render('certificate_renewal_error.ejs', { domain: domain, message: message, format: 'text' })
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sendMails([ mailOptions ]);
|
|
|
|
|
});
|
2016-03-19 20:40:03 -07:00
|
|
|
}
|
|
|
|
|
|
2017-01-07 13:52:32 -08:00
|
|
|
function oomEvent(program, context) {
|
|
|
|
|
assert.strictEqual(typeof program, 'string');
|
|
|
|
|
assert.strictEqual(typeof context, 'string');
|
|
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2017-01-07 13:52:32 -08:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig.notificationFrom,
|
|
|
|
|
to: config.provider() === 'caas' ? 'support@cloudron.io' : mailConfig.adminEmails.join(', '),
|
|
|
|
|
subject: util.format('[%s] %s exited unexpectedly', mailConfig.cloudronName, program),
|
|
|
|
|
text: render('oom_event.ejs', { cloudronName: mailConfig.cloudronName, program: program, context: context, format: 'text' })
|
|
|
|
|
};
|
2018-01-19 16:27:19 +01:00
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
sendMails([ mailOptions ]);
|
2017-01-07 13:52:32 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-10 00:24:40 -08:00
|
|
|
// this function bypasses the queue intentionally. it is also expected to work without the mailer module initialized
|
2018-01-19 19:26:45 +01:00
|
|
|
// NOTE: crashnotifier should ideally be able to send mail when there is no db, however we need the 'from' address domain from the db
|
2017-01-19 14:55:54 +01:00
|
|
|
function unexpectedExit(program, context, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof program, 'string');
|
|
|
|
|
assert.strictEqual(typeof context, 'string');
|
2017-01-19 14:55:54 +01:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2017-02-24 10:40:49 -08:00
|
|
|
if (config.provider() !== 'caas') return callback(); // no way to get admins without db access
|
|
|
|
|
|
2018-01-19 19:26:45 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2018-01-19 16:27:19 +01:00
|
|
|
|
|
|
|
|
var mailOptions = {
|
2018-01-19 19:26:45 +01:00
|
|
|
from: mailConfig.notificationFrom,
|
2018-01-19 16:27:19 +01:00
|
|
|
to: 'support@cloudron.io',
|
2018-01-19 19:26:45 +01:00
|
|
|
subject: util.format('[%s] %s exited unexpectedly', mailConfig.cloudronName, program),
|
|
|
|
|
text: render('unexpected_exit.ejs', { cloudronName: mailConfig.cloudronName, program: program, context: context, format: 'text' })
|
2018-01-19 16:27:19 +01:00
|
|
|
};
|
2016-08-02 14:40:47 -07:00
|
|
|
|
2018-01-19 16:27:19 +01:00
|
|
|
sendMails([ mailOptions ], callback);
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
2015-08-04 14:31:40 +02:00
|
|
|
|
2018-01-23 16:10:23 -08:00
|
|
|
function sendTestMail(domain, email) {
|
|
|
|
|
assert.strictEqual(typeof domain, 'string');
|
2017-09-15 14:21:52 +02:00
|
|
|
assert.strictEqual(typeof email, 'string');
|
|
|
|
|
|
2018-01-19 17:44:12 +01:00
|
|
|
getMailConfig(function (error, mailConfig) {
|
|
|
|
|
if (error) return debug('Error getting mail details:', error);
|
2017-09-15 14:21:52 +02:00
|
|
|
|
2018-01-19 16:27:19 +01:00
|
|
|
var mailOptions = {
|
2018-02-03 18:27:55 -08:00
|
|
|
from: `"${mailConfig.cloudronName}" <no-reply@${domain}>`,
|
2018-01-19 16:27:19 +01:00
|
|
|
to: email,
|
2018-01-19 17:44:12 +01:00
|
|
|
subject: util.format('Test Email from %s', mailConfig.cloudronName),
|
|
|
|
|
text: render('test.ejs', { cloudronName: mailConfig.cloudronName, format: 'text'})
|
2018-01-19 16:27:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
|
|
|
|
});
|
2017-09-15 14:21:52 +02:00
|
|
|
}
|
|
|
|
|
|
2016-01-18 14:00:35 +01:00
|
|
|
function _getMailQueue() {
|
|
|
|
|
return gMailQueue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 16:53:20 -08:00
|
|
|
function _clearMailQueue(callback) {
|
2016-01-18 14:00:35 +01:00
|
|
|
gMailQueue = [];
|
2016-02-08 16:53:20 -08:00
|
|
|
|
|
|
|
|
if (callback) callback();
|
2016-01-18 14:00:35 +01:00
|
|
|
}
|