Files
the-biergarten-app/src/config/sparkpost/sendEmail.ts
Aaron William Po b939219c67 Fix: update dependencies, remove sparkpost-node
This commit removes the sparkpost-node and switches to using a raw api call using fetch. This commit also updates the dependency react-email.
2023-11-12 18:28:26 -05:00

36 lines
866 B
TypeScript

import { SPARKPOST_API_KEY, SPARKPOST_SENDER_ADDRESS } from '../env';
interface EmailParams {
address: string;
text: string;
html: string;
subject: string;
}
const sendEmail = async ({ address, text, html, subject }: EmailParams) => {
const from = SPARKPOST_SENDER_ADDRESS;
const data = {
recipients: [{ address }],
content: { from, subject, text, html },
};
const transmissionsEndpoint = 'https://api.sparkpost.com/api/v1/transmissions';
const response = await fetch(transmissionsEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: SPARKPOST_API_KEY,
},
body: JSON.stringify(data),
});
if (response.status !== 200) {
throw new Error(`Sparkpost API returned status code ${response.status}`);
}
};
export default sendEmail;