-
Notifications
You must be signed in to change notification settings - Fork 1
/
ses_sendemail.js
72 lines (65 loc) · 2.03 KB
/
ses_sendemail.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk')
const moment = require('moment')
const { createClient } = require('@supabase/supabase-js')
const fs = require('fs')
const html = fs.readFileSync(__dirname + '/public/index.html', 'utf8')
var today = moment().subtract(1, 'day')
const SUPABASE_URL = process.env.SUPABASE_URL.toString().trim()
const SUPABASE_SERVICE_KEY = process.env.SUPABASE_SERVICE_KEY.toString().trim()
const supabase = createClient(SUPABASE_URL, SUPABASE_SERVICE_KEY)
AWS.config.update({ region: 'us-east-1' })
async function fetchEmails() {
const { data, error } = await supabase.from('subscribers').select('email')
if (error) {
throw new Error(error.message)
}
return data.map((x) => x.email)
}
async function sendEmail() {
try {
// const Source = '[email protected]'
const Source = 'Current Events <[email protected]>'
const ReplyToAddresses = ['[email protected]']
const BccAddresses = await fetchEmails()
// Create sendEmail params
var params = {
Destination: {
BccAddresses,
},
Source,
ReplyToAddresses,
Message: {
/* required */
Body: {
/* required */
Html: {
Charset: 'UTF-8',
Data: html,
},
Text: {
Charset: 'UTF-8',
Data: 'Text not available. See latest current events at https://currentevents.email',
},
},
Subject: {
Charset: 'UTF-8',
Data: `Current Events: ${today.format('dddd DD MMMM YYYY')}`,
},
},
}
// Create the promise and SES service object
var sendPromise = new AWS.SES({ apiVersion: '2010-12-01' }).sendEmail(params).promise()
// Handle promise's fulfilled/rejected states
sendPromise
.then(function (data) {
console.log(data.MessageId)
})
.catch(function (err) {
console.error(err, err.stack)
})
} catch (error) {
console.log('error.message', error.message)
}
}
sendEmail()