
xdmp.email( message as Object, [options as Object?] ) as null
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).
// 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);
// 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);
// 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);
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);
// 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: Get the most useful answers to questions from the MarkLogic community, or ask your own question.