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:

Email with Multiple Attachments - Basic Task

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 ReportClosed The Run Crystal Report tool automates the execution of SAP® Crystal Reports®. Recordsets and variables from other task steps can be consumed to dynamically populate the report at runtime. In addition, where the report requires access to a database, security credentials can be passed from in the Run Crystal Report step. tool
► Save PDFs — Each Crystal Report is saved as an individual PDF, using a dynamic filename
► SE - Email Orders to Customers — An email is sent for each individual PDF to the relevant customer using the Send Email (SMTP)Closed The Send Email (SMTP) tool sends messages to any SMTP compliant mail server. This tool is capable of sending multiple messages in either text or HTML formats, incorporating data from Input and Format steps to any number of recipients. If recordset data being used from an Input step includes a column containing email addresses, then this can be used as a "dynamic" recipient address. tool

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 TextClosed 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.

  1. Add a Format as Text step to the task.
  2. In the General tab, Name your task step and consume the recordset outputted by the Database Query step.
  3. Go to the Formatting tab.
  4. 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.

  5. Click OK to save and close the step.
  6. Change the task flow so that this step is after the Save File step and before the Send Email (SMTP) step:

Multiple Attachments - Full Task

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:

  1. Open the Send Email (SMTP) step.
  2. Consume the recordset from the Format as Text step.
  3. 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 ScheduleClosed 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 ProClosed 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