// Copyright Abridged, Inc. 2023,2024. All Rights Reserved. // Node module: @collabland/aws // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT import { SESClient, SESClientConfig, SendTemplatedEmailCommand, } from '@aws-sdk/client-ses'; import {getEnvVar, loggers} from '@collabland/common'; import {AnyObject} from '@loopback/repository'; import {getDefaultConfig} from '../common/types.js'; const {trace, error} = loggers('collabland:aws:ses'); export class SEServiceClient { readonly ses: SESClient; constructor(options?: SESClientConfig) { this.ses = new SESClient({ ...getDefaultConfig(), region: 'us-east-1', //getAWSRegion(), // because email-client is setup in us-east-1 ...options, }); } async sendEmail( to: string | string[], templateName: string, templateData: AnyObject, ) { const input = { Source: 'James from Collab.Land ', ReplyToAddresses: [getEnvVar('COLLABLAND_EMAIL')!], // Replace with your verified sender email address Destination: { ToAddresses: typeof to === 'string' ? [to] : to, // Replace with the recipient's email address }, Template: templateName, // Replace with the name of the template you created in SES TemplateData: JSON.stringify(templateData), }; trace('Sending Email: %O', input); const cmd = new SendTemplatedEmailCommand(input); try { const result = await this.ses.send(cmd); return result; } catch (err) { error('Fail to send email %s to %s: %O', templateData, to, err); throw err; } } }