XQuery/Sending E-mail

      Motivation

      You want to send an e-mail message from within an XQuery. This frequently done when a report has finished running or when a key event such as a task update has been done.

      ↑Jump back a section

      Method

      eXist provides a simple interface to e-mail.

      ↑Jump back a section

      Format of the send-email function

        mail:send-email($email as element()+, $server as xs:string?, $charset as xs:string?) xs:boolean+
      

      where $email The email message in the following format:

      <mail>
         <from/>
         <reply-to/>
         <to/>
         <cc/>
         <bcc/>
         <subject/>
         <message>
           <text/>
           <xhtml/>
         </message>
         <attachment filename="" mimetype="">xs:base64Binary</attachment> 
      </mail>
      
        $server   The SMTP server. If empty, then it tries to use the local sendmail program.
        $charset       The charset value used in the "Content-Type" message header (Defaults to UTF-8)
      
      ↑Jump back a section

      Sample Code

      xquery version "1.0";
       
      (: Demonstrates sending an email through Sendmail from eXist :)
       
      declare namespace mail="http://exist-db.org/xquery/mail";
       
      declare variable $message {
        <mail>
          <from>John Doe &lt;sender@domain.com&gt;</from>
          <to>recipient@otherdomain.com</to>
          <cc>cc@otherdomain.com</cc>
          <bcc>bcc@otherdomain.com</bcc>
          <subject>A new task is waiting your approval</subject>
          <message>
            <text>A plain ASCII text message can be placed inside the text elements.</text>
            <xhtml>
                 <html>
                     <head>
                       <title>HTML in an e-mail in the body of the document.</title>
                     </head>
                     <body>
                        <h1>Testing</h1>
                        <p>Test Message 1, 2, 3</p>
                     </body>
                 </html>
            </xhtml>
          </message>
        </mail>
      };
       
      if ( mail:send-email($message, 'mail server', ()) ) then
        <h1>Sent Message OK :-)</h1>
      else
        <h1>Could not Send Message :-(</h1>
      
      ↑Jump back a section
      Last modified on 26 March 2010, at 22:49