Skip to content

EmailBuilder ^v2.7.194

Builds and sends an email.

js
var email = new EmailBuilder()
    .from(fromAddress)
    .to(toAddress)
    .subject(subject)
    .body(body)
    .attachBase64(filename, base64)
    .attachFile(filename, path)
    .send();

Constructor

js
new EmailBuilder();

Creates a new email builder instance.

Methods

  • from(addr) (string): The sender email address. Required.
  • to(addr) (string | string[]): Add a TO recipient email address. Required.
  • cc(addrOrList) (string | string[]): Adds CC recipients.
  • subject(text) (string): Email subject. Defaults to "Notification" if not specified.
  • body(htmlString) (string): Email body. Defaults to empty string if not specified.
  • attachBase64(filename, base64) (string, string): Adds an attachment using base64 content.
  • attachFile(filename, path) (string, string): Adds an attachment from a file on disk.
  • clearAttachments() (): Removes all attachments.
  • clearRecipients() (): Clears TO and CC recipients.
  • send() (boolean): Sends the email.

TIP

Note: bcc() is not currently supported.

WARNING

Attachment limitation: At this point in time, if any attachments are added, only one TO recipient is supported. Use cc() for additional recipients, or send multiple emails.

Recipient Input

  • to()
  • cc()
    • accepts:
      • A single email string
      • A delimited string ("a@b.com; c@d.com" or "a@b.com, c@d.com")
      • An array of strings

Attachments

Base64 Attachment

js
.attachBase64("report.pdf", base64String)
  • filename – The attachment name shown in the email.
  • base64String – Raw base64 file content.

File Attachment

js
.attachFile("sales_slice.csv", "exports/sales_slice.csv")
  • path must be inside the server filesystem directory.
  • The file is validated, read, and encoded to base64 automatically.

Returns

  • send() returns true if the email was successfully sent; otherwise false.

Examples

Basic Email

js
var email = new EmailBuilder()
    .from("no-reply@yourcompany.com")
    .to("user@acme.com")
    .subject("Hello")
    .body("<p>Sent from a script.</p>")
    .send();

console.log("Sent:", email);

With CC

js
var email = new EmailBuilder()
    .from("no-reply@yourcompany.com")
    .to("user@acme.com")
    .cc(["manager@acme.com", "finance@acme.com"])
    .subject("Monthly Update")
    .body("<p>Please review the attached report.</p>")
    .send();

With File Attachment

js
var email = new EmailBuilder()
    .from("no-reply@yourcompany.com")
    .to("user@acme.com")
    .subject("Export")
    .body("<p>Attached export from disk.</p>")
    .attachFile("sales_slice.csv", "exports/sales_slice.csv")
    .send();

With Base64 Attachment

js
var pdfBase64 = "JVBERi0xLjcKJc..."; // example

var ok = new EmailBuilder()
    .from("no-reply@yourcompany.com")
    .to("user@acme.com")
    .subject("Report")
    .body("<p>See attached report.</p>")
    .attachBase64("report.pdf", pdfBase64)
    .send();