aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2020-12-25 15:49:52 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2020-12-25 15:49:52 +0100
commit4c066bedb82d4b705d0e60308ceee64daf97388c (patch)
tree2090aaca1db3287b813d2f90d287abcacc49364b
parentc804725c15813e17dfc36ab4e932621948e7d262 (diff)
downloadmitjafelicijan.com-4c066bedb82d4b705d0e60308ceee64daf97388c.tar.gz
Added json template and upgraded code to allow testing
-rw-r--r--emailing/campaigns/template.json15
-rw-r--r--emailing/package.json3
-rw-r--r--emailing/send.js44
-rw-r--r--emailing/template.hbs2
4 files changed, 47 insertions, 17 deletions
diff --git a/emailing/campaigns/template.json b/emailing/campaigns/template.json
new file mode 100644
index 0000000..56b33c9
--- /dev/null
+++ b/emailing/campaigns/template.json
@@ -0,0 +1,15 @@
1{
2 "categories": [
3 {
4 "name": "General",
5 "links": [
6 {
7 "title": "",
8 "url": "",
9 "excerpt": "",
10 "source": ""
11 }
12 ]
13 }
14 ]
15}
diff --git a/emailing/package.json b/emailing/package.json
index e860f0a..6def0f4 100644
--- a/emailing/package.json
+++ b/emailing/package.json
@@ -13,6 +13,7 @@
13 "@sendgrid/mail": "^7.4.0", 13 "@sendgrid/mail": "^7.4.0",
14 "axios": "^0.21.1", 14 "axios": "^0.21.1",
15 "dayjs": "^1.9.7", 15 "dayjs": "^1.9.7",
16 "handlebars": "^4.7.6" 16 "handlebars": "^4.7.6",
17 "yesno": "^0.3.1"
17 } 18 }
18} 19}
diff --git a/emailing/send.js b/emailing/send.js
index 2188afd..3e4aa5a 100644
--- a/emailing/send.js
+++ b/emailing/send.js
@@ -4,14 +4,20 @@ const axios = require('axios');
4const dayjs = require('dayjs'); 4const dayjs = require('dayjs');
5const weekOfYear = require('dayjs/plugin/weekOfYear'); 5const weekOfYear = require('dayjs/plugin/weekOfYear');
6const handlebars = require('handlebars'); 6const handlebars = require('handlebars');
7const yesno = require('yesno');
7 8
8dayjs.extend(weekOfYear); 9dayjs.extend(weekOfYear);
9 10
10(async function () { 11(async function () {
11 12
12 const campaignId = 'b03de6fc-ff95-40a0-8707-c0706b3c0b31'; 13 //const campaignId = 'b03de6fc-ff95-40a0-8707-c0706b3c0b31'; // production
14 //const campaignId = '2bbcdedb-49d8-48f3-9f33-df6e04c9e5bf'; // testing
13 const from = { name: 'Mitja Felicijan', email: 'weekly@mitjafelicijan.com' }; 15 const from = { name: 'Mitja Felicijan', email: 'weekly@mitjafelicijan.com' };
14 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
15 const headers = { 21 const headers = {
16 'Authorization': 'Bearer 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', 22 'Authorization': 'Bearer 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',
17 'Content-Type': 'application/json', 23 'Content-Type': 'application/json',
@@ -31,7 +37,7 @@ dayjs.extend(weekOfYear);
31 const contacts = await axios.get('https://api.sendgrid.com/v3/marketing/contacts', { headers: headers }).catch(error => { console.log(error) }); 37 const contacts = await axios.get('https://api.sendgrid.com/v3/marketing/contacts', { headers: headers }).catch(error => { console.log(error) });
32 if (contacts) { 38 if (contacts) {
33 for (const contact of contacts.data.result) { 39 for (const contact of contacts.data.result) {
34 if (contact.list_ids.includes(campaignId)) { 40 if (contact.list_ids.includes(mailingList.id)) {
35 personalizations.push({ to: [{ email: contact.email }] }); 41 personalizations.push({ to: [{ email: contact.email }] });
36 } 42 }
37 } 43 }
@@ -47,21 +53,29 @@ dayjs.extend(weekOfYear);
47 process.exit(1); 53 process.exit(1);
48 } 54 }
49 55
50 // send actual emails 56 // asks for user input to allow sending emails
51 await axios.post('https://api.sendgrid.com/v3/mail/send', { 57 console.log(`\nWill send to ${personalizations.length} subscribers from list "${mailingList.env}":`)
52 from,
53 subject: `Week #${dayjs().week()} Links`,
54 personalizations,
55 content: [{
56 type: 'text/html',
57 value: template
58 }]
59 }, { headers: headers }).catch(error => { console.log(error) });
60
61 // report
62 console.log(`Emailing sent to ${personalizations.length} subscribers:`)
63 for (const subscriber of personalizations) { 58 for (const subscriber of personalizations) {
64 console.log(' - ', subscriber.to[0].email) 59 console.log(' - ', subscriber.to[0].email)
65 } 60 }
66 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
67}()); 81}());
diff --git a/emailing/template.hbs b/emailing/template.hbs
index 39dc28c..8fe22df 100644
--- a/emailing/template.hbs
+++ b/emailing/template.hbs
@@ -4,7 +4,7 @@
4 <title></title> 4 <title></title>
5 </head> 5 </head>
6 6
7 <body style="background: #f2f2f2; padding: 60px; color: #222222; line-height: 22px; font-family: sans; font-size: 16px;"> 7 <body style="background: #f2f2f2; padding: 60px; color: #222222; line-height: 22px; font-family: sans-serif, system-ui, -apple-system; font-size: 16px;">
8 8
9 <div style="width: 650px; background: #ffffff; text-align: left; padding: 40px; margin: 0 auto; border-radius: 10px;"> 9 <div style="width: 650px; background: #ffffff; text-align: left; padding: 40px; margin: 0 auto; border-radius: 10px;">
10 10