1 | var fs = require('fs'),
|
2 | path = require('path'),
|
3 |
|
4 | winston = module.parent.require('winston'),
|
5 | Meta = module.parent.require('./meta'),
|
6 |
|
7 | Mandrill = require('node-mandrill')(Meta.config['mandrill:apiKey'] || 'Replace Me'),
|
8 | Emailer = {};
|
9 |
|
10 | Emailer.send = function(data) {
|
11 |
|
12 | Mandrill('/messages/send', {
|
13 | message: {
|
14 | to: [{email: data.to, name: data.toName}],
|
15 | subject: data.subject,
|
16 | from_email: data.from,
|
17 | html: data.html,
|
18 | text: data.plaintext,
|
19 | auto_text: !!!data.plaintext
|
20 | }
|
21 | }, function (err, response) {
|
22 | if (!err) {
|
23 | winston.info('[emailer.mandrill] Sent `' + data.template + '` email to uid ' + data.uid);
|
24 | } else {
|
25 | winston.warn('[emailer.mandrill] Unable to send `' + data.template + '` email to uid ' + data.uid + '!!');
|
26 | winston.error('[emailer.mandrill] ' + err);
|
27 | winston.error('[emailer.mandrill] ' + response);
|
28 | }
|
29 | });
|
30 | };
|
31 |
|
32 | Emailer.admin = {
|
33 | menu: function(custom_header, callback) {
|
34 | custom_header.plugins.push({
|
35 | "route": '/plugins/emailer-mandrill',
|
36 | "icon": 'fa-envelope-o',
|
37 | "name": 'Emailer (Mandrill)'
|
38 | });
|
39 |
|
40 | return custom_header;
|
41 | },
|
42 | route: function(custom_routes, callback) {
|
43 | fs.readFile(path.join(__dirname, 'public/templates/admin.tpl'), function(err, tpl) {
|
44 | custom_routes.routes.push({
|
45 | route: '/plugins/emailer-mandrill',
|
46 | method: "get",
|
47 | options: function(req, res, callback) {
|
48 | callback({
|
49 | req: req,
|
50 | res: res,
|
51 | route: '/plugins/emailer-mandrill',
|
52 | name: 'Emailer (Mandrill)',
|
53 | content: tpl
|
54 | });
|
55 | }
|
56 | });
|
57 |
|
58 | callback(null, custom_routes);
|
59 | });
|
60 | }
|
61 | };
|
62 |
|
63 | module.exports = Emailer;
|