Sending Emails

Fluent API

The Mail.to() method returns a MessageBuilder with chainable methods:

const result = await Mail.to('user@example.com')
  .subject('Hello')
  .html('<h1>Hello!</h1>')
  .send();

Chainable Methods

MethodSignatureDescription
to()Mail.to(address: string | string[])Start building an email (entry point)
subject().subject(subject: string)Set the email subject
html().html(html: string)Set HTML body content
text().text(text: string)Set plain text body content
from().from(from: string)Override the default sender address
cc().cc(cc: string | string[])Add CC recipients
bcc().bcc(bcc: string | string[])Add BCC recipients
replyTo().replyTo(replyTo: string)Set reply-to address
attachments().attachments(attachments: Attachment[])Add file attachments
headers().headers(headers: Record<string, string>)Set custom email headers
template().template(template: string)Use a template file (requires template engine)
data().data(data: Record<string, unknown>)Pass data to the template
send().send(mailable?: Mailable): Promise<MailResponse>Send the email
queue().queue(mailable?: Mailable): Promise<QueueJobResult>Queue for background sending
later().later(delaySeconds: number, mailable?: Mailable): Promise<QueueJobResult>Queue with delay
at().at(date: Date, mailable?: Mailable): Promise<QueueJobResult>Schedule for a specific time

Mailable Classes

Create reusable email definitions by extending the abstract Mailable class:

import { Mailable } from '@impruthvi/nodemail';

class OrderConfirmation extends Mailable {
  constructor(
    private order: { id: string; total: number },
    private customerName: string
  ) {
    super();
  }

  build() {
    return this
      .subject(`Order #${this.order.id} Confirmed`)
      .from('orders@example.com')
      .html(`<h1>Thank you, ${this.customerName}!</h1>
             <p>Your order #${this.order.id} totaling $${this.order.total} is confirmed.</p>`)
      .cc('orders-archive@example.com')
      .replyTo('support@example.com')
      .attach('./invoices/invoice.pdf', 'invoice.pdf')
      .withHeaders({ 'X-Order-Id': this.order.id });
  }
}

Protected Methods in Mailable

MethodSignatureDescription
build()abstract build(): thisDefine email content (must override)
from()protected from(from: string): thisSet sender address
subject()protected subject(subject: string): thisSet subject line
html()protected html(html: string): thisSet HTML content
text()protected text(text: string): thisSet plain text content
view()protected view(template: string, data?: Record<string, unknown>): thisUse a template with data
cc()protected cc(cc: string | string[]): thisAdd CC recipients
bcc()protected bcc(bcc: string | string[]): thisAdd BCC recipients
replyTo()protected replyTo(replyTo: string): thisSet reply-to address
attach()protected attach(path: string, filename?: string): thisAdd an attachment
withHeaders()protected withHeaders(headers: Record<string, string>): thisSet custom headers

Public Methods in Mailable

MethodSignatureDescription
to()to(recipients: string | string[]): thisSet recipients
setMailManager()setMailManager(manager: MailManager): thisSet the mail manager (internal)
send()send(): Promise<MailResponse>Send the email directly
getMailOptions()getMailOptions(): Partial<MailOptions>Get the built options (calls build())

Sending Patterns

Pattern 1: Fluent API with inline content

await Mail.to('user@example.com')
  .subject('Hello')
  .html('<h1>Hello!</h1>')
  .send();

Pattern 2: Fluent API with Mailable

await Mail.to('user@example.com').send(new WelcomeEmail(user));

Pattern 3: Direct send via Mail.send()

const email = new WelcomeEmail(user);
email.to('user@example.com');
await Mail.send(email);

Pattern 4: Mailable self-send

const email = new WelcomeEmail(user);
email.to('user@example.com');
email.setMailManager(mailManager);
await email.send();

Switching Mailers

Use Mail.mailer() to get a MailManager instance configured for a specific provider:

// Send via SendGrid instead of the default mailer
const sendgridManager = Mail.mailer('sendgrid');
await sendgridManager.to('user@example.com')
  .subject('Hello via SendGrid')
  .html('<h1>Hello!</h1>')
  .send();

Message Class (Low-Level Builder)

The Message class is a standalone message builder that can be used independently of the Mail facade:

import { Message } from '@impruthvi/nodemail';

const message = new Message();
message
  .to('user@example.com')
  .from('sender@example.com')
  .subject('Hello')
  .html('<h1>Hello!</h1>')
  .text('Hello!')
  .cc('cc@example.com')
  .bcc('bcc@example.com')
  .replyTo('reply@example.com')
  .attach({ filename: 'file.pdf', path: './file.pdf' })
  .header('X-Custom', 'value')
  .template('welcome', { name: 'John' });

const options = message.toOptions(); // Get the built MailOptions

Message Methods

MethodSignatureDescription
to()to(address: string | string[] | MailAddress | MailAddress[]): thisSet recipients
from()from(address: string | MailAddress): thisSet sender
subject()subject(subject: string): thisSet subject
html()html(html: string): thisSet HTML content
text()text(text: string): thisSet plain text content
cc()cc(address: string | string[] | MailAddress | MailAddress[]): thisAdd CC recipients
bcc()bcc(address: string | string[] | MailAddress | MailAddress[]): thisAdd BCC recipients
replyTo()replyTo(address: string | MailAddress): thisSet reply-to address
attach()attach(attachment: Attachment): thisAdd an attachment
header()header(name: string, value: string): thisAdd a custom header
template()template(name: string, data: Record<string, unknown>): thisSet template and data
toOptions()toOptions(): Partial<MailOptions>Get the built message options