forked from openhab/openhab-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.js
66 lines (63 loc) · 1.8 KB
/
mailer.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
//This is an utility class to send e-mails to a user on behalf of openhab-cloud
const path = require('path'),
templatesDir = path.resolve(__dirname, '.', 'templates'),
nodemailer = require('nodemailer'),
logger = require('./logger.js'),
app = require('./app'),
productionEnv = process.env.NODE_ENV || 'dev';
const Email = require('email-templates');
if (productionEnv === 'production') {
module.exports.sendEmail = function(email, subject, templateName, locals, cb) {
let transport;
try {
let smtpConfig = {
host : app.config.mailer.host,
port : app.config.mailer.port,
secure : app.config.mailer.secureConnection, // use SSL
tls : {
rejectUnauthorized : false
}
};
if(app.config.mailer.user){
smtpConfig.auth = {
user : app.config.mailer.user,
pass : app.config.mailer.password
};
}
transport = nodemailer.createTransport(smtpConfig);
} catch (error) {
logger.error('openHAB-cloud: sendMail error occured during SMTP transport: ' + error );
}
try {
const message = {
from : app.config.mailer.from,
to : email,
subject : subject,
generateTextFromHTML : true
};
const emailsender = new Email({
views : {
root : templatesDir,
options : {
extension : 'ejs'
}
},
transport : transport
});
emailsender.send({
template : templateName,
message : message,
locals : locals
});
cb(null);
} catch (error) {
logger.error('openHAB-cloud: sendMail error occured during sending: ' + error);
}
}
} else {
logger.info('openHAB-cloud: Mailer will emulate sending in development environment');
module.exports.sendEmail = function(email, subject, templateName, locals, cb) {
logger.info('openHAB-cloud: Emulating sendEmail to ' + email + ' about ' + subject);
cb(null);
}
}