Parts: splitting and renaming email attachments

In a Control Script, parts can be defined to determine which sections should be output to the same file. This way it is possible to split the Print context or the Web context into multiple email attachments. This topic shows how to do that.

For information about Control Scripts in general, see Control Scripts and Control Script API. If you don't know how to write scripts, see Writing your own scripts.

Defining parts

Defining parts is done by setting the part field on a section, for example:

Copy
 merge.template.contexts.PRINT.sections['Section 2'].part = "PDF_Attachment2";. 

(Also see section and Control Script API.)

  • If a part name is given, then that delimits the start of a new part (even if the part name is the same as the previous one). Following sections that don't define a part name, will be added to the previous part.
  • A part ends at the last enabled* section or at the last section before the start of a new part.
    *When a Control Script has set the enabled field of a section to false, it will not be outputted.

If no part name is set on any section, it is assumed that there is only one part, consisting of the default section (for Web output) or of all sections (for Print output). The attachment(s) will be named after the email subject.

Examples

No parts defined

Assume there are three Print sections: sections A, B and C. When generating Email output with the Print context as attachment, all three Print sections will be put together in one file and attached to the email. If the email's subject is 'Take action', the name of the attached file will be 'Take action.PDF'.

Splitting and renaming a Print attachment

Assume there are three Print sections: sections A, B and C. In a Control Script a part name is defined for section C:

Copy
var section = merge.template.contexts.PRINT.sections['Section C'];
section.part = 'Part2';

When generating Email output with the Print context as attachment, the email will have two attachments:

  • attachment 1: Section A, Section B
  • attachment 2: "Part2", which is Section C. The file name of this attachment is the part name.

Note: For Web sections, a part always consists of only the given section. Web pages cannot be appended to form a single part. It is however possible to attach multiple Web pages to one email; see the following example.

Controlling multiple Email attachments

The following script attaches the following sections to an email:

  • Print section 3 + 4 as attachment with continued page numbers
  • Print section 6 as separate attachment
  • Web sections A and B as separate attachments
if (channel == Channel.EMAIL) { // only when generating Email output
if (merge.context.type == ContextType.PRINT) {
merge.context.sections['Section 1'].enabled = false;
merge.context.sections['Section 2'].enabled = false;
merge.context.sections['Section 3'].enabled = true;
merge.context.sections['Section 3'].part = "PDFAttach1";
merge.context.sections['Section 4'].enabled = true;
merge.context.sections['Section 4'].restartPageNumber = false;
merge.context.sections['Section 5'].enabled = false;
merge.context.sections['Section 6'].enabled = true;
merge.context.sections['Section 6'].part = "PDFAttach2";
}
else if (merge.context.type == ContextType.WEB) { // disable whatever is the default section
merge.context.sections['default Section'].enabled = false; merge.context.sections['Section A'].enabled = true;
merge.context.sections['Section A'].part = "WebPartA";
merge.context.sections['Section B'].enabled = true;
merge.context.sections['Section B'].part = "WebPartB";
}

}

Note: For another example, see this how-to: Output sections conditionally.