@@ -10,38 +10,132 @@ declare option xdmp:mapping "false"; (::)
1010 : Wrapper function for sending an email
1111 :)
1212declare function util:send-notification (
13- $recipient-name as xs:string,
14- $recipient-email as xs:string,
13+ $recipient-names as xs:string+ ,
14+ $recipient-emails as xs:string+ ,
1515 $subject as xs:string,
1616 $message as item ()
1717) as empty-sequence () {
18- xdmp:email (
19- <em:Message
20- xmlns:em = "URN:ietf:params:email-xml:"
21- xmlns:rf = "URN:ietf:params:rfc822:" >
22- <rf:subject>{$subject}</rf:subject>
23- <rf:from>
24- <em:Address>
25- <em:name>MarkLogic Demo</em:name>
26- <em:adrs>
[email protected] </em:adrs>
27- </em:Address>
28- </rf:from>
29- <rf:to>
30- <em:Address>
31- <em:name>{$recipient-name}</em:name>
32- <em:adrs>{$recipient-email}</em:adrs>
33- </em:Address>
34- </rf:to>
35- <em:content>
18+ util:send-email (
19+ "MarkLogic Demo" ,
20+ 21+ $recipient-names,
22+ $recipient-emails,
23+ (),
24+ (),
25+ $subject,
26+ "text/html" ,
27+ "body" ,
28+ <html xmlns = "http://www.w3.org/1999/xhtml" >
29+ <head>
30+ <title>{$subject}</title>
31+ </head>
32+ <body>{$message}</body>
33+ </html>
34+ )
35+ };
36+
37+ declare function util:send-email (
38+ $from-name as xs:string,
39+ $from-email as xs:string,
40+ $to-names as xs:string+,
41+ $to-emails as xs:string+,
42+ $cc-names as xs:string*,
43+ $cc-emails as xs:string*,
44+ $subject as xs:string,
45+ $content-types as xs:string*,
46+ $content-filenames as xs:string*,
47+ $content as item ()*
48+ ) as empty-sequence () {
49+ let $newline := " "
50+ let $boundary := concat ("boundary-" , xdmp:random ())
51+ let $encoded-content := xdmp:multipart-encode (
52+ $boundary,
53+ <manifest>{
54+ for $item at $i in $content
55+ let $content-type := ($content-types[$i], "text/html" )[1 ]
56+ let $filename := ($content-filenames[$i], "untitled.html" )[1 ]
57+ return
58+ <part>
59+ <headers>
60+ <Content-Type>{$content-type}</Content-Type>
61+ <Content-Disposition>{
62+ if ($item instance of binary () or $filename != "" ) then
63+ concat ("attachment; filename=" , $filename)
64+ else
65+ "inline"
66+ }</Content-Disposition>
67+ <Content-Transfer-Encoding>{
68+ if ($item instance of binary () or $filename != "" ) then
69+ "base64"
70+ else
71+ "quoted-printable"
72+ }</Content-Transfer-Encoding>
73+ </headers>
74+ </part>
75+ }</manifest>,
76+ for $item at $i in $content
77+ let $content-type := ($content-types[$i], "text/html" )[1 ]
78+ return
79+ if ($item instance of binary () or ($item instance of document-node () and
80+ $item/binary ())) then
81+ document { xs:base64Binary ($item) }
82+ else if (contains ($content-type, "html" ) and (not ($content instance of
83+ element ()) or empty ($content/*:html))) then
3684 <html xmlns = "http://www.w3.org/1999/xhtml" >
3785 <head>
3886 <title>{$subject}</title>
3987 </head>
40- <body>{$message }</body>
88+ <body>{$item }</body>
4189 </html>
42- </em:content>
43- </em:Message>
90+ else if ($item instance of node ()) then
91+ $item
92+ else
93+ (: multipart encode requires nodes as input :)
94+ document { $item }
4495 )
96+
97+ let $to :=
98+ for $email at $i in $to-emails
99+ let $name := $to-names[$i]
100+ return
101+ <em:Address xmlns:em = "URN:ietf:params:email-xml:" >
102+ <em:name>{$name}</em:name>
103+ <em:adrs>{$email}</em:adrs>
104+ </em:Address>
105+ let $cc :=
106+ for $email at $i in $cc-emails
107+ let $name := $cc-names[$i]
108+ return
109+ <em:Address xmlns:em = "URN:ietf:params:email-xml:" >
110+ <em:name>{$name}</em:name>
111+ <em:adrs>{$email}</em:adrs>
112+ </em:Address>
113+ return
114+ xdmp:email (
115+ <em:Message xmlns:em = "URN:ietf:params:email-xml:" xmlns:rf = "URN:ietf:params:rfc822:" >
116+ <rf:subject>{$subject}</rf:subject>
117+ <rf:from>
118+ <em:Address>
119+ <em:name>{$from-name}</em:name>
120+ <em:adrs>{$from-email}</em:adrs>
121+ </em:Address>
122+ </rf:from>
123+ <rf:to>
124+ <em:Group>
125+ <em:name>recipients</em:name>
126+ {$to}
127+ </em:Group>
128+ </rf:to>
129+ <rf:cc>
130+ <em:Group>
131+ <em:name>cc</em:name>
132+ {$cc}
133+ </em:Group>
134+ </rf:cc>
135+ <rf:content-type>multipart/mixed; boundary={$boundary}</rf:content-type>
136+ <em:content xml:space = "preserve" >{xdmp:binary-decode ($encoded-content, "utf-8" )}</em:content>
137+ </em:Message>
138+ )
45139};
46140
47141declare function util:highlight ($doc, $query) {
@@ -53,6 +147,18 @@ declare function util:is-binary($node) {
53147 or $node/node () instance of binary ()
54148};
55149
150+ declare function util:is-json ($node) {
151+ $node instance of object-node ()
152+ or $node instance of array-node ()
153+ or $node/node () instance of array-node ()
154+ or $node/node () instance of object-node ()
155+ };
156+
157+ declare function util:is-xml ($node) {
158+ $node instance of element ()
159+ or $node/node () instance of element ()
160+ };
161+
56162declare function util:get-content-type ($uri) {
57163 xdmp:uri-content-type ($uri)
58164};
@@ -61,6 +167,10 @@ declare function util:get-content-type($uri, $doc) {
61167 let $content-type :=
62168 if (util:is-binary ($doc)) then
63169 xdmp:document-properties ($uri)//*:meta[@name = 'content-type' ]/(@content, .)[not (. = ('' , 'text/plain' ))]
170+ else if (util:is-json ($doc)) then
171+ "application/json"
172+ else if (util:is-xml ($doc)) then
173+ "application/xml"
64174 else ()
65175 return
66176 ($content-type, xdmp:uri-content-type ($uri))[1 ]
0 commit comments