Loading TOC...

xdmp:email

xdmp:email(
   $message as element(),
   [$options as (element()|map:map)?]
) as empty-sequence()

Summary

Send an email in an XQuery 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 an XML file that complies with the schema files listed below, or it can be a JavaScript object that follows our definition of JSON email format (please see xdmp.email).

Parameters
$message An XML representation of an email message to send. The message must comply with the XML schemas defined in the following schema files:
  • install_dir/Config/email-xml.xsd
  • install_dir/Config/rfc822.xsd
where install_dir is the directory in which MarkLogic Server is installed.
$options Options with which to customize this operation. You can specify options as either an XML element in the "xdmp:email" namespace, or as a map:map. The options names below are XML element localnames. When using a map, replace the hyphens with camel casing. For example, "an-option" becomes "anOption" when used as a map:map key. This function supports the following options. When including an option from another function in an XML options node, use the namespace appropriate to that function in the option element.

<authentication>

The credentials to use for this request. This element can contain the following child elements:
  • 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 a SMTP server that only supports classic STMP protocol.

<verify-cert>

A boolean value to specify whether the server's certificate should be verified. For SMTP server that supports Extended SMTP, the default value is true. For SMTP server that only supports classic SMTP, the default value is false. If a value of true is specified but 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.
  
xdmp:email(
<em:Message
 xmlns:em="URN:ietf:params:email-xml:"
 xmlns:rf="URN:ietf:params:rfc822:">
  <rf:subject>Sample HTML Email</rf:subject>
  <rf:from>
    <em:Address>
      <em:name>MarkLogic</em:name>
      <em:adrs>marklogic@yourdomain</em:adrs>
    </em:Address>
  </rf:from>
  <rf:to>
    <em:Address>
      <em:name>System Administrator</em:name>
      <em:adrs>admin@yourdomain</em:adrs>
    </em:Address>
  </rf:to>
  <em:content>
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Test HTML message</title>
      </head>
      <body>
        <h1>Test HTML message</h1>
        <p>Here is a simple paragraph</p>
      </body>
    </html>
  </em:content>
</em:Message>)

Example

This example demonstrates sending a message with
plain text content.

xdmp:email(
<em:Message
 xmlns:em="URN:ietf:params:email-xml:"
 xmlns:rf="URN:ietf:params:rfc822:">
  <rf:subject>Sample Plain Text Email</rf:subject>
  <rf:from>
    <em:Address>
      <em:name>MarkLogic</em:name>
      <em:adrs>marklogic@yourdomain</em:adrs>
    </em:Address>
  </rf:from>
  <rf:to>
    <em:Address>
      <em:name>System Administrator</em:name>
      <em:adrs>admin@yourdomain</em:adrs>
    </em:Address>
  </rf:to>
  <em:content xml:space="preserve">
This is a sample email with a plain text body.
</em:content>
</em:Message>)

Example

This example demonstrates sending a message with
attachments.

let $newline := "&#13;&#10;"
let $boundary := concat("blah", xdmp:random())
let $content-type := concat("multipart/mixed; boundary=",$boundary)
let $attachment1 := xs:base64Binary(doc("/space/binaries/testdata1/Bon-Jovi.jpeg"))
let $attachment2 := xs:base64Binary(doc("/space/binaries/testdata1/logo.gif"))
let $content := concat(
  "--",$boundary,$newline,
  $newline,
  "This is a test email with two images attached.", $newline,
  "--",$boundary,$newline,
  "Content-Type: image/jpeg", $newline,
  "Content-Disposition: attachment; filename=Bon-Jovi.jpeg", $newline,
  "Content-Transfer-Encoding: base64", $newline,
  $newline,
  $attachment1, $newline,
  "--",$boundary,$newline,
  "Content-Type: image/gif", $newline,
  "Content-Disposition: attachment; filename=logo.gif", $newline,
  "Content-Transfer-Encoding: base64", $newline,
  $newline,
  $attachment2, $newline,
  "--",$boundary,"--", $newline)

return
  xdmp:email(
  <em:Message
    xmlns:em="URN:ietf:params:email-xml:"
    xmlns:rf="URN:ietf:params:rfc822:">
    <rf:subject>Sample Email</rf:subject>
    <rf:from>
      <em:Address>
        <em:name>MarkLogic</em:name>
        <em:adrs>marklogic@yourdomain</em:adrs>
      </em:Address>
    </rf:from>
    <rf:to>
      <em:Address>
        <em:name>System Administrator</em:name>
        <em:adrs>admin@yourdomain</em:adrs>
      </em:Address>
    </rf:to>
    <rf:content-type>{$content-type}</rf:content-type>
    <em:content xml:space="preserve">
      {$content}
    </em:content>
  </em:Message>)

Example

xdmp:email(<em:Message
    xmlns:em="URN:ietf:params:email-xml:"
    xmlns:rf="URN:ietf:params:rfc822:">
    <rf:subject>Sample Email</rf:subject>
    <rf:from>
      <em:Address>
        <em:name>MarkLogic</em:name>
        <em:adrs>marklogic@yourdomain</em:adrs>
      </em:Address>
    </rf:from>
    <rf:to>
      <em:Address>
        <em:name>System Administrator</em:name>
        <em:adrs>admin@yourdomain</em:adrs>
      </em:Address>
    </rf:to>
  </em:Message>,
     <options xmlns="xdmp:email">
       <authentication>
         <username>myname</username>
         <password>mypassword</password>
       </authentication>
       <verify-cert>true</verify-cert>
     </options>)

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