-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmail.js
More file actions
94 lines (80 loc) · 2.27 KB
/
mail.js
File metadata and controls
94 lines (80 loc) · 2.27 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use strict';
const nodemailer = require('nodemailer');
var utils = require('./utils');
const config = utils.getConfig();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: 'sparkpostmail',
host: 'smtp.sparkpostmail.com',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: config.smtp_usr,
pass: config.smtp_key
}
});
// setup email data with unicode symbols
let mailOptions = {
from: config.smtp_from, // sender address
};
function sendPlainMail(subject, message, to) {
if(Array.isArray(to))
to = to.join(',');
// setup email data
mailOptions.subject = subject;
mailOptions.text = message;
mailOptions.to = to;
// send mail with defined transport object
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
return reject(error);
} else {
console.log('Message sent: %s', info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
resolve(info);
}
});
});
}
function sendWithTemplate(subject, data, to, template, attachment) {
if(Array.isArray(to))
to = to.join(',');
//reference the plugin
var hbs = require('nodemailer-express-handlebars');
var exphbs = require('express-handlebars');
var engine = exphbs();
var options =
{
viewEngine: engine,
viewPath: 'views'
}
//attach the plugin to the nodemailer transporter
transporter.use('compile', hbs(options));
//send mail with options
mailOptions.subject = subject;
mailOptions.to = to;
mailOptions.template = template;
mailOptions.context = data;
if (attachment) {
mailOptions.attachments = [attachment]
}
// send mail with defined transport object
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
return reject(error);
} else {
console.log('Message sent: %s', info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
resolve(info);
}
});
});
}
module.exports = {
sendPlainMail: sendPlainMail,
sendWithTemplate: sendWithTemplate
};