blob: 2188afd44aee4fe2036821d57d06e8249cb9e364 (
plain)
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 fs = require('fs');
// const path = require('path');
const axios = require('axios');
const dayjs = require('dayjs');
const weekOfYear = require('dayjs/plugin/weekOfYear');
const handlebars = require('handlebars');
dayjs.extend(weekOfYear);
(async function () {
const campaignId = 'b03de6fc-ff95-40a0-8707-c0706b3c0b31';
const from = { name: 'Mitja Felicijan', email: 'weekly@mitjafelicijan.com' };
const headers = {
'Authorization': 'Bearer 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',
'Content-Type': 'application/json',
};
// gets current week
let campaign = null;
try {
campaign = require(`./campaigns/${dayjs().format('YYYY')}-${dayjs().week()}.json`);
} catch (err) {
console.error(err);
process.exit(1);
}
// gets list subscribers
const personalizations = [];
const contacts = await axios.get('https://api.sendgrid.com/v3/marketing/contacts', { headers: headers }).catch(error => { console.log(error) });
if (contacts) {
for (const contact of contacts.data.result) {
if (contact.list_ids.includes(campaignId)) {
personalizations.push({ to: [{ email: contact.email }] });
}
}
}
// gets handlebars template contents
let template = null;
try {
template = handlebars.compile(fs.readFileSync('template.hbs', 'utf8'));
template = template(campaign);
} catch (e) {
console.error(err);
process.exit(1);
}
// send actual emails
await axios.post('https://api.sendgrid.com/v3/mail/send', {
from,
subject: `Week #${dayjs().week()} Links`,
personalizations,
content: [{
type: 'text/html',
value: template
}]
}, { headers: headers }).catch(error => { console.log(error) });
// report
console.log(`Emailing sent to ${personalizations.length} subscribers:`)
for (const subscriber of personalizations) {
console.log(' - ', subscriber.to[0].email)
}
}());
|