mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
Attempt to improve unsent mail
This commit is contained in:
committed by
Thomas Ruoff
parent
a462cd1c14
commit
942066bc7a
@@ -2,12 +2,11 @@ import { Booking } from '../db/booking'
|
||||
import { getBaseURL } from '../helpers/url'
|
||||
import { daysFormatFrontend } from './date'
|
||||
import { generateCalendarEntry } from './ical'
|
||||
import { retryWithDelay } from './retryWithDelay'
|
||||
import sgMail from '@sendgrid/mail'
|
||||
|
||||
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY
|
||||
const ADMIN_EMAIL = process.env.ADMIN_EMAIL
|
||||
const FROM_EMAIL = process.env.FROM_EMAIL
|
||||
const SENDGRID_URL = 'https://api.sendgrid.com/v3/mail/send'
|
||||
|
||||
if (!SENDGRID_API_KEY) {
|
||||
throw new Error('NO SENDGRID_API_KEY set!')
|
||||
@@ -21,6 +20,8 @@ if (!FROM_EMAIL) {
|
||||
throw new Error('No FROM_EMAIL set!')
|
||||
}
|
||||
|
||||
sgMail.setApiKey(SENDGRID_API_KEY)
|
||||
|
||||
const footer = `
|
||||
|
||||
Viele Grüße
|
||||
@@ -29,6 +30,7 @@ Thomas Ruoff
|
||||
Pfadi Bussle Wart
|
||||
|
||||
Tel. 0151/212 253 62
|
||||
${getBaseURL()}
|
||||
`
|
||||
|
||||
function getReceivedBookingBookerText(booking: Booking): string {
|
||||
@@ -210,36 +212,22 @@ async function sendMail({
|
||||
}[]
|
||||
}): Promise<void> {
|
||||
const data = {
|
||||
personalizations: [
|
||||
{
|
||||
to,
|
||||
},
|
||||
],
|
||||
to,
|
||||
from,
|
||||
subject,
|
||||
content: [{ type: 'text/plain', value: textPlainContent }],
|
||||
text: textPlainContent,
|
||||
attachments,
|
||||
}
|
||||
|
||||
const fetchOptions = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${SENDGRID_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
try {
|
||||
await sgMail.send(data)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
|
||||
if (error.response) {
|
||||
console.error(error.response.body)
|
||||
}
|
||||
|
||||
// TODO: stuff into DB if failed and retry later
|
||||
}
|
||||
|
||||
return retryWithDelay({
|
||||
run: async () => {
|
||||
const resp = await fetch(SENDGRID_URL, fetchOptions)
|
||||
const bodyText = await resp.text()
|
||||
|
||||
if (!resp.ok) {
|
||||
throw new Error(
|
||||
`Failed to send mail with status ${resp.status}: ${bodyText}`
|
||||
)
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user