mail

Mail related functions.

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

Methods

(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. ‘name@domain.org’ ) or an address with a display name. In the latter case the email will be enclosed with angle brackets (e.g. ‘Some Name name@domain.org’ ).

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.

Parameters:
Name Type Description
message object

JSON with the parameters.

Properties
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.

Properties
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.

Returns:

True if the message was sent successfully, false otherwise.

Type
boolean
Examples
// Send a simple HTML mail.
var flag1 = mailLib.send({
    from: 'me@enonic.com',
    to: 'user@somewhere.org',
    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 <sales@enonic.com>',
    to: 'user@somewhere.org',
    subject: 'Email Test from Enonic XP',
    cc: 'other@example.org',
    bcc: ['support@enonic.com', 'other@enonic.com'],
    replyTo: 'support@enonic.com',
    body: 'Welcome to Enonic XP!' + '\r\n\r\n' + '- The Dev Team',
    headers: {
        'Disposition-Notification-To': 'me@enonic.com'
    },
    attachments: [
        {
            fileName: 'logo.png',
            mimeType: 'image/png',
            data: stream1,
            headers: {
                'Content-ID': 'logo-img'
            }
        },
        {
            fileName: 'text.txt',
            data: stream2
        }
    ]
});