2022-06-28 12:59:31 +02:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import axios from 'axios'
|
|
|
|
import { unified } from 'unified'
|
|
|
|
import markdown from 'remark-parse'
|
|
|
|
import remark2rehype from 'remark-rehype'
|
|
|
|
import format from 'rehype-format'
|
|
|
|
import html from 'rehype-stringify'
|
|
|
|
import mustache from 'mustache'
|
|
|
|
import nodemailer from 'nodemailer'
|
|
|
|
import { testers } from '../config/newsletter-testers.mjs'
|
2023-01-09 20:46:32 +01:00
|
|
|
import { fileURLToPath } from 'url'
|
2023-04-02 10:34:07 +02:00
|
|
|
import { SESv2Client, SendEmailCommand } from '@aws-sdk/client-sesv2'
|
2022-06-27 17:15:18 +02:00
|
|
|
|
2022-06-28 12:59:31 +02:00
|
|
|
// Current working directory
|
|
|
|
const cwd = path.dirname(fileURLToPath(import.meta.url))
|
|
|
|
|
2023-01-09 20:46:32 +01:00
|
|
|
const backend = 'https://backend.freesewing.org/'
|
2022-06-27 17:15:18 +02:00
|
|
|
|
|
|
|
const asHtml = async (text) => {
|
2023-01-09 20:46:32 +01:00
|
|
|
const content = await unified()
|
|
|
|
.use(markdown)
|
|
|
|
.use(remark2rehype)
|
|
|
|
.use(format)
|
|
|
|
.use(html)
|
|
|
|
.process(text)
|
2022-06-27 17:15:18 +02:00
|
|
|
|
2022-06-28 16:07:30 +02:00
|
|
|
return content.value
|
2022-06-27 17:15:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const getToken = async () => {
|
|
|
|
let res = await axios.post(`${backend}login`, {
|
|
|
|
username: process.env.FS_USER,
|
|
|
|
password: process.env.FS_PASSWORD,
|
|
|
|
})
|
|
|
|
if (res.data) return res.data.token
|
|
|
|
else if (res.err) return console.log(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const getSubscribers = async (test = true) => {
|
|
|
|
if (test) return testers
|
|
|
|
let token = await getToken()
|
|
|
|
let res = await axios.get(`${backend}admin/subscribers`, {
|
|
|
|
headers: { Authorization: 'Bearer ' + token },
|
|
|
|
})
|
|
|
|
if (res.data) return res.data
|
|
|
|
else return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const send = async (test = true) => {
|
2023-04-02 10:34:07 +02:00
|
|
|
const us = 'FreeSewing <info@freesewing.org>'
|
2022-06-28 12:59:31 +02:00
|
|
|
const template = fs.readFileSync(`${cwd}/../config/templates/newsletter.html`, 'utf8')
|
|
|
|
let edition
|
|
|
|
try {
|
2023-01-09 20:46:32 +01:00
|
|
|
edition = await axios.get(
|
|
|
|
`https://posts.freesewing.org/newsletters?slug_eq=${process.env.NL_EDITION}`,
|
|
|
|
'utf8'
|
|
|
|
)
|
2022-06-28 12:59:31 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
process.exit()
|
|
|
|
}
|
2022-06-28 16:07:30 +02:00
|
|
|
const text = edition.data[0].body
|
2022-06-28 12:59:31 +02:00
|
|
|
|
2022-06-27 17:15:18 +02:00
|
|
|
const subscribers = await getSubscribers(test)
|
|
|
|
const content = await asHtml(text)
|
|
|
|
const inject = { content }
|
|
|
|
|
2023-04-02 10:34:07 +02:00
|
|
|
// Oh AWS your APIs are such a clusterfuck
|
|
|
|
const client = new SESv2Client({ region: 'us-east-1' })
|
|
|
|
|
2022-06-27 17:15:18 +02:00
|
|
|
let i = 1
|
2023-01-09 20:46:32 +01:00
|
|
|
subscribers.sort()
|
2022-06-27 17:15:18 +02:00
|
|
|
let subs = subscribers.length
|
|
|
|
for (let sub of subscribers) {
|
|
|
|
// If your SMTP relay start rate-limiting midway through
|
|
|
|
// you can use this if loop to start just after the last
|
|
|
|
// successful delivery
|
|
|
|
if (i > 0) {
|
2022-07-02 15:44:41 +02:00
|
|
|
let unsub = `${backend}newsletter/unsubscribe/${sub.ehash}`
|
2022-06-27 17:15:18 +02:00
|
|
|
inject.unsubscribe = unsub
|
|
|
|
let body = mustache.render(template, inject)
|
|
|
|
console.log(`${i}/${subs} Sending to ${sub.email}`)
|
2023-04-02 10:34:07 +02:00
|
|
|
|
|
|
|
// Via API
|
|
|
|
const command = new SendEmailCommand({
|
|
|
|
ConfigurationSetName: 'Newsletter',
|
|
|
|
Content: {
|
|
|
|
Simple: {
|
|
|
|
Body: {
|
|
|
|
Text: {
|
|
|
|
Charset: 'utf-8',
|
|
|
|
Data: text,
|
|
|
|
},
|
|
|
|
Html: {
|
|
|
|
Charset: 'utf-8',
|
|
|
|
Data: body,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Subject: {
|
|
|
|
Charset: 'utf-8',
|
|
|
|
Data: 'FreeSewing newsletter: Spring 2023',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: {
|
|
|
|
ToAddresses: [sub.email],
|
|
|
|
},
|
|
|
|
//FeedbackForwardingEmailAddress: us,
|
|
|
|
FromEmailAddress: us,
|
|
|
|
//FromEmailAddressIdentityArn: "arn:aws:ses:us-east-1:550348293871:identity/freesewing.org",
|
|
|
|
//ReplyToAddresses: us,
|
|
|
|
})
|
|
|
|
let result
|
|
|
|
try {
|
2023-05-19 10:41:53 +02:00
|
|
|
await client.send(command)
|
2023-04-02 10:34:07 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
return false
|
|
|
|
}
|
2022-06-27 17:15:18 +02:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const sendTest = () => send(true)
|
|
|
|
const sendReal = () => send(false)
|
|
|
|
|
2023-01-09 20:46:32 +01:00
|
|
|
export { sendTest, sendReal }
|