import { Injectable } from '@travetto/di'; import { RuntimeResources } from '@travetto/runtime'; import type { MailInterpolator } from './template.ts'; import type { MailTransport } from './transport.ts'; import type { EmailCompiled, EmailOptions, SentEmail } from './types.ts'; import { MailUtil } from './util.ts'; type MessageWithoutBody = Omit; /** * Email service for sending and templating emails */ @Injectable() export class MailService { #compiled = new Map(); #transport: MailTransport; #interpolator: MailInterpolator; #cacheResults = true; constructor(transport: MailTransport, interpolator: MailInterpolator) { this.#interpolator = interpolator; this.#transport = transport; } setCacheState(active: boolean): void { this.#cacheResults = active; } /** * Get compiled content by key */ async getCompiled(key: string): Promise { if (!this.#compiled.has(key)) { const [html, text, subject] = await Promise.all( [ RuntimeResources.readUTF8(`${key}.compiled.html`), RuntimeResources.readUTF8(`${key}.compiled.text`), RuntimeResources.readUTF8(`${key}.compiled.subject`) ].map(file => file.then(MailUtil.purgeBrand)) ); if (this.#cacheResults) { this.#compiled.set(key, { html, text, subject }); } } return this.#compiled.get(key)!; } /** * Build message from key/context * @param keyOrMessage * @param ctx * @returns */ async renderMessage(keyOrMessage: string | EmailCompiled | EmailOptions, ctx: Record): Promise { const template = typeof keyOrMessage === 'string' ? await this.getCompiled(keyOrMessage) : keyOrMessage; const [html, text, subject] = await Promise.all([ this.#interpolator.render(template.html, ctx), this.#interpolator.render(template.text ?? '', ctx), this.#interpolator.render(template.subject, ctx) ]); return { html, text, subject }; } /** * Send a single message */ async send( message: Pick, key: string, ctx?: Record, base?: MessageWithoutBody ): Promise; async send(message: EmailOptions): Promise; async send( message: EmailOptions | Pick, key?: string, ctx?: Record, base?: MessageWithoutBody ): Promise { const keyOrMessage = key ?? ('html' in message ? message : '') ?? ''; const context = ctx ?? ('context' in message ? message.context : {}) ?? {}; const compiled = await this.renderMessage(keyOrMessage, context); const final = { ...base, ...message, ...compiled, context }; // Extract images if (compiled.html) { const { html, attachments } = await MailUtil.extractImageAttachments(compiled.html); final.html = html; final.attachments = [...attachments, ...(final.attachments ?? [])]; } // Disable threading if desired, provide a unique message id and a unique reply-to if ('disableThreading' in message && message.disableThreading) { const id = MailUtil.buildUniqueMessageId(message); final.headers = { 'In-Reply-To': id, References: id, 'X-Message-Id': id, ...final.headers }; } return this.#transport.send(final); } /** * Send multiple messages. */ async sendAll(messages: EmailOptions[], base: Partial = {}): Promise { return Promise.all( messages.map(message => this.send({ ...base, ...message, ...(message.context || base.context ? { context: { ...(base.context || {}), ...(message.context || {}) } } : {}) }) ) ); } }