-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlibrary.js
71 lines (58 loc) · 1.61 KB
/
library.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var winston = require.main.require('winston');
var Meta = require.main.require('./src/meta');
var Emailer = {};
var Mailjet = require('node-mailjet');
var server;
Emailer.init = function(params, callback) {
function render(req, res, next) {
res.render('admin/plugins/emailer-mailjet', {});
}
Meta.settings.get('mailjet', function(err, settings) {
if (!err && settings && settings.apiKey && settings.secretKey) {
server = Mailjet.connect(settings.apiKey, settings.secretKey);
} else {
winston.error('[plugins/emailer-mailjet] API key or SECRET Key not set!');
}
});
params.router.get('/admin/plugins/emailer-mailjet', params.middleware.admin.buildHeader, render);
params.router.get('/api/admin/plugins/emailer-mailjet', render);
callback();
};
Emailer.send = function(data, callback) {
if (!server) {
winston.error('[emailer.mailjet] Mailjet is not set up properly!')
return callback(null, data);
}
var sendEmail = server.post('send');
var emailData = {
'FromEmail': data.from,
'FromName': data.from_name,
'Subject': data.subject,
'Text-part': data.plaintext,
'Html-part': data.html,
'Recipients': [{
'Email': data.to
}]
};
sendEmail
.request(emailData)
.on('success', handleSuccess)
.on('error', handleError);
function handleSuccess(data) {
callback(null, data);
}
function handleError(err) {
callback(err);
}
};
Emailer.admin = {
menu: function(custom_header, callback) {
custom_header.plugins.push({
"route": '/plugins/emailer-mailjet',
"icon": 'fa-envelope-o',
"name": 'Emailer (Mailjet)'
});
callback(null, custom_header);
}
};
module.exports = Emailer;