-
Notifications
You must be signed in to change notification settings - Fork 0
/
emails.js
62 lines (47 loc) · 1.42 KB
/
emails.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 db = require('./db');
const crypto = require('crypto');
let mailgunApiKey = process.env.MAILGUN_API_KEY;
let mailgun = require('mailgun-js')({apiKey: mailgunApiKey, domain: 'mg.miastojestnasze.org'});
let baseUrl = process.env.PAGE_URL;
let message = `Droga koleżanko, drogi kolego,
Poniżej znajdziesz swój osobisty link do ankiety.
Z poważaniem,
Zarząd MJN
****
`;
if (process.argv.length < 2) {
console.log('Please provide emails');
process.exit();
}
let emails = process.argv[process.argv.length-1].split(',');
async function sendEmails(db) {
// Generate tokens
let tokens = emails.map((e) => crypto.randomBytes(6).toString('hex'));
// Remove existing tokens and add new
await db.collection('tokens').remove();
await db.collection('tokens').insertMany(tokens.map((token) => ({_id: token})));
// Send emails with tokens
return await Promise.all(emails.map((email, index) => {
let data = {
from: 'Miasto Jest Nasze <[email protected]>',
to: email,
subject: 'Link do ankiety',
text: message + baseUrl + '/?token=' + tokens[index]
};
console.log('Sending email to %s', email);
return mailgun.messages().send(data);
}));
}
db.connect(function(err, db){
if (err) {
console.error(err);
process.exit();
}
sendEmails(db).then(function(res){
console.log(res);
process.exit();
}, function(err) {
console.log(err);
process.exit();
});
});