Template Engine language reference > Template Engine utilities > Attachment functions > $attachments.addAttachment

$attachments.addAttachment
This function is used to attach a file, referenced by a URI, to an email.
When the $attachments.addAttachment() function is called it creates an attachment output stream in which the file data is sent. It also generates a unique identifier, a cid, associated with the file data in the output stream. The cid, which is an ordinary string, is returned to the caller. The cid can be used in, for example, <img> and <a> tags to reference the file data.
Example 114
The following directive creates an attachment output stream for the file C:\images\Img1.jpg and returns a cid associated with the file data:
#set($img=$attachments.addAttachment('C:\images\img1.jpg',
'im.jpg', 'image/pjpeg'))
In the Template Engine template <img src="$img"/> is used to reference the file data. The Template Engine output will have the cid as reference, for example:
<img src="cid:57E498EB-8E08-9E46-9FCC-B59CB41280CB">
 
Syntax
$attachments.addAttachment(<uri>, <name>, <contentType>)
uri
Use a file path if the file is available on a local file system or network share. You can specify absolute paths or paths relative to the StreamServer working directory.
Use http:// if the file is available on a web server or content management system.
Use resource:// if the file is stored as a resource in the runtime repository. See Resource output connector in the Connectors documentation or Resource filter in the Design Center documentation for information about how to store the resources.
Use otmm:// if the file is an OpenText Media Management (OTMM) asset. You must first create a Media Management connection profile (see the Profile configurations documentation). When you have the connection profile you can reference an asset using the following URI:
otmm://<profileName>/<reference>
Where <reference> is either the path to the resource or ?id=<GUID>, for example ?id=9E664C0E-807F-024C-8885-659C6EC6E9C8.
name
contentType
The content type of the file. For example, image/pjpeg for a JPEG image.
Calling $attachments.addAttachment() and referencing the attachment
In the Template Engine template you can call the $attachments.addAttachment() function and reference the corresponding attachment in different ways. Some valid example are illustrated below.
Example 115
<img src="$attachments.addAttachment('C:\abc\img.jpg', 'im.jpg', 'image/pjpeg'))"/>
#set($att="$attachments.addAttachment('C:\abc\img.jpg', 'im.jpg', 'image/pjpeg'))")
<img src="$att"/>
#set($path='C:\abc\img.jpg')
#set($name='im.jpg')
#set($ct='image/pjpeg')
<img src="$attachments.addAttachment($path, $name, $ct)"/>
 
Attaching files
If you only declare a variable for the attachment, e.g. #set($img=$attachments.addAttachment('C:\images\img1.jpg', 'im.jpg', 'image/pjpeg')), the file is added as an attachment to the email. The email recipient must click the attachment to view the content in this case.
Note:
If the attachment name does not have the appropriate extension (e.g. im.jpg) the email client cannot determine which viewer to use.
Using links to reference attachments
If you add an <a> tag to the Template Engine template, the email recipient can click a link to view the content of an attachment. For example:
#set($r=$attachments.addAttachment('C:\abc\img.jpg', 'im.jpg', 'image/pjpeg'))
<a href="$r">Link text</a>
Note:
If the attachment name does not have the appropriate extension (e.g. im.jpg) the email client cannot determine which viewer to use.
Displaying attached images in the email body
If the attachment is an image you can add an <img> tag to the Template Engine template and have the image displayed in the email body. For example:
#set($r=$attachments.addAttachment('C:\abc\img1.jpg', 'Dog', 'image/pjpeg'))
<img src="$img1"/>
Note:
Referencing different files using the same attachment name
If you try to reference different files using the same attachment name, the cid is associated with the file you reference first. This means the same file will be used for both references in the output.
Example 116
Template
#set($abc=$attachments.addAttachment('C:\images\Dog.jpg', 'Animal.jpg', 'image/pjpeg'))
<img src="$abc"/>
#set($xyz=$attachments.addAttachment('C:\images\Cat.jpg', 'Animal.jpg', 'image/pjpeg'))
<img src="$xyz"/>
Output
<img src="cid:70179CF0-C9A0-5D4D-B4D2-32CFBC5E0639">
<img src="cid:70179CF0-C9A0-5D4D-B4D2-32CFBC5E0639">
 
Attaching document definition resources
The $attachments.addAttachment function is used to attach external resources to an email. To attach resources in a document definition to an email you must use the toAttachment() function. See toAttachment().
Note:
If you use the same attachment name in a toAttachment() function and in an $attachments.addAttachment function, both references will have the same cid.
Template examples
Example 117
Template
#set($img=$attachments.addAttachment('C:\abc\Dog.jpg', 'Dog.jpg', 'image/pjpeg'))
<p>See attachment Dog.jpg.</p>
Output
<p>See attachment Dog.jpg.</p>
Comment
The file Dog.jpg is attached as Dog.jpg to the email. The recipient must click the attachment to view the content.
 
Example 118
Template
#set($info=$attachments.addAttachment('C:\abc\Info.txt', 'Info.txt', 'text/plain'))
<p><a href="$info">See this information</a></p>
Output
<p><a href="cid:06474328-D808-B642-B748-E8D294283E49">See this information</a></p>
Comment
The file Info.txt is attached as Info.txt to the email. The recipient must click the "See this information" link to view the content.
 
Example 119
Template
#set($img=$attachments.addAttachment('C:\abc\Dog.jpg', 'Dog.jpg', 'image/pjpeg'))
<img src="$img"/>
Output
<img src="cid:6E8D6107-90E2-6E41-99BF-308E7E93C2FA">
Comment
The file Dog.jpg is attached as Dog.jpg to the email and displayed in the email body.
 
Example 120
Template
#set($img=$attachments.addAttachment('..\data\images\Dog.jpg', 'Dog.jpg', 'image/pjpeg'))
<img src="$img"/>
Output
<img src="cid:6E8D6107-90E2-6E41-99BF-308E7E93C2FA">
Comment
Same as the example above, but the reference is relative to the working directory of the StreamServer application.
 
Example 121
Template
#set($css1=$attachments.addAttachment("C:\abc\myCss.css", "TheCss.css", "text/css"))
<!DOCTYPE html>
<html>
<head><link rel="stylesheet" href="$css1"></head>
Output
<!DOCTYPE html>
<html>
<head><link rel="stylesheet" href="cid:8F19F2DE-F115-DA4B-9B40-30C9078D7E6A"></head>
Comment
The file myCss.css is attached as TheCss.css to the email and referenced in the <link> tag. This CSS is used as stylesheet for the email.
 
 
OpenText StreamServe 5.6.2 Updated: 2018-01-26