forked from NodeBB/nodebb-plugin-emailer-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (62 loc) · 1.95 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
72
73
74
75
76
'use strict';
var winston = module.parent.require('winston'),
Meta = module.parent.require('./meta'),
nodemailer = require('nodemailer'),
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 username = settings['username'];
var pass = settings['password'];
var transportOptions = {
host: settings['host'],
port: parseInt(settings['port'], 10),
secure: settings['secure'] === 'on'
};
if( username || pass ) {
// transportOptions.authMethod = 'LOGIN';
transportOptions.auth = {
user: username,
pass: pass
};
}
var transport = nodemailer.createTransport(transportOptions);
transport.sendMail({
from: data.from,
to: data.to,
html: data.html,
text: data.plaintext,
subject: data.subject
},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 (Local)'
});
callback(null, custom_header);
}
};
module.exports = Emailer;