Skip to main content

Sending Documents by Email - REST API

How to send documents using a REST API and SMTP server?

Written by Lenka Haringerová

Via the REST API — just like through the web interface — you can send a document by email. For sending to work, a connection to an SMTP server must be configured on the server.


SMTP server configuration

In the file flexibee-server.xml (on Linux in /etc/flexibee/flexibee-server.xml, where to find it), add the following values:

Value

Meaning

smtp.host

The SMTP server address, typically localhost if the SMTP server runs on the same machine as the ABRA Flexi Server.

smtp.port

The SMTP server port, typically 25. Optional.

smtp.defaultFrom

The default sender email address.

smtp.encryption

The security mode for SMTP communication — see the values in the table below.

smtp.auth.user

The login username, if the SMTP server requires authentication. Optional.

smtp.auth.password

The password. Optional.

Values for the smtp.encryption parameter:

none

No security (default).

starttls

Secure communication if supported by the server (STARTTLS command).

tls

Required switch to secure communication (TLS).

ssl

Fully secure communication (SSL).

💡 In the cloud, where you don't have access to the flexibee-server.xml file, SMTP can also be configured via the REST API for an individual company — see SMTP server configuration - API.


Sending a document

On a properly configured server, sending a document means calling the PUT or POST method on the odeslani-dokladu sub-record:

PUT https://demo.flexibee.eu/c/demo/faktura-vydana/1/odeslani-dokladu.xml?to=email@example.com&subject=Doklad%20ABC

Parameter

Meaning

to=email@example.com

Recipient; this parameter can be specified multiple times.

cc=email@example.com

CC; this parameter can be specified multiple times.

subject=Doklad ABC

The email subject.

sablona=code:SABLONA

Custom email template — the ID of a record from the sablona-mail record type.

report-lang=en

The language of the PDF attachment. If not specified, the default email language is used.

At least one recipient or CC recipient must be specified — otherwise the API returns 400 with code recipientIsMissing and the message At least one recipient must be specified. The sender (the From header) will be the current user account under which you are performing the sending, or otherwise the default value from the configuration.

The request body may contain the plain-text body of the email, encoded in UTF-8. The email will include the PDF version of the document as an attachment, and the ISDOC version as well, if applicable.

ℹ️ Either the odeslani-dokladu.xml extension or the Accept: text/xml header must be specified. Parameters in the URL must be correctly encoded — pay particular attention to the subject in the example above.

Where the email text comes from

Both the subject and the body are looked up in the following order — the first source with non-empty text is used:

  1. The subject parameter, or the request body. Text consisting only of whitespace characters is not used.

  2. The template from the sablona parameter.

  3. The template from the document type.

  4. The text from the document type.

  5. The default template for the given record type.

  6. The default system text.


Customizing email messages

Sent messages can be customized in two ways:

  • Text customization — you can set a default text in the document type that will be used when sending documents. Custom templates are managed via the sablona-mail record type; see Custom email templates in the REST API.

  • Template customization — described in the next section.


Customizing the email message template

The basis for this is enabling the developer directory (developerDirectory). The full description is quite long; here we're only interested in setting it up and then modifying the template:

  1. In the file flexibee-server.xml (where to find it?), add an entry with the directory where the modification data will be stored: <entry key="developerDirectory">/devel/</entry>

  2. Restart ABRA Flexi.

  3. In that directory, create a directory named default — the so-called default instance; any other value only makes sense in a cloud deployment — and inside it, create a directory with the company identifier that matches the one used in the web interface. Alternatively, you can use the special identifier !all.

  4. Copy the sample scripts into $developerDirectory/$instance/!all; the important files are those in the mail-templates directory.


Automatic sending of documents

ABRA Flexi supports automatically sending all documents that have not yet been sent but are marked for sending — i.e., those whose stavMailK property has the value stavMail.odeslat (in the application, the Mail status field = Send). Other possible values are stavMail.neodesilat and stavMail.odeslano.

Sending can be triggered manually by calling:

PUT https://demo.flexibee.eu/c/demo/faktura-vydana/automaticky-odeslat-neodeslane
Accept: application/xml

🚨 This call sends emails for all documents marked for sending at once, so make sure you know in advance how many documents this affects — for example, by using the /c/{firma}/faktura-vydana.json?detail=custom:kod,stavMailK&filter=stavMailK%20eq%20'stavMail.odeslat' query.

Instead of demo, enter the system name of your company — you can find it, for example, in the URL after logging into the web interface. If you're running Flexi on your own server, use your server's address instead of demo.flexibee.eu.


Related articles

Did this answer your question?