From 4c066bedb82d4b705d0e60308ceee64daf97388c Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Fri, 25 Dec 2020 15:49:52 +0100 Subject: Added json template and upgraded code to allow testing --- emailing/campaigns/template.json | 15 ++++++++++++++ emailing/package.json | 3 ++- emailing/send.js | 44 ++++++++++++++++++++++++++-------------- emailing/template.hbs | 2 +- 4 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 emailing/campaigns/template.json (limited to 'emailing') 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 @@ +{ + "categories": [ + { + "name": "General", + "links": [ + { + "title": "", + "url": "", + "excerpt": "", + "source": "" + } + ] + } + ] +} 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 @@ "@sendgrid/mail": "^7.4.0", "axios": "^0.21.1", "dayjs": "^1.9.7", - "handlebars": "^4.7.6" + "handlebars": "^4.7.6", + "yesno": "^0.3.1" } } 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'); const dayjs = require('dayjs'); const weekOfYear = require('dayjs/plugin/weekOfYear'); const handlebars = require('handlebars'); +const yesno = require('yesno'); dayjs.extend(weekOfYear); (async function () { - const campaignId = 'b03de6fc-ff95-40a0-8707-c0706b3c0b31'; + //const campaignId = 'b03de6fc-ff95-40a0-8707-c0706b3c0b31'; // production + //const campaignId = '2bbcdedb-49d8-48f3-9f33-df6e04c9e5bf'; // testing const from = { name: 'Mitja Felicijan', email: 'weekly@mitjafelicijan.com' }; + const mailingList = process.argv[2] == 'production' + ? { env: 'production', id: 'b03de6fc-ff95-40a0-8707-c0706b3c0b31' } + : { env: 'testing', id: '2bbcdedb-49d8-48f3-9f33-df6e04c9e5bf' }; + const headers = { 'Authorization': 'Bearer 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', 'Content-Type': 'application/json', @@ -31,7 +37,7 @@ dayjs.extend(weekOfYear); 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)) { + if (contact.list_ids.includes(mailingList.id)) { personalizations.push({ to: [{ email: contact.email }] }); } } @@ -47,21 +53,29 @@ dayjs.extend(weekOfYear); 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:`) + // asks for user input to allow sending emails + console.log(`\nWill send to ${personalizations.length} subscribers from list "${mailingList.env}":`) for (const subscriber of personalizations) { console.log(' - ', subscriber.to[0].email) } + const consent = await yesno({ + question: '\nAre you sure you want to continue?' + }); + + if (consent) { + // 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) }); + } + + console.log('\nAnd we are done.\n'); + }()); 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 @@ - +
-- cgit v1.2.3