Loading TOC...

xdmp.email

xdmp.email(
   message as Object,
   [options as Object?]
) as null

Summary

Send an email in a JavasCript program. A valid SMTP Relay must be configured in the Groups page of the Admin Interface for the email to be sent. The format of the email message can be a JavaScript object that follows the format as below, or it can be an XML file that complies with the schema files for email (see xdmp:email).

Parameters
message A JSON presentation of an email message to send. The message must comply with the following format:
{
  "cc" : {"user@yourdomain.com"},
  "comments" : "This is comment",
  "from" : {"name" : "sender", "address" : admin@marklogic.com},
  "reply-to" : {"group" : "team", "addresses": [ address1, address2]},
  "subject"  :  "JSON Email Format",
  "to" : {"addresses": [ address 1, address 2]},
  "content-type" : "multipart/mixed; boundary = blablabla",
  ...
  "content" : { "boundary" : "blablabla",
                "parts" : [
                            "Text part",
                            {
                             "Content-Type" : "image/jpeg",
                             "Disposition" : "attachment",
                             "filename": "sample_attachement_1.jpg",
                             "Content-Transfer-Encoding": "base64",
                             "attachment": encoded attachement 1
                             },

                             {
                               "Content-Type" : "image/jpeg",
                               "Disposition" : "attachment",
                               "filename": "sample_attachement_2.gif",
                               "Content-Transfer-Encoding": "base64",
                               "attachment": encoded attachement 2
                             }
                           ]
              }
}
options Options with which to customize this operation.

authentication

The credentials to use for this request. This property can contain the following child properties:
  • username: The username of the user to be authenticated on the http server
  • password: The password for username.

An error will be thrown if authentication is used for an SMTP server that only supports classic STMP protocol.

verifyCert

A boolean value to specify whether the server's certificate should be verified. For an SMTP server that supports Extended SMTP, the default value is true. For an SMTP server that only supports classic SMTP, the default value is false. If a value of true is specified but the SMTP server only supports classic SMTP, an error will be thrown.

Required Privileges

http://marklogic.com/xdmp/privileges/xdmp-email

Example

// This example demonstrates sending a message with
// HTML content.
  
var from = {"name":"Administrator", "address":"admin@marklogic.com"};
var address1 = {"name":"user1", "address":"user1@yourdomain.com"};
var address2 = {"name":"user2", "address": "user2@yourdomain.com"};
var address3 = {"name":"user3","address":"user3@yourdomain.com"};
var contentType = "html"
var html = '<html>
              <head>
                <title>HTML message</title>
              </head>
              <body>
                <h1>Test Header</h1>
                <p>This is a sample paragraph</p>
              </body>
            </html>';
var message = {"from":from,
               "to":{"addresses":[address1, address2, address3]},
               "subject":"Test HTML email from ML Server",
               "content-type":contentType,
               "content":html
               };
xdmp.email(message);

Example

// This example demonstrates sending a message with
// plain text content.

var message = {"from":{"name":"Administrator", "address":"admin@marklogic.com"},
               "to":{"name":"username", "address":"user@yourdomain.com"},
               "subject":"Plain Text Message",
               "content":"Plain text content."};
xdmp.email(message);


Example

// This example demonstrates sending a message with
// attachments.

var boundary = "blahblahblah" + xdmp.random();
var contentType = "multipart/mixed; boundary=" + boundary;
var att1 = xs.base64Binary(fn.head(xdmp.documentGet("/path/to/the file1.jpeg")));
var att2 = xs.base64Binary(fn.head(xdmp.documentGet("/path/to/the file2.jpeg")));
var part1 = "This is a sample multipart message in JSON format";
var part2 = {
             "Content-Type": "image/jpeg",
             "Content-Disposition":"attachment",
             "filename":"file1.jpeg",
             "Content-Transfer-Encoding":"base64",
             "attachment":att1
            };
var part3 = {
             "Content-Type": "image/jpeg",
             "Content-Disposition":"attachment",
             "filename":"file2.jpeg",
             "Content-Transfer-Encoding":"base64",
             "attachment":att2
            };
var from = {"name":"Administrator", "address":"admin@marklogic.com"};
var to ={"address":"username@yourdomain.com"};
var subject = "Test JSON Multipart Message with Attachments!";
var message = {
               "from":from,
               "to":to,
               "subject": subject,
               "content-type":contentType,
               "content":{
                          "boundary":boundary,
                          "parts":[part1, part2, part3]
                         }
};
xdmp.email(message);

Example

var message = {"from":{"name":"Administrator", "address":"admin@marklogic.com"},
               "to":{"name":"username", "address":"user@yourdomain.com"},
               "subject":"Plain Text Message",
               "content":"Plain text content."};
var options = {"authentication": {
                "username" : "myname",
                "password" : "mypassword"},
               "verifyCert": true};
xdmp.email(message, options);

Example

// This example demonstrates sending a message that contains
// diacritic headers.

var boundary = "blahblahblah" + xdmp.random();
var contentType = "multipart/mixed; boundary=" + boundary;
var att1 = xs.base64Binary(fn.head(xdmp.documentGet("/space/binaries/testdata1/Bon-Jovi.jpeg")));
var att2 = xs.base64Binary(fn.head(xdmp.documentGet("/space/binaries/testdata1/logo.gif")));
var part1 = "This is a sample multipart message in JSON format";
var part2 = {
             "Content-Type": "image/jpeg",
             "Content-Disposition":"attachment",
             "filename":"file1.jpeg",
             "Content-Transfer-Encoding":"base64",
             "attachment":att1
            };
var part3 = {
             "Content-Type": "image/jpeg",
             "Content-Disposition":"attachment",
             "filename":"file2.jpeg",
             "Content-Transfer-Encoding":"base64",
             "attachment":att2
            };
var from = {"name":"=?utf-8?Q?Diäcritic sender?=", "address":"admin@marklogic.com"};
var to ={"name":"=?utf-8?Q?Diäcritic receiver?=", "address":"username@yourdomain.com"};
var subject = "=?utf-8?Q?Subject with diäcritic?=";
var message = {
               "from":from,
               "to":to,
               "subject": subject,
               "content-type":contentType,
               "content":{
                          "boundary":boundary,
                          "parts":[part1, part2, part3]
                         }
};
xdmp.email(message);

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.