Main Page » XQuery » Functions » Mail Functions
BaseX 14 · This is the documentation of an upcoming version. Current version →

Mail Functions

This module contains functions for sending e-mail from XQuery. It is based on Jakarta Mail and its reference implementation Angus Mail.

Conventions

The module must be installed in the repository before it can be used. The version that matches BaseX is retrieved from the BaseX Maven Repository:

REPO INSTALL mail

In order to use it, it needs to be imported in the header of the query:

import module namespace mail = 'http://basex.org/modules/mail';

All functions in this module are assigned to the http://basex.org/modules/mail namespace, which in the following is assumed to be bound to the mail prefix.

Updated: Errors are assigned to the module namespace.

Errors are assigned to the module namespace; their messages are prefixed with Mail: . The codes are mail:options (missing or inconsistent options), mail:not-found (missing attachment), mail:address (invalid address), mail:connect (connection failure), mail:auth (authentication failure) and mail:error (other errors).

Functions

mail:send

Updated: Based on Jakarta Mail; stricter SSL/TLS and authentication checks. Options html, headers, bounce and timeout added. Attachments can be supplied as data and as file URIs. Sending is aborted when the query is stopped.

Signature
mail:send(  $options  as map(*)) as empty-sequence()
Summary Sends an e-mail. The $options map carries both the SMTP connection settings and the message contents. The following keys are recognized:
Key Type Description
subject xs:string Subject line.
from xs:string Sender address.
to xs:string* Recipient addresses.
cc xs:string* CC recipients.
bcc xs:string* BCC recipients.
reply-to xs:string* Reply-to addresses.
host xs:string SMTP host.
port xs:integer SMTP port. Default: 465 if ssl is enabled, 25 otherwise.
ssl xs:boolean Use SSL on connect.
tls xs:boolean Require STARTTLS.
user xs:string SMTP user name.
password xs:string SMTP password.
message xs:string Message body.
html xs:string HTML message body. If supplied, message is sent as plain-text alternative.
attachment (xs:string | map(*))* Attachments: file paths or file URIs, or maps with the keys name (file name), content (xs:base64Binary, xs:hexBinary or xs:string) and, optionally, type (content type; by default, derived from the file name).
headers map(xs:string, xs:string*) Additional headers.
bounce xs:string Envelope sender address, to which bounces are sent. Default: from.
timeout xs:decimal Maximum number of seconds to wait for the connection and for each read or write; 0 disables the timeout. Default: 60.

The message body is encoded as UTF-8. Each address and attachment value is trimmed before use. from, at least one recipient and a host must be supplied. SMTP authentication is performed if user and password are supplied, which must be specified together. With ssl or tls, the identity of the server is checked against its certificate.

Examples
import module namespace mail = 'http://basex.org/modules/mail';

mail:send({
  'host': 'smtp.your-mail-provider.org',
  'ssl': true(),
  'user': 'from@your-mail-provider.org',
  'password': '...',
  'from': 'from@your-mail-provider.org',
  'to': 'to@your-mail-recipient.org',
  'subject': 'Subject',
  'message': 'Message'
})
Sends a plain-text e-mail via an authenticated SMTP server.

Changelog

Version 14.0
  • Updated: mail:send: Based on Jakarta Mail; stricter SSL/TLS and authentication checks. Options html, headers, bounce and timeout added. Attachments can be supplied as data and as file URIs. Sending is aborted when the query is stopped.
  • Updated: Conventions: Errors are assigned to the module namespace.

⚡Generated with XQuery