shellphone.app/integrations/aws-ses.ts
2021-10-31 16:16:29 +01:00

47 lines
1.0 KiB
TypeScript

import type { SendEmailRequest } from "aws-sdk/clients/ses";
import { Credentials, SES } from "aws-sdk";
import { getConfig } from "blitz";
const { serverRuntimeConfig } = getConfig();
const credentials = new Credentials({
accessKeyId: serverRuntimeConfig.awsSes.accessKeyId,
secretAccessKey: serverRuntimeConfig.awsSes.secretAccessKey,
});
const ses = new SES({ region: serverRuntimeConfig.awsSes.awsRegion, credentials });
type SendEmailParams = {
text?: string;
html: string;
subject: string;
recipients: string[];
};
export async function sendEmail({ text, html, subject, recipients }: SendEmailParams) {
const request: SendEmailRequest = {
Destination: { ToAddresses: recipients },
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: html,
},
},
Subject: {
Charset: "UTF-8",
Data: subject,
},
},
Source: serverRuntimeConfig.awsSes.fromEmail,
};
if (text) {
request.Message.Body.Text = {
Charset: "UTF-8",
Data: text,
};
}
await ses.sendEmail(request).promise();
}