Email: attaching files uploaded by the end user
NOTE This feature is only available in xxllnc Expertsystemen web.
Besides documents created by the model and fixed files that belong to the model, the Email action can also send files that the end user uploaded while running the model. This takes three steps:
- let the end user upload files,
- collect the names of the uploaded files in a variable,
- use that variable as the attachment of the email.
1. Let the end user upload files
The upload widget is part of the file upload plugin, see Plugin: file upload for all its options. The smallest setup uses three Action rules (Add a rule > Action) in the node that shows the widget:
setmetadata('uploads.0.id', 'attachments')
setmetadata('uploads.0.text', 'Upload the files to attach to the email')
setmetadata('uploads.0.multiple', 1)
setmetadata() is used for what it does, not for its return value, so use it as an action, not as a formula. With multiple set to 1 the end user can upload more than one file.

2. Collect the names of the uploaded files
The email must be sent from a node that comes after the node with the upload widget. That way the files are there by the time the email is prepared.
In that next node, build a list of file names, separated by semicolons (;), using these rules in this order:
-
Formula
counter1with the value0. -
Formula
attachmentswith the value''. -
Formula
filewith the value'fluke'. Any text will do; it only makes sure the loop starts. -
A
Loopthat runs untilfile = '', with these three formulas inside:filewith the valuesessionfilename(counter1)attachmentswith the valueattachments + ';' + file, and the conditionNOT (pos('_', file) = 1)counter1with the valuecounter1 + 1
sessionfilename(INDEX) returns the name of the file at position INDEX in the current session, and an empty text if there is none. That is why the loop stops at file = ''.
The condition in the loop skips the file names that start with an underscore (_). Those are tag files: the upload widget creates one next to each upload, to store the optional metadata of that upload (see Tags in Plugin: file upload). They are not meant to be sent.
The resulting list starts and ends with a semicolon. That is fine: the email interface ignores empty items.
TIP While building, show the result in a text, e.g.
Files: ^{main.node_2.attachments}, to check the list before sending anything.

3. Use the list as the attachment
Add the email with Add a rule > Email, after the rules above, and fill in the dialog as described on the Email page. The Studio creates a graph with a node called sendmail; see the section Under the hood on that page.

The Attachments field in the dialog only accepts files that belong to the model. You cannot add a variable directly there. So open the graph and edit the variable attachment in the node sendmail instead:
'^{main.node_2.attachments}'
Replace node_2 by the name of your node. The text between quotes is read as a text with a variable, just like in an on-screen text. The result is a list of names separated by semicolons, which is the format the email interface expects.
You can combine uploaded files with fixed files that belong to the model, by adding their names after the variable:
'^{main.node_2.attachments};terms-and-conditions.pdf'
Adding fixed files with Upload attachment in the email dialog works as well, and so does removing individual attachments there. This goes for fixed files and for a text that is built from uploaded files, like the one above. Your own edit of attachment in the graph is kept when you open and save the dialog again.