-
Notifications
You must be signed in to change notification settings - Fork 2
/
mailer.js
39 lines (35 loc) · 1.13 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
const nodemailer = require('nodemailer');
function send(html, options = {}) {
return new Promise(function (resolve, reject) {
let smtpConfig = {
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT || 587,
secure: process.env.SMTP_SECURE == '1',
auth: {
user: process.env.SMTP_AUTH_USER,
pass: process.env.SMTP_AUTH_PASS
}
};
let transporter = nodemailer.createTransport(smtpConfig);
let subject = typeof (options.subject) == 'function' ? options.subject(html, options) : options.subject;
// setup email data with unicode symbols
let mailOptions = Object.assign({ subject }, (({ from, to, bcc }) => ({ from, to, bcc }))(options));
log.debug(mailOptions);
mailOptions.html = html;
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
let msg = 'failed sending email';
log.warn({ error }, msg);
reject(msg, error);
} else {
let msg = `Message sent: ${info.messageId}`;
resolve(msg);
log.debug(msg);
}
});
});
}
module.exports = {
send
};