UNPKG

@bitblit/ratchet-aws

Version:

Common tools for use with AWS browser and node

68 lines 2.69 kB
import { RequireRatchet } from '@bitblit/ratchet-common/lang/require-ratchet'; import { Logger } from '@bitblit/ratchet-common/logger/logger'; import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet'; import { SendRawEmailCommand } from '@aws-sdk/client-ses'; import { DateTime } from 'luxon'; import { MailerUtil } from '@bitblit/ratchet-common/mail/mailer-util'; export class SesMailSendingProvider { _ses; _archiveRatchet; archivePrefix; constructor(_ses, _archiveRatchet, archivePrefix) { this._ses = _ses; this._archiveRatchet = _archiveRatchet; this.archivePrefix = archivePrefix; RequireRatchet.notNullOrUndefined(this._ses); if (!!_archiveRatchet && !_archiveRatchet.getDefaultBucket()) { throw new Error('If archiveRatchet specified, must set a default bucket'); } } async archiveEmail(mail, _rawSendResult) { let rval = null; if (this._archiveRatchet) { Logger.debug('Archiving outbound email to : %j', mail.destinationAddresses); let targetPath = StringRatchet.trimToEmpty(this.archivePrefix); if (!targetPath.endsWith('/')) { targetPath += '/'; } const now = DateTime.utc(); targetPath += 'year=' + now.toFormat('yyyy') + '/month=' + now.toFormat('MM') + '/day=' + now.toFormat('dd') + '/hour=' + now.toFormat('HH') + '/' + now.toFormat('mm_ss__SSS'); targetPath += '.json'; try { rval = await this._archiveRatchet.writeObjectToCacheFile(targetPath, mail); } catch (err) { Logger.warn('Failed to archive email %s %j : %s', targetPath, mail, err); } } return rval; } get sesClient() { return this._ses; } get archiveRatchet() { return this._archiveRatchet; } async sendEmail(inRts) { RequireRatchet.notNullOrUndefined(inRts, 'RTS must be defined'); RequireRatchet.notNullOrUndefined(inRts.destinationAddresses, 'Destination addresses must be defined'); let rval = null; const rawMail = MailerUtil.convertResolvedReadyToSendEmailToRaw(inRts); const params = { RawMessage: { Data: new TextEncoder().encode(rawMail) }, }; rval = await this._ses.send(new SendRawEmailCommand(params)); return rval; } } //# sourceMappingURL=ses-mail-sending-provider.js.map