aboutsummaryrefslogtreecommitdiff
path: root/emailing/send.js
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 /emailing/send.js
parentc804725c15813e17dfc36ab4e932621948e7d262 (diff)
downloadmitjafelicijan.com-4c066bedb82d4b705d0e60308ceee64daf97388c.tar.gz
Added json template and upgraded code to allow testing
Diffstat (limited to 'emailing/send.js')
-rw-r--r--emailing/send.js44
1 files changed, 29 insertions, 15 deletions
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}());