aboutsummaryrefslogtreecommitdiff
path: root/emailing/send.js
diff options
context:
space:
mode:
Diffstat (limited to 'emailing/send.js')
-rw-r--r--emailing/send.js81
1 files changed, 0 insertions, 81 deletions
diff --git a/emailing/send.js b/emailing/send.js
deleted file mode 100644
index 3e4aa5a..0000000
--- a/emailing/send.js
+++ /dev/null
@@ -1,81 +0,0 @@
1const fs = require('fs');
2// const path = require('path');
3const axios = require('axios');
4const dayjs = require('dayjs');
5const weekOfYear = require('dayjs/plugin/weekOfYear');
6const handlebars = require('handlebars');
7const yesno = require('yesno');
8
9dayjs.extend(weekOfYear);
10
11(async function () {
12
13 //const campaignId = 'b03de6fc-ff95-40a0-8707-c0706b3c0b31'; // production
14 //const campaignId = '2bbcdedb-49d8-48f3-9f33-df6e04c9e5bf'; // testing
15 const from = { name: 'Mitja Felicijan', email: 'weekly@mitjafelicijan.com' };
16
17 const mailingList = process.argv[2] == 'production'
18 ? { env: 'production', id: 'b03de6fc-ff95-40a0-8707-c0706b3c0b31' }
19 : { env: 'testing', id: '2bbcdedb-49d8-48f3-9f33-df6e04c9e5bf' };
20
21 const headers = {
22 'Authorization': 'Bearer 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',
23 'Content-Type': 'application/json',
24 };
25
26 // gets current week
27 let campaign = null;
28 try {
29 campaign = require(`./campaigns/${dayjs().format('YYYY')}-${dayjs().week()}.json`);
30 } catch (err) {
31 console.error(err);
32 process.exit(1);
33 }
34
35 // gets list subscribers
36 const personalizations = [];
37 const contacts = await axios.get('https://api.sendgrid.com/v3/marketing/contacts', { headers: headers }).catch(error => { console.log(error) });
38 if (contacts) {
39 for (const contact of contacts.data.result) {
40 if (contact.list_ids.includes(mailingList.id)) {
41 personalizations.push({ to: [{ email: contact.email }] });
42 }
43 }
44 }
45
46 // gets handlebars template contents
47 let template = null;
48 try {
49 template = handlebars.compile(fs.readFileSync('template.hbs', 'utf8'));
50 template = template(campaign);
51 } catch (e) {
52 console.error(err);
53 process.exit(1);
54 }
55
56 // asks for user input to allow sending emails
57 console.log(`\nWill send to ${personalizations.length} subscribers from list "${mailingList.env}":`)
58 for (const subscriber of personalizations) {
59 console.log(' - ', subscriber.to[0].email)
60 }
61
62 const consent = await yesno({
63 question: '\nAre you sure you want to continue?'
64 });
65
66 if (consent) {
67 // send actual emails
68 await axios.post('https://api.sendgrid.com/v3/mail/send', {
69 from,
70 subject: `Week #${dayjs().week()} Links`,
71 personalizations,
72 content: [{
73 type: 'text/html',
74 value: template
75 }]
76 }, { headers: headers }).catch(error => { console.log(error) });
77 }
78
79 console.log('\nAnd we are done.\n');
80
81}());