Here is the function that takes multiple content parts, and sends a multipart 
email:

declare function util:send-email(
  $from-name as xs:string,
  $from-email as xs:string,
  $to-names as xs:string+,
  $to-emails as xs:string+,
  $cc-names as xs:string*,
  $cc-emails as xs:string*,
  $subject as xs:string,
  $content-types as xs:string*,
  $content-filenames as xs:string*,
  $content as item()*
) as empty-sequence() {
  let $newline := "
"
  let $boundary := concat("boundary-", xdmp:random())
  let $encoded-content := xdmp:multipart-encode(
    $boundary,
    <manifest>{
      for $item at $i in $content
      let $content-type := ($content-types[$i], "text/html")[1]
      let $filename := ($content-filenames[$i], "untitled.html")[1]
      return
        <part>
          <headers>
            <Content-Type>{$content-type}</Content-Type>
            <Content-Disposition>{
              if ($item instance of binary() or $filename != "") then
                concat("attachment; filename=", $filename)
              else
                "inline"
            }</Content-Disposition>
            <Content-Transfer-Encoding>{
              if ($item instance of binary() or $filename != "") then
                "base64"
              else
                "quoted-printable"
            }</Content-Transfer-Encoding>
          </headers>
        </part>
    }</manifest>,
    for $item at $i in $content
    let $content-type := ($content-types[$i], "text/html")[1]
    return
      if ($item instance of binary() or ($item instance of document-node() and 
$item/binary())) then
        document{ xs:base64Binary($item) }
      else if (contains($content-type, "html") and (not($content instance of 
element()) or empty($content/xhtml:html))) then
        <html xmlns="http://www.w3.org/1999/xhtml";>
          <head>
            <title>{$subject}</title>
          </head>
          <body>{$item}</body>
        </html>
      else if ($item instance of node()) then
        $item
      else
        (: multipart encode requires nodes as input :)
        document{ $item }
  )

  let $to :=
    for $email at $i in $to-emails
    let $name := $to-names[$i]
    return
      <em:Address xmlns:em="URN:ietf:params:email-xml:">
        <em:name>{$name}</em:name>
        <em:adrs>{$email}</em:adrs>
      </em:Address>
  let $cc :=
    for $email at $i in $cc-emails
    let $name := $cc-names[$i]
    return
      <em:Address xmlns:em="URN:ietf:params:email-xml:">
        <em:name>{$name}</em:name>
        <em:adrs>{$email}</em:adrs>
      </em:Address>
  return
  try {
  xdmp:email(
    <em:Message
      xmlns:em="URN:ietf:params:email-xml:"
      xmlns:rf="URN:ietf:params:rfc822:">
      <rf:subject>{$subject}</rf:subject>
      <rf:from>
        <em:Address>
          <em:name>{$from-name}</em:name>
          <em:adrs>{$from-email}</em:adrs>
        </em:Address>
      </rf:from>
      <rf:to>
        <em:Group>
          <em:name>recipients</em:name>
          {$to}
        </em:Group>
      </rf:to>
      <rf:cc>
        <em:Group>
          <em:name>cc</em:name>
          {$cc}
        </em:Group>
      </rf:cc>
      <rf:content-type>multipart/mixed; boundary={$boundary}</rf:content-type>
      <em:content xml:space="preserve">{xdmp:binary-decode($encoded-content, 
"utf-8")}</em:content>
    </em:Message>
  )
  } catch * {
    ()
  }
};

It might take a little effort to piece the two together, but it should contain 
all detail you need.

Kind regards,
Geert

From: 
<[email protected]<mailto:[email protected]>>
 on behalf of Geert Josten 
<[email protected]<mailto:[email protected]>>
Reply-To: MarkLogic Developer Discussion 
<[email protected]<mailto:[email protected]>>
Date: Thursday, April 13, 2017 at 10:57 AM
To: MarkLogic Developer Discussion 
<[email protected]<mailto:[email protected]>>
Subject: Re: [MarkLogic Dev General] Sending an HTML Email With Attachments

Hi William,

We use this method in demo-cat, which might make it a little easier for you to 
handle sending emails:

https://github.com/marklogic/demo-cat/blob/master/src/lib/utilities.xqy#L16

Note that the $message param is expecting xhtml elements..

It is lacking the attachments bit, but i’ll search my archive a bit. I might be 
able to recover how i did that..

Cheers,
Geert

From: 
<[email protected]<mailto:[email protected]>>
 on behalf of "William Holmes (WLT GB)" 
<[email protected]<mailto:[email protected]>>
Reply-To: MarkLogic Developer Discussion 
<[email protected]<mailto:[email protected]>>
Date: Thursday, April 13, 2017 at 10:00 AM
To: MarkLogic Developer Discussion 
<[email protected]<mailto:[email protected]>>
Subject: Re: [MarkLogic Dev General] Sending an HTML Email With Attachments

Thanks for your question Gert,

I’m trying to send the second one, an HTML formatted message with PDF 
attachments.

From:[email protected]<mailto:[email protected]>
 [mailto:[email protected]] On Behalf Of Geert Josten
Sent: 12 April 2017 20:05
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] Sending an HTML Email With Attachments

Hi William,

Are you trying to send html with embedded images or such, or just a pretty 
formatted message (in html) with some pdf or other doc as collateral?

We use html formatted messages in demo-cat, but I am sure I have also sent an 
attachment with success. I had trouble figuring out how to show attachments as 
part of the html message, but maybe you don’t need to go that far?

Cheers,
Geert

From: 
<[email protected]<mailto:[email protected]>>
 on behalf of "William Holmes (WLT GB)" 
<[email protected]<mailto:[email protected]>>
Reply-To: MarkLogic Developer Discussion 
<[email protected]<mailto:[email protected]>>
Date: Wednesday, April 12, 2017 at 11:27 AM
To: "[email protected]<mailto:[email protected]>" 
<[email protected]<mailto:[email protected]>>
Subject: [MarkLogic Dev General] Sending an HTML Email With Attachments

Hi,

I am trying to send an email using xdmp:email with an HTML body and attachments.

The only example on http://docs.marklogic.com/xdmp:email is with plain text in 
the body.

Does anyone have any examples of an email body containing HTML, e.g., <p>hello 
world</p> with attachments?

Thanks,

William Holmes

GOGREEN Climate Protection with DHL: please consider your environmental 
responsibility before printing this email.

This email is intended exclusively for the individual or entity to which it is 
addressed. This communication may contain information that is proprietary, 
privileged or confidential. If you are not the named addressee, you are not 
authorized to read, print, retain, copy or disseminate this message or any part 
of it. If you have received this message in error, please notify the sender 
immediately by email and delete all copies of the message.


GOGREEN Climate Protection with DHL: please consider your environmental 
responsibility before printing this email.

This email is intended exclusively for the individual or entity to which it is 
addressed. This communication may contain information that is proprietary, 
privileged or confidential. If you are not the named addressee, you are not 
authorized to read, print, retain, copy or disseminate this message or any part 
of it. If you have received this message in error, please notify the sender 
immediately by email and delete all copies of the message.
_______________________________________________
General mailing list
[email protected]
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to