Objectif Lune joins Upland Software.Learn more >

Back to all How-tos

Adding attachments to an email using scripts

Note! As of OL Connect version 2018.1, attachments can be added via the user interface, without scripting. This is how it works.

This How-To will show you how to write a script that inserts your own file attachments when sending email through the Designer module, or through automation using the Create Email Contents task in the Workflow module.

Remember that email clients have their limitations, and a lot of them still think that 10 or 20MB is a valid limit for file attachments. If sending attachments larger than 10MB, it’s suggested to use online file sharing instead, such as Dropbox, OneDrive, Google Drive, etc.

Method 1: Adding to the document

You can test this method by creating a simple Email Template using Connect Designer.

Adding an attachment simply requires a (generally) one-line script, which follow this basic idea:

  1. Add a script (just “Script”, not a text script or otherwise) with the head selector
  2. Append the correct code which follows the format

Here is a basic example of the actual script, which of course needs to be adjusted for your own requirements. You can link any file you like:

results.append("<link rel='related' href='file:///c:/...'>");

The <link> tag is used to define a link between the connect document and the linked file.

The “rel” parameter must be set to ‘related’.

The “href” parameter determines where the file comes from. For resources inside of the template, use ‘images/file.extension’ , or ‘fonts/myfont.otf’, etc. For external resources, you need the full path to the file, such as ‘file:///c:/resources/attachments/instructions.pdf’. Of course, you can also use dynamic calls such as ‘file:///c:/clientfiles/’+record.fields.client_id+’/invoices/’+record.fields.invoice_number+’.pdf

More information about the append() function is available here.

To test this method using an email template, click on the Send Test Email button .

Method 2: Using Control Scripts for a lot more... control

Adding static resources as attachments is all well and good, but what if you want to do more advanced stuff? The Control Script API gives you that power. With it, you can not only rename attachments but also include different sections in a Print Context, separately or merged together.

To test this example, you can create an Email Template using Connect Designer and add the following:

  • A Print Context containing 6 Sections each named “Section 1” through “Section 6” and with different identifiable content on them (i.e.: “Section X” where X corresponds to the section number for example).
  • A Web Context containing 2 Sections named “Section A” and “Section B”.

This example will merge, in a template, Section 3 and Section 4 as a single attachment with continued page numbers. It also includes Section 6 as a seperate attachment, and finally Sections A and Section B from a Web context as 2 last seperate attachments, for a total of 4 attachments. Simply add a new Control Script and replace the contents with the following (in a template that has 6 Print sections and 2 Web sections along with the email context…):

if (merge.channel == Channel.EMAIL) { // only when generating for EMAIL
if (merge.context.type == ContextType.PRINT) {
merge.context.sections['Section 1'].enabled = false; // Disables Section 1 assuming it's the default one
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 6'].enabled = true;
merge.context.sections['Section 6'].part = "PDFAttach2";
} else if (merge.context.type == ContextType.WEB) {
merge.context.sections['default Section'].enabled = false; // disable whatever is the default section
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";
}
}

To test this method using an email template, click on the Send Test Email button .

Leave a Reply

Your email address will not be published. Required fields are marked *