Skip to content

mail

The mail helper creates an email object to send messages.

Constructor

mail()

Returns a new mail object that can be configured and sent.

js
let email = mail();

Optional - mail(host, user, pass, protocol, port)

You can set up and use a custom SMTP server.

  • host: SMTP server address
  • user: Username
  • pass: Password
  • protocol: "TLS" or "SSL"
  • port: Port number
js
let myMail = mail('smtp.mailtrap.io', 'username', 'password', 'TLS', '2525');

mail.subject(subject: string)

Sets the subject of the email.

js
email.subject('My Subject');

mail.from(address: string)

Sets the sender address.

js
email.from('noreply@mydomain.com');

mail.to(address: string)

Sets the main recipient. Overwrites existing "to" field if called again.

js
email.to('user@example.com');

mail.addTo(address: string)

Adds another "to" recipient without overwriting existing ones.

js
email.addTo('second@example.com');

mail.addCC(address: string)

Adds a CC recipient.

js
email.addCC('copy@example.com');

mail.addBcc(address: string)

Adds a BCC recipient.

js
email.addBcc('hidden@example.com');

mail.message(content: string)

Sets the email body (supports HTML).

js
let message = `
<!DOCTYPE html>
<html>
  <body>
    <h1>Heading</h1>
    <p>Some paragraph.</p>
  </body>
</html>`;
email.message(message);

mail.attach(data: binary, contentType: string, filename: string)

Adds an attachment.

js
let myFile = fs.load('path/to/file');
email.attach(myFile.data, myFile.contentType, myFile.fileName);

You can call attach() multiple times to add several attachments.

See more about fs.load.

mail.send()

Sends the configured email.

js
email.send();