Mail related functions.

var mailLib = require('/lib/xp/mail');


(static) send(message) → {boolean}

This function sends an email message using the mail server configured.

The address values can be either a simple email address (e.g. ‘’ ) or an address with a display name. In the latter case the email will be enclosed with angle brackets (e.g. ‘Some Name’ ).

The parameters to, cc and bcc can be passed as a single string or as an array of strings, if there are multiple addresses to specify.

The content-type of the email can be specified by using the contentType parameter. See example below for sending a message with an HTML body.

Name Type Description
message object

JSON with the parameters.

Name Type Attributes Description
from string

The email address, and optionally name of the sender of the message.

to string | Array.<string>

The email address(es), and optionally name(s) of the primary message’s recipient(s).

cc string | Array.<string> <optional>

The carbon copy email address(es).

bcc string | Array.<string> <optional>

The blind carbon copy email address(es).

replyTo string <optional>

The email address that should be used to reply to the message.

subject string

The subject line of the message.

body string

The text content of the message.

contentType string <optional>

Content type of the message body.

headers object <optional>

Custom headers in the form of name-value pairs.

attachments Array.<object> <optional>

Attachments to include in the email.

Name Type Attributes Description
fileName string

Attachment file name.

data *

Attachment stream.

mimeType string <optional>

Attachment content type. If not specified will be inferred from the file extension.

headers object <optional>

Attachment headers, in the form of name-value pairs.


True if the message was sent successfully, false otherwise.

// Send a simple HTML mail.
var flag1 = mailLib.send({
    from: '',
    to: '',
    subject: 'HTML email test',
    body: '<h1>HTML Email!</h1><p>You can use the contentType parameter for HTML messages.</p>',
    contentType: 'text/html; charset="UTF-8"'
// Send a mail with attachments.
var flag2 = mailLib.send({
    from: 'Sales Department <>',
    to: '',
    subject: 'Email Test from Enonic XP',
    cc: '',
    bcc: ['', ''],
    replyTo: '',
    body: 'Welcome to Enonic XP!' + '\r\n\r\n' + '- The Dev Team',
    headers: {
        'Disposition-Notification-To': ''
    attachments: [
            fileName: 'logo.png',
            mimeType: 'image/png',
            data: stream1,
            headers: {
                'Content-ID': 'logo-img'
            fileName: 'text.txt',
            data: stream2