UNPKG

2.12 kBJavaScriptView Raw
1var winston = module.parent.require('winston'),
2 Meta = module.parent.require('./meta'),
3 Mandrill,
4 Emailer = {};
5
6Emailer.init = function(app, middleware, controllers, callback) {
7
8 var render = function(req, res, next) {
9 res.render('admin/plugins/emailer-mandrill', {});
10 };
11
12 Meta.settings.get('mandrill', function(err, settings) {
13 if (!err && settings && settings.apiKey) {
14 Mandrill = require('node-mandrill')(settings.apiKey || 'Replace Me');
15 } else {
16 winston.error('[plugins/emailer-mandrill] API key not set!');
17 }
18
19 app.get('/admin/plugins/emailer-mandrill', middleware.admin.buildHeader, render);
20 app.get('/api/admin/plugins/emailer-mandrill', render);
21
22 if (typeof callback === 'function') {
23 callback();
24 }
25 });
26};
27
28Emailer.send = function(data) {
29 if (Mandrill) {
30 Mandrill('/messages/send', {
31 message: {
32 to: [{email: data.to, name: data.toName}],
33 subject: data.subject,
34 from_email: data.from,
35 html: data.html,
36 text: data.plaintext,
37 auto_text: !!!data.plaintext
38 }
39 }, function (err, response) {
40 if (!err) {
41 winston.info('[emailer.mandrill] Sent `' + data.template + '` email to uid ' + data.uid);
42 } else {
43 winston.warn('[emailer.mandrill] Unable to send `' + data.template + '` email to uid ' + data.uid + '!!');
44 winston.warn('[emailer.mandrill] Error Stringified:' + JSON.stringify(err));
45 }
46 });
47 } else {
48 winston.warn('[plugins/emailer-mandrill] API key not set, not sending email as Mandrill object is not instantiated.');
49 }
50};
51
52Emailer.admin = {
53 menu: function(custom_header, callback) {
54 custom_header.plugins.push({
55 "route": '/plugins/emailer-mandrill',
56 "icon": 'fa-envelope-o',
57 "name": 'Emailer (Mandrill)'
58 });
59
60 callback(null, custom_header);
61 }
62};
63
64module.exports = Emailer;
65
66