Skip to content

EmailBuilder ^v2.7.194 ​

Builds and sends an email.

js
const sent = new EmailBuilder()
    .from("no-reply@yourcompany.com")
    .to("user@acme.com")
    .subject("Hello")
    .body("Sent from a script.")
    .send();

Constructor ​

new EmailBuilder new EmailBuilder() EmailBuilder ​

Creates an empty email. Every method except send() returns the builder, so calls can be chained.

js
const email = new EmailBuilder();

Methods ​

from from(address) EmailBuilder ​

Sets the sender's address. Required: send() fails if it's missing or isn't a valid address.

ParameterTypeDescription
addressstringThe sender's email address.

to to(addresses) EmailBuilder ​

Adds recipients. Call it more than once to keep adding. At least one recipient is required.

ParameterTypeDescription
addressesstring | string[]One address, several separated by ; or ,, or an array of addresses.

cc cc(addresses) EmailBuilder ​

Adds CC recipients.

ParameterTypeDescription
addressesstring | string[]One address, several separated by ; or ,, or an array of addresses.

subject subject(text) EmailBuilder ​

Sets the subject line. Defaults to "Notification" if it isn't set.

ParameterTypeDescription
textstringThe subject.

body body(body) EmailBuilder ​

Sets the body. Defaults to empty.

ParameterTypeDescription
bodystringThe body text.

attachFile attachFile(filename, path) EmailBuilder ​

Attaches a file. The file is read when the email is sent.

ParameterTypeDescription
filenamestringThe attachment's name, as shown in the email.
pathstringThe path of the file to attach.

attachBase64 attachBase64(filename, base64) EmailBuilder ​

Attaches a file from base64 content.

ParameterTypeDescription
filenamestringThe attachment's name, as shown in the email.
base64stringThe file's content, base64-encoded.

clearAttachments clearAttachments() EmailBuilder ​

Removes every attachment, so the builder can be reused with different files.

clearRecipients clearRecipients() EmailBuilder ​

Removes every TO and CC recipient, so the builder can be reused for different people.

send send() boolean ​

Sends the email. Returns true when it was sent, otherwise false.

Examples ​

Basic email ​

js
const sent = new EmailBuilder()
    .from("no-reply@yourcompany.com")
    .to("user@acme.com")
    .subject("Hello")
    .body("Sent from a script.")
    .send();

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

With CC ​

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

With a file attachment ​

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

With a base64 attachment ​

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

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