arrow-down
    1. Widgets
    1. ID providers
    2. System ID provider
    3. Users and groups
    4. Roles
    1. Projects
    2. Layers
        1. AttachmentUploader
        2. Checkbox
        3. Combobox
        4. ContentSelector
        5. ContentTypeFilter
        6. CustomSelector
        7. Date
        8. DateTime
        9. Double
        10. GeoPoint
        11. HtmlArea
        12. ImageSelector
        13. Long
        14. MediaSelector
        15. Radiobutton
        16. Tag
        17. TextArea
        18. TextLine
        19. Time
      1. Field set
      2. Item set
      3. Option set
      4. Mixins
      5. Localization
    3. Content Types
    4. X-data
    5. Macros
    6. Custom styles
    7. Sites
      1. Regions
      2. Part component
      3. Layout component
      4. Text component
      5. Fragments
      6. Filtering
      7. Component indexing
      8. Visual editor
    8. Page templates
  1. Applications
    1. Sandboxes
    2. Code
    3. Building
    4. Configuration
    5. TypeScript
      1. Controllers
      2. Globals
      3. HTTP
      4. Events
      5. Error handler
      6. Filters
      7. ID provider
      8. Tasks
      9. Templating
      10. Localization
      11. Websocket
      12. Mappings
      13. Components
      14. Processors
      15. Contributions
      16. Main controller
      17. Java bridge
      1. Admin API
      2. Application API
      3. Auditlog API
      4. Authentication API
      5. Cluster API
      6. Common API
      7. Content API
      8. Context API
      9. Event API
      10. Export API
      11. Grid API
      12. I18N API
      13. IO API
      14. Mail API
      15. Node API
      16. Portal API
      17. Project API
      18. Repo API
      19. Scheduler API
      20. Schema API
      21. Tasks API
      22. Value API
      23. VHost API
      24. Websocket API
      1. Webapp Engine
        1. Image service
        2. Component service
      2. Admin Engine
      3. Asset service
      4. HTTP service
      5. IDprovider service
    1. Task engine
    2. Management Endpoint
    3. Statistics Endpoint
    1. Nodes and repos
    2. Properties
    3. Indexing
    4. Branches
    5. Queries (NoQL)
    6. Queries (DSL)
    7. Filters
    8. Aggregations
    9. Highlighting
    10. Editors
    1. Strategies
    2. Distributions
    3. Docker image
    4. Vhosts
    5. Configuration
    6. Backup & restore
    7. Systemd
    8. Clustering
  2. Audit Logs
    1. Upgrade Notes
    2. Upgrading Apps

Mail API

Contents

This API provides functions that are used for sending emails.

Usage

Add the following to your build.gradle file:

dependencies {
  include "com.enonic.xp:lib-mail:${xpVersion}"
}

Add the import statement to your controller:

import mailLib from '/lib/xp/mail';

You are now ready to use the API.

You also need to configure your smtp-server before you can send out email. For details, see the configuration page

Functions

send

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 from parameter supports the default from email substitution. If the email address is not specified in angle brackets (e.g., Some Name <> or <>), the default from email address is used. However, an error is thrown if the default from email is not set.

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

An object with the following parameters:

Name Kind Details

from

string

E-mail address and optionally name of message sender

to

string | string[]

E-mail address(es) and optionally name(s) of the message’s recipient(s)

cc

string | string[]

Optional list of carbon copy e-mail address(es)

bcc

string | string[]

Optional list of blind carbon copy e-mail address(es)

replyTo

string

Optional alternative e-mail address for replies

subject

string

Subject line of the message

body

string

Text content of the message

contentType

string

Optional content type of the message body

headers

string

Optional custom headers in the form of name-value pairs

attachments

Array

Optional attachments to include in the e-mail

Returns

boolean : true if message was successfully sent, false otherwise.

Example

Send a simple HTML e-mail
import {send} from '/lib/xp/mail';

const flag1 = 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 an e-mail with attachments
import {send} from '/lib/xp/mail';

const flag2 = 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
        }
    ]
});

getDefaultFromEmail

XP XP 7.14.1 7.14.1 Returns the default from email address configured in the XP mail configuration.

Example

Get a defaultFromEmail
import {getDefaultFromEmail} from '/lib/xp/mail';

const defaultFromEmail = getDefaultFromEmail();

Objects

attachments

Array of attachments to include in an e-mail

Properties of each element in the array

Name Kind Details

fileName

string

Attachment file name

data

*

Attachment stream

mimeType

string

Optional specification of attachment content type. If not specified, it will be inferred from the filename.

headers

object

Optional attachment headers in the form of name-value pairs.


Contents

Contents