2023-04-01 15:53:09 +08:00
|
|
|
import type { EmailContent } from '@aws-sdk/client-sesv2';
|
|
|
|
import { SendEmailCommand, SESv2Client } from '@aws-sdk/client-sesv2';
|
|
|
|
import type { AwsCredentialIdentity } from '@aws-sdk/types';
|
2024-01-25 19:44:20 +08:00
|
|
|
import { replaceSendMessageHandlebars, type SendMessagePayload } from '@logto/connector-kit';
|
2023-04-01 15:53:09 +08:00
|
|
|
|
2024-01-25 19:44:20 +08:00
|
|
|
import type { AwsSesConfig, Template } from './types.js';
|
2023-04-01 15:53:09 +08:00
|
|
|
|
|
|
|
export const makeClient = (
|
|
|
|
accessKeyId: string,
|
|
|
|
secretAccessKey: string,
|
|
|
|
region: string
|
|
|
|
): SESv2Client => {
|
|
|
|
const credentials: AwsCredentialIdentity = {
|
|
|
|
accessKeyId,
|
|
|
|
secretAccessKey,
|
|
|
|
};
|
|
|
|
|
|
|
|
return new SESv2Client({ credentials, region });
|
|
|
|
};
|
|
|
|
|
2024-01-25 19:44:20 +08:00
|
|
|
export const makeEmailContent = (template: Template, payload: SendMessagePayload): EmailContent => {
|
2023-04-01 15:53:09 +08:00
|
|
|
return {
|
|
|
|
Simple: {
|
2024-01-25 19:44:20 +08:00
|
|
|
Subject: { Data: replaceSendMessageHandlebars(template.subject, payload), Charset: 'utf8' },
|
2023-04-01 15:53:09 +08:00
|
|
|
Body: {
|
|
|
|
Html: {
|
2024-01-25 19:44:20 +08:00
|
|
|
Data: replaceSendMessageHandlebars(template.content, payload),
|
2023-04-01 15:53:09 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const makeCommand = (
|
|
|
|
config: AwsSesConfig,
|
|
|
|
emailContent: EmailContent,
|
|
|
|
to: string
|
|
|
|
): SendEmailCommand => {
|
|
|
|
return new SendEmailCommand({
|
|
|
|
Destination: { ToAddresses: [to] },
|
|
|
|
Content: emailContent,
|
|
|
|
FromEmailAddress: config.emailAddress,
|
|
|
|
FromEmailAddressIdentityArn: config.emailAddressIdentityArn,
|
|
|
|
FeedbackForwardingEmailAddress: config.feedbackForwardingEmailAddress,
|
|
|
|
FeedbackForwardingEmailAddressIdentityArn: config.feedbackForwardingEmailAddressIdentityArn,
|
|
|
|
ConfigurationSetName: config.configurationSetName,
|
|
|
|
});
|
|
|
|
};
|