forked from Danor/nodebb-plugin-emailer-smtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (59 loc) · 1.94 KB
/
index.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
'use strict';
var winston = module.parent.require('winston'),
Meta = module.parent.require('./meta'),
nodemailer = require('nodemailer'),
smtpTransport = require('nodemailer-smtp-transport'),
Emailer = {};
var settings = {};
Emailer.init = function(data, callback) {
function renderAdminPage(req, res) {
res.render('admin/emailers/local', {});
}
data.router.get('/admin/emailers/local', data.middleware.admin.buildHeader, renderAdminPage);
data.router.get('/api/admin/emailers/local', renderAdminPage);
Meta.settings.get('emailer-local', function(err, _settings) {
if (err) {
return winston.error(err);
}
settings = _settings;
});
callback();
};
Emailer.send = function(data, callback) {
var smtpConfig = {
host: settings['emailer:local:host'],
port: settings['emailer:local:port'],
secure: (settings['emailer:local:secure'] !== "off"),
auth: {
user: settings['emailer:local:username'],
pass: settings['emailer:local:password'],
},
};
var mailOptions = {
from: data.from,
to: data.to,
html: data.html,
text: data.plaintext,
subject: data.subject
};
var transporter = nodemailer.createTransport(smtpConfig);
transporter.sendMail(mailOptions, function(err) {
if ( !err ) {
winston.info('[emailer.smtp] Sent `' + data.template + '` email to uid ' + data.uid);
} else {
winston.warn('[emailer.smtp] Unable to send `' + data.template + '` email to uid ' + data.uid + '!');
}
callback(err, data);
});
};
Emailer.admin = {
menu: function(custom_header, callback) {
custom_header.plugins.push({
"route": '/emailers/local',
"icon": 'fa-envelope-o',
"name": 'Emailer SMTP'
});
callback(null, custom_header);
}
};
module.exports = Emailer;