-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
67 lines (63 loc) · 1.76 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
63
64
65
66
67
const nodemailer = require('nodemailer'),
creds = require('./creds'),
transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: creds.user,
pass: creds.pass,
},
}),
EmailTemplate = require('email-templates').EmailTemplate,
path = require('path'),
Promise = require('bluebird');
// I changed the emails from what's in the tutorial because people kept using
// [email protected] and sending me their test emails. :P Lesson learned. :)
//
// So yeah, change the emails below from '[email protected]' to YOUR email,
// please.
//
// Thank you!
let users = [
{
name: 'Jack',
email: '[email protected]',
},
{
name: 'John',
email: '[email protected]',
},
{
name: 'Joe',
email: '[email protected]',
},
];
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-april-2017', users).then((results) => {
return Promise.all(results.map((result) => {
sendEmail({
to: result.context.email,
from: 'Me :)',
subject: result.email.subject,
html: result.email.html,
text: result.email.text,
});
}));
}).then(() => {
console.log('Yay!');
});