-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_sendgrid.js
37 lines (35 loc) · 1.04 KB
/
send_sendgrid.js
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
const sgMail = require('@sendgrid/mail')
const fs = require('fs')
var request = require('request')
const moment = require('moment')
const html = fs.readFileSync(__dirname + '/public/index.html', 'utf8')
var today = moment().subtract(1, 'day')
sgMail.setApiKey(`${process.env.SENDGRID_API_KEY}`)
request(
{
method: 'GET',
url: 'https://api.sendgrid.com/v3/marketing/contacts',
headers: { authorization: `Bearer ${process.env.SENDGRID_API_KEY}` },
},
function (error, response, body) {
if (error) throw new Error(error)
let json = JSON.parse(body)
let { result: contacts } = json
let personalizations = contacts.map((x) => ({
to: {
email: x.email,
},
}))
const msg = {
personalizations,
from: {
email: '[email protected]',
name: `Current Events`,
},
subject: `Current Events: ${today.format('dddd DD MMMM YYYY')}`,
text: 'Text not available. See latest current events at https://currentevents.email',
html,
}
sgMail.send(msg)
}
)