Introduction
This article describes how to create a task that sends an email with attachments, where some emails require a single attachment and others multiple.
Example Scenario
You have a task that periodically emails customers with their order information. Some customers have placed a single order in that time-frame, others have placed multiple orders. Each order must be in a separate file.
Typically, you would create a task as:
where:
| ► | DBQ - Get Orders — The Database Query step extracts the orders and other relevant information from the database; the extracted records are ordered by ORDER_NUMBER. |
| ► | RCR - Create PDF Per Order — A Crystal Report template is populated with the order information using the Run Crystal Report |
| ► | Save PDFs — Each Crystal Report is saved as an individual PDF, using a dynamic filename |
- Add a Save File
The Save File tool saves output from another task step capable of producing document, as one or more files on the local drive, on-premise file server, external server, or cloud based server. step to the task. - In the General tab, Name your task step.
- Enable Read documents from a source (or Read XML documents from a source and save as XML file if processing XML files).
- Choose the relevant document output from the drop-down below.
- Go to the Main tab.
- Enter the required Destination folder Path.
- Drag the key recordset column from the Task Browser into Filename. Give it the appropriate file extension.
In this example, we are using the ORDER_NUMBER to form the filename as this is unique per order, resulting in something like ORDER-12345.PDF.
| ► | SE - Email Orders to Customers — An email is sent for each individual PDF to the relevant customer using the Send Email (SMTP) |
- Add a Send Email (SMTP) step to the task.
- In the General tab, Name your step.
- Enable Read documents from a source.
- From the drop-down, select the Run Crystal Report step.
- Go to the Connection tab and choose the relevant email server to process this emails.
- Go to the Main tab.
- Add a relevant From address (click From).
- Use the recordset column containing the email address as the To address (click To then New to create a dynamic address):

In the above example, we are also using the customer name (from the TAKEN_BY recordset column) as part of the email.
- Click Attach then Add.
- Enable Dynamic Attachment and Path To File On Disk.
- Enter the folder Path where the PDFs are located.
- Add a back-slash (
\) to the end of the folder path. - We must add the dynamic filename to Path but rather than using the Save File version, we must use the Send Email (SMTP) version so that the customer is tied to the order:
- In the Task Browser, go to the Environment tab.
- Change Scope to This Step.
- Drag the recordset column that forms the document name to be after the
\so you have something likeC:\Customer-Orders\ORDER-{=ThisStep.DocumentSource.RecordSet("ORDER_NUMBER")}.PDF:
- Go to the Options tab.
- From the Dynamic Attachment Options pane, enable Each Time Column Changes.
- Choose the recordset column that your original document generation is based on — for this scenario, this is ORDER_NUMBER.
If a customer has placed multiple emails during the requested time-frame, they would be bombarded with emails from this task. Instead, adjust the task as follows so a single customer receives a single email with an attachments for each order placed.
Combining Attachments per Recordset Value
To ensure the correct PDFs are attached to the email, we now must group the PDFs according to customer. To do this, we must create a second recordset for the task, ordered by customer. This effectively splits the task in half, where the first half consumes a recordset focussed on orders and the second on customers.
At the time of writing, no such tool exists for just consuming a recordset and outputting it reformatted. The Format tools available to you all must output some form of document which is not required by this task, with the exception of the Format as Text
The Format as Text tool creates single or multiple text documents, using information provided by Input tools such as the Database Query (ODBC) tool. It requires no global settings. tool. With this tool you can skip the document creation part without generating any errors.
Finally, we then need to adjust the Send Email (SMTP) step to consume from the Format as Text step.
Adding a Format as Text Step
We use the Format as Text tool as we don't want to change the documents, only create a recordset of sorts that the Send Email (SMTP) tool can use.
- Add a Format as Text step to the task.
- In the General tab, Name your task step and consume the recordset outputted by the Database Query step.
How Do I Do This?- Enable Use a Recordset.
- From the Input Recordset drop-down, choose the Database Query step.
- Go to the Formatting tab.
- Enable New document when column changes and choose the recordset column that groups the documents — for this scenario, we want the CUSTOMER column.
This creates the recordset where the records are ordered by CUSTOMER.
- Click OK to save and close the step.
- Change the task flow so that this step is after the Save File step and before the Send Email (SMTP) step:
Changing the Send Email (SMTP) Step
We need to change the Send Email (SMTP) step to consume the output from the new Format as Text step:
- Open the Send Email (SMTP) step.
- Consume the recordset from the Format as Text step.
How Do I Do This?- Enable Read documents from a source.
- From the Document Source drop-down, choose the new Format as Text step.
- If you used a dynamic filename for the PDFs and configured the file path as described above, no further changes are required for the Send Email (SMTP) step else change the dynamic attachment settings as described.
The Flow of Data Through the Task
When the task is run, the data flow is as follows:
| ► | Records are extracted from the database, ordered by ORDER_NUMBER, and saved to a recordset |
| ► | A PDF is generated and saved to disk for each record in the recordset |
| ► | An additional recordset is generated to group the records according to CUSTOMER |
| ► | An email is generated for each unique value in the second recordset |
| ■ | PDFs associated with that unique value are attached to the email |
How Can I Improve This Task?
The above example is a very basic task. Consider the following changes:
Add a Schedule Step
Currently, this task has no way of being automatically run. Consider adding a Schedule
The Schedule tool is used to trigger a task to run based on a date and time schedule. Any number of Schedule steps can be created for a task. step to have the task run at the same time every day, week, or month — see How to Add a Schedule Task Step.
Replace Format as Text with Format as HTML Pro
It can be assumed the email body is in plain text as we haven't added an HTML step to format the email body. The Format as HTML and Format as HTML Pro
The Format as HTML Pro tool is used to create a task step that produces single or multiple HTML documents. You can choose to create the HTML template from scratch or import a pre-existing template. Recordsets and other task step properties can also be used to populate the HTML document to product dynamic content documents. These documents can then be consumed and then delivered by Output or Execute task steps. tools can consume a recordset, generate a New document when column changes, and output an HTML email body and a recordset for the Send Email (SMTP) step to use.
Changing the Send Email (SMTP) Step
The only changes needed for the Send Email (SMTP) step are:
| ► | General tab — Consume the Format as HTML or Format as HTML Pro step. |
| ► | Main tab — Ensure Document Output in the Message Contents pane is enabled. This ensures the consumed HTML document forms the email body. |
For a detailed description of how to use either tool, see How to Add a Format as HTML Task Step and How to Add a Format as HTML Pro Task Step.
Article originally published 03 December 2015 — Article updated 04 November 2019