-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (57 loc) · 1.75 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
const nodemailer = require('nodemailer'),
creds = require('./creds.js'),
transporter = nodemailer.createTransport({
pool: true,
service: 'gmail',
secure: false,
auth: {
user: creds.email,
pass: creds.password
}
}),
EmailTemplate = require('email-templates').EmailTemplate,
path = require('path'),
Promise = require('bluebird');
let users = [
{
name: 'NAME_OF_PERSON',
email: 'EMAIL_OF_PERSON',
filename: 'FILENAME_OF ATTACHMENT_IF_ANY', //Delete this and next line in case of no attachments
path: 'path/to/file/with/extension'
}
]
function sendEmail(obj) {
return transporter.sendMail(obj);
}
function loadTemplate(templateName, contexts){
let template = new EmailTemplate(path.join(__dirname, 'templates', templateName));
return Promise.all(contexts.map((context) => {
return new Promise((resolve, reject) => {
template.render(context, (err, result) => {
if (err) reject(err);
else resolve({
email: result,
context,
});
})
});
}));
}
loadTemplate('updates', users).then((results) => {
return Promise.all(results.map((result) => {
sendEmail({
to: result.context.email,
from: 'COMPANY_OR_PERSON_NAME',
subject: result.email.subject,
html: result.email.html,
text: result.email.text,
//Uncomment in case of attachements
// attachments: {
// filename: result.context.filename,
// path: result.context.path
// }
});
}))
}).then(() => {
console.log("Done");
})