JCC Express

Digging Deeper

Mail

Introduction

JCC Express MVC provides a Laravel-style mail API with:

  • Mail facade
  • Mailable base class
  • Envelope, Address, Content, and Attachment
  • Driver manager (Nodemailer or Resend, based on MAIL_DRIVER)

Quick send with Mail

TypeScript
import { Mail } from "jcc-express-mvc/Core/Mail";

await Mail.to("user@example.com")
  .cc("manager@example.com")
  .bcc("audit@example.com")
  .send({
    subject: "Welcome",
    sender: "My App",
    email: "noreply@example.com",
    message: "Thanks for signing up",
    content: "mail/welcome", // view path
  });

Mail.to(...).cc(...).bcc(...).send(...) builds a payload and routes it through MailManager.


Sending a Mailable class

Use Mailable for reusable templates and metadata:

TypeScript
import { Mailable, Content, Envelope, Address } from "jcc-express-mvc/Core/Mail";

export class OrderShipped extends Mailable {
  constructor(private order: any) {
    super();
  }

  content(): Content {
    return new Content({
      view: "mail/orders/shipped",
      with: { order: this.order },
    });
  }

  envelope(): Envelope {
    return new Envelope({
      subject: "Order shipped",
      from: new Address("noreply@store.com", "Store"),
    });
  }
}

Send it:

TypeScript
await Mail.to(user.email).send(new OrderShipped(order));

Attachments

The Attachment helper supports path, storage, and in-memory data:

TypeScript
import { Attachment } from "jcc-express-mvc/Core/Mail";

attachments() {
  return [
    Attachment.fromPath("/tmp/invoice.pdf").as("invoice.pdf"),
    Attachment.fromStorage("reports/monthly.pdf", "public"),
    Attachment.fromData(() => pdfBuffer, "generated.pdf").withMime("application/pdf"),
  ];
}

Driver selection

MailManager chooses the driver from MAIL_DRIVER:

  • resend -> Resend driver
  • anything else (smtp, mailtrap, etc.) -> Nodemailer driver

Related env values used by drivers include:

  • MAIL_DRIVER
  • MAIL_HOST, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD
  • MAIL_FROM_ADDRESS, MAIL_FROM_NAME
  • RESEND_API_KEY (or MAIL_API_KEY fallback)

Rendering behavior

  • If content is Content, it compiles with its view and with data.
  • If content is a string, it is treated as a view/template path and compiled.
  • subject, recipients, cc/bcc, and attachments are then passed to the active driver.

Summary

  • Use Mail.to(...).send(...) for simple emails.
  • Use Mailable + Envelope + Content for reusable classes.
  • Use Attachment for files/data payloads.
  • Driver is selected automatically by MAIL_DRIVER.