import { EventEmittor, FlexibleFactory } from '@devbro/neko-helper'; import * as AwsSes from '@aws-sdk/client-ses'; import nodemailer from 'nodemailer'; /** * Interface representing an email message. * Defines the structure for email data that can be sent through various providers. */ interface Mailable { /** Sender email address */ from: string; /** Recipient email address(es) */ to: string | string[]; /** CC (carbon copy) email address(es) */ cc?: string | string[]; /** BCC (blind carbon copy) email address(es) */ bcc?: string | string[]; /** Email subject line */ subject: string; /** * Gets the plain text content of the email. * @returns The text content as a string */ getTextContent(): Promise; /** * Gets the HTML content of the email. * @returns The HTML content as a string */ getHtmlContent(): Promise; } /** * Interface that all mailer providers must implement. * Defines the contract for sending emails through different services. */ interface MailerProvider { /** * Sets the default sender email address. * @param from - The default sender email address */ setDefaultFrom(from: string): void; /** * Sends an email message. * @param mail - The email message to send */ sendMail(mail: Mailable): Promise; } /** Available mailer events */ declare const MailerEvents: string[]; /** Type representing mailer events */ type MailerEvent = (typeof MailerEvents)[number]; /** * Main mailer class for sending emails with event support. * Provides a unified interface for sending emails through various providers * with event notifications for sent and failed emails. */ declare class Mailer implements EventEmittor<["sent", "failed"]> { private provider; private eventManager; /** * Creates a new Mailer instance. * @param provider - The mailer provider to use for sending emails */ constructor(provider: MailerProvider); /** * Registers an event listener. * @param event - The event to listen for ('sent' or 'failed') * @param listener - The callback function to execute when the event occurs * @returns This mailer instance for chaining */ on(event: MailerEvent, listener: (...args: any[]) => void): this; /** * Removes an event listener. * @param event - The event to stop listening for * @param listener - The callback function to remove * @returns This mailer instance for chaining */ off(event: MailerEvent, listener: (...args: any[]) => void): this; /** * Emits an event to all registered listeners. * @param event - The event to emit * @param args - Arguments to pass to the listeners * @returns True if the event was emitted successfully */ emit(event: MailerEvent, ...args: any[]): Promise; /** * Sends an email message. * Emits 'sent' event on success or 'failed' event on error. * @param mail - The email message to send * @throws The error from the provider if sending fails */ send(mail: Mailable): Promise; } /** * Factory class for creating and registering mailer providers. * Uses a flexible factory pattern to manage different mailer provider implementations. */ declare class MailerProviderFactory { /** The singleton factory instance */ static instance: FlexibleFactory; /** * Registers a mailer provider factory function. * @template T - The provider type * @param key - Unique identifier for the provider * @param factory - Factory function that creates the provider instance */ static register(key: string, factory: (...args: any[]) => MailerProvider): void; /** * Creates a mailer provider instance. * @template T - The provider type * @param key - The provider identifier * @param args - Arguments to pass to the provider factory * @returns A new mailer provider instance */ static create(key: string, ...args: any[]): MailerProvider; } /** * Configuration options for the FunctionProvider. */ type FunctionProviderConfig = { /** Default sender email address */ default_from: string; }; /** * Mailer provider that uses a custom function to send emails. * Useful for testing or custom email sending logic. */ declare class FunctionProvider implements MailerProvider { private func; private config; private defaultFrom; /** * Creates a new FunctionProvider instance. * @param func - Function to call for sending emails * @param config - Provider configuration options */ constructor(func: Function, config: FunctionProviderConfig); /** * Sets the default sender email address. * @param from - The default sender email address */ setDefaultFrom(from: string): void; /** * Sends an email by calling the configured function. * @param mail - The email message to send */ sendMail(mail: Mailable): Promise; } /** * Configuration options for the MailchimpProvider. */ type MailchimpProviderConfig = { /** Mailchimp Transactional API key */ api_key: string; /** Default sender email address */ default_from: string; }; /** * Mailer provider that sends emails via Mailchimp Transactional (formerly Mandrill). * Supports API key from configuration or MAILCHIMP_API_KEY environment variable. */ declare class MailchimpProvider implements MailerProvider { private defaultFrom; private apiKey; /** * Creates a new MailchimpProvider instance. * @param options - Provider configuration options */ constructor(options?: Partial); /** * Sets the default sender email address. * @param from - The default sender email address */ setDefaultFrom(from: string): void; /** * Sends an email via Mailchimp Transactional API. * @param mail - The email message to send * @throws Error if the Mailchimp API request fails or emails are rejected */ sendMail(mail: Mailable): Promise; } /** * Configuration options for the MailgunProvider. */ type MailgunProviderConfig = { /** Mailgun API key */ api_key: string; /** Mailgun domain */ domain: string; /** Default sender email address */ default_from: string; /** Use EU servers (default: false) */ eu?: boolean; }; /** * Mailer provider that sends emails via Mailgun API. * Supports both US and EU Mailgun servers. * API key and domain can be provided via configuration or environment variables. */ declare class MailgunProvider implements MailerProvider { private defaultFrom; private apiKey; private domain; private baseUrl; /** * Creates a new MailgunProvider instance. * @param options - Provider configuration options */ constructor(options?: Partial); /** * Sets the default sender email address. * @param from - The default sender email address */ setDefaultFrom(from: string): void; /** * Sends an email via Mailgun API. * @param mail - The email message to send * @throws Error if the Mailgun API request fails */ sendMail(mail: Mailable): Promise; } /** * Configuration options for the MemoryProvider. */ type MemoryProviderConfig = { /** Default sender email address */ default_from: string; }; /** * Mailer provider that stores sent emails in memory. * Useful for testing without actually sending emails. */ declare class MemoryProvider implements MailerProvider { private config; private defaultFrom; /** Array of emails that have been "sent" (stored in memory) */ sentEmails: { from: string; to: string[]; cc: string[]; bcc: string[]; subject: string; text: string; html: string; }[]; /** * Creates a new MemoryProvider instance. * @param config - Provider configuration options */ constructor(config?: Partial); /** * Sets the default sender email address. * @param from - The default sender email address */ setDefaultFrom(from: string): void; /** * "Sends" an email by storing it in memory. * @param mail - The email message to send */ sendMail(mail: Mailable): Promise; } /** * Configuration options for the PostmarkProvider. */ type PostmarkProviderConfig = { /** Postmark server token */ server_token: string; /** Default sender email address */ default_from: string; }; /** * Mailer provider that sends emails via Postmark API. * Supports server token from configuration or POSTMARK_SERVER_TOKEN environment variable. */ declare class PostmarkProvider implements MailerProvider { private defaultFrom; private serverToken; /** * Creates a new PostmarkProvider instance. * @param options - Provider configuration options */ constructor(options?: Partial); /** * Sets the default sender email address. * @param from - The default sender email address */ setDefaultFrom(from: string): void; /** * Sends an email via Postmark API. * @param mail - The email message to send * @throws Error if the Postmark API request fails */ sendMail(mail: Mailable): Promise; } /** * Configuration options for the ProtonMailProvider. */ type ProtonMailProviderConfig = { /** ProtonMail Bridge host (default: 127.0.0.1) */ bridge_host?: string; /** ProtonMail Bridge port (default: 1025) */ bridge_port?: number; /** ProtonMail username */ username: string; /** ProtonMail password */ password: string; /** Default sender email address */ default_from: string; /** Whether to verify SSL certificates (default: false for Bridge) */ reject_unauthorized?: boolean; }; /** * Mailer provider that sends emails via ProtonMail Bridge SMTP. * Requires ProtonMail Bridge to be installed and running locally. * @see https://proton.me/mail/bridge */ declare class ProtonMailProvider implements MailerProvider { private defaultFrom; private transporter; private static nodemailerModule; /** * Creates a new ProtonMailProvider instance. * @param options - Provider configuration options */ constructor(options?: Partial); /** * Sets the default sender email address. * @param from - The default sender email address */ setDefaultFrom(from: string): void; /** * Sends an email via ProtonMail Bridge SMTP. * @param mail - The email message to send */ sendMail(mail: Mailable): Promise; } /** * Configuration options for the SESProvider. */ type SESProviderConfig = { /** AWS SES client configuration */ sesClientConfig: AwsSes.SESClientConfig; /** Default sender email address */ default_from: string; }; /** * Mailer provider that sends emails via Amazon SES (Simple Email Service). * Supports AWS credentials from environment variables or explicit configuration. */ declare class SESProvider implements MailerProvider { private sesClient; private defaultFrom; private static sesModule; /** * Creates a new SESProvider instance. * @param options - Provider configuration options */ constructor(options?: Partial); /** * Sets the default sender email address. * @param from - The default sender email address */ setDefaultFrom(from: string): void; /** * Sends an email via Amazon SES. * @param mail - The email message to send */ sendMail(mail: Mailable): Promise; } /** * Configuration options for the SendGridProvider. */ type SendGridProviderConfig = { /** SendGrid API key */ api_key: string; /** Default sender email address */ default_from: string; }; /** * Mailer provider that sends emails via SendGrid API. * Supports API key from configuration or SENDGRID_API_KEY environment variable. */ declare class SendGridProvider implements MailerProvider { private defaultFrom; private apiKey; /** * Creates a new SendGridProvider instance. * @param options - Provider configuration options */ constructor(options?: Partial); /** * Sets the default sender email address. * @param from - The default sender email address */ setDefaultFrom(from: string): void; /** * Maps email addresses to SendGrid email objects. * @param emails - Array of email addresses * @param required - Whether the field is required * @returns Array of email objects or undefined if not required and empty */ private mapToEmailObjects; /** * Sends an email via SendGrid API. * @param mail - The email message to send * @throws Error if the SendGrid API request fails */ sendMail(mail: Mailable): Promise; } /** * Configuration options for the SMTPProvider. */ type SMTPProviderConfig = { /** Nodemailer transport options for SMTP configuration */ nodemailer_options: nodemailer.TransportOptions; /** Default sender email address */ default_from: string; }; /** * Mailer provider that sends emails via SMTP using nodemailer. * Supports standard SMTP server configuration. */ declare class SMTPProvider implements MailerProvider { private defaultFrom; private transporter; private static nodemailerModule; /** * Creates a new SMTPProvider instance. * @param options - Provider configuration options */ constructor(options?: Partial); /** * Sets the default sender email address. * @param from - The default sender email address */ setDefaultFrom(from: string): void; /** * Sends an email via SMTP. * @param mail - The email message to send */ sendMail(mail: Mailable): Promise; } export { FunctionProvider, type FunctionProviderConfig, type Mailable, MailchimpProvider, type MailchimpProviderConfig, Mailer, type MailerEvent, MailerEvents, type MailerProvider, MailerProviderFactory, MailgunProvider, type MailgunProviderConfig, MemoryProvider, type MemoryProviderConfig, PostmarkProvider, type PostmarkProviderConfig, ProtonMailProvider, type ProtonMailProviderConfig, SESProvider, type SESProviderConfig, SMTPProvider, type SMTPProviderConfig, SendGridProvider, type SendGridProviderConfig };