|
1
|
|
|
const nodemailer = require('nodemailer') |
|
|
|
|
|
|
2
|
|
|
const Router = require('express') |
|
|
|
|
|
|
3
|
|
|
const config = require('../config') |
|
|
|
|
|
|
4
|
|
|
const logger = require('winston') |
|
|
|
|
|
|
5
|
|
|
var rp = require('request-promise') |
|
6
|
|
|
var ok = require('./utils').ok |
|
7
|
|
|
var fail = require('./utils').fail |
|
8
|
|
|
|
|
9
|
|
|
module.exports = class MessageController { |
|
10
|
|
|
constructor () { |
|
11
|
|
|
this.smtpConfig = { |
|
12
|
|
|
host: config.mail.server, |
|
|
|
|
|
|
13
|
|
|
port: config.mail.port, |
|
14
|
|
|
secure: true, // use SSL |
|
15
|
|
|
auth: { |
|
16
|
|
|
user: config.mail.username, |
|
17
|
|
|
pass: config.mail.password |
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
sendSMS (sms) { |
|
23
|
|
|
var data = { |
|
24
|
|
|
'username': config.sms.username, |
|
|
|
|
|
|
25
|
|
|
'password': config.sms.password, |
|
26
|
|
|
'function': 'sendSms', |
|
27
|
|
|
'number': sms.number, |
|
28
|
|
|
'message': sms.message, |
|
29
|
|
|
'senderid': 'GrogBurners' |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return rp('https://www.my-cool-sms.com/api-socket.php', { json: true, body: data }) |
|
33
|
|
|
.then((resp) => { |
|
34
|
|
|
if (resp.hasOwnProperty('errorcode')) { |
|
35
|
|
|
logger.error('Send SMS failed: ' + resp.description) |
|
|
|
|
|
|
36
|
|
|
throw new Error('SMS Send failed!') |
|
37
|
|
|
} else { |
|
38
|
|
|
logger.info('SMS Sent to: ' + JSON.Stringify(resp)) |
|
39
|
|
|
logger.debug('SMS Sent: ' + JSON.Stringify(resp)) |
|
40
|
|
|
return { message: 'Message sent' } |
|
41
|
|
|
} |
|
42
|
|
|
}) |
|
43
|
|
|
/* .catch((err) => { |
|
44
|
|
|
return new Error('Error creating message: ' + err) |
|
45
|
|
|
}); */ |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
sendTestEmail (body) { |
|
49
|
|
|
const mailOptions = { |
|
50
|
|
|
from: '"' + config.mail.from.name + '" <' + config.mail.from.address + '>', // sender address |
|
|
|
|
|
|
51
|
|
|
to: body.to, // list of receivers |
|
52
|
|
|
subject: body.subject, // Subject line |
|
53
|
|
|
text: body.text, // plaintext body |
|
54
|
|
|
html: body.html // html body |
|
55
|
|
|
} |
|
56
|
|
|
return this.send(mailOptions) |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
sendEmail (email) { |
|
60
|
|
|
const transporter = nodemailer.createTransport(this.smtpConfig) |
|
|
|
|
|
|
61
|
|
|
return transporter.sendMail(email) |
|
62
|
|
|
.then(() => { |
|
63
|
|
|
return { message: 'Message sent' } |
|
64
|
|
|
}) |
|
65
|
|
|
/* .catch((err) => { |
|
66
|
|
|
return new Error('Error creating message: ' + err) |
|
67
|
|
|
}); */ |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
routeAPI () { |
|
71
|
|
|
const router = new Router() |
|
|
|
|
|
|
72
|
|
|
router.post('/email', (req, res) => { |
|
73
|
|
|
this |
|
74
|
|
|
.sendEmail(req.body) |
|
75
|
|
|
.then(ok(res)) |
|
76
|
|
|
.then(null, fail(res)) |
|
77
|
|
|
}) |
|
78
|
|
|
|
|
79
|
|
|
router.post('/sms', (req, res) => { |
|
80
|
|
|
this |
|
81
|
|
|
.sendSMS(req.body) |
|
82
|
|
|
.then(ok(res)) |
|
83
|
|
|
.then(null, fail(res)) |
|
84
|
|
|
}) |
|
85
|
|
|
|
|
86
|
|
|
return router |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|