Newer Version Available

This content describes an older version of this product. View Latest

Adding Attachments

You have the ability to add attachments to your Visualforce email templates. Each attachment must be encapsulated within a single <messaging:attachment> component. Code within <messaging:attachment> can be a combination of HTML and Visualforce tags.

The previous example shows how to create a Visualforce email template by iterating through some data and displaying it to an email recipient. This example shows how to modify that markup to display the data as an attachment:
1swfobject.registerObject("clippy.codeblock-0", "9");<messaging:emailTemplate recipientType="Contact"
2	relatedToType="Account"
3	subject="Case report for Account: {!relatedTo.name}"
4	replyTo="support@acme.com">
5
6	<messaging:htmlEmailBody>
7		<html>
8			<body>
9			<p>Dear {!recipient.name},</p>
10			<p>Attached is a list of cases related to {!relatedTo.name}.</p>
11			<center>
12				<apex:outputLink value="http://www.salesforce.com">
13					For more detailed information login to Salesforce.com
14				</apex:outputLink>
15			</center>
16			</body>
17		</html>
18	</messaging:htmlEmailBody>
19
20	<messaging:attachment>
21		<apex:repeat var="cx" value="{!relatedTo.Cases}">
22			Case Number: {!cx.CaseNumber}
23			Origin: {!cx.Origin}
24			Creator Email: {!cx.Contact.email}
25			Case Number: {!cx.Status}
26		</apex:repeat> 
27	</messaging:attachment>
28</messaging:emailTemplate>
29
This markup renders in an email as an attached data file, without any formatting. You can display the data in a more readable format by using one of the following options:

Changing the Filename

The <messaging:attachment> tag has an attribute called filename that defines the name of the attached file. While it is good practice to define an easily identifiable name, it is not required. If you leave it undefined, Salesforce generates a name for you.

A filename without an extension defaults to a text file. You can render an attached file as a CSV:
1<messaging:attachment filename="cases.csv">
2	<apex:repeat var="cx" value="{!relatedTo.Cases}">
3		{!cx.CaseNumber}
4		{!cx.Origin}
5		{!cx.Contact.email}
6		{!cx.Status}
7	</apex:repeat> 
8</messaging:attachment>
You can also render the data as an HTML file:
1<messaging:attachment filename="cases.html">
2	<html>
3		<body>
4		<table border="0" >
5			<tr>
6				<th>Case Number</th><th>Origin</th>
7				<th>Creator Email</th><th>Status</th>
8			</tr>
9			<apex:repeat var="cx" value="{!relatedTo.Cases}">
10			<tr>
11				<td><a href = 
12					"https://na1.salesforce.com/{!cx.id}">{!cx.CaseNumber}
13				</a></td>
14				<td>{!cx.Origin}</td>
15				<td>{!cx.Contact.email}</td>
16				<td>{!cx.Status}</td>
17			</tr>
18			</apex:repeat> 
19		</table>
20		</body>
21	</html>
22</messaging:attachment>

Although you can only define one filename for every <messaging:attachment> component, you can attach multiple files to an email.

Changing the renderAs Attribute

Similar to other Visualforce pages, setting the renderAs attribute to PDF on a <messaging:attachment> component renders the attachment as a PDF. For example:
1<messaging:attachment renderAs="PDF" filename="cases.pdf">
2	<html>
3		<body>
4		<p>You can display your {!relatedTo.name} cases as a PDF:</p>
5			<table border="0" >
6			<tr>
7				<th>Case Number</th><th>Origin</th>
8				<th>Creator Email</th><th>Status</th>
9			</tr>
10			<apex:repeat var="cx" value="{!relatedTo.Cases}">
11			<tr>
12				<td><a href = 
13					"https://na1.salesforce.com/{!cx.id}">{!cx.CaseNumber}
14				</a></td>
15				<td>{!cx.Origin}</td>
16				<td>{!cx.Contact.email}</td>
17				<td>{!cx.Status}</td>
18			</tr>
19			</apex:repeat> 
20		</table>
21		</body>
22	</html>
23</messaging:attachment>
Limitations of the Visualforce PDF rendering service include:
  • PDF is the only supported rendering service.
  • Rendering a Visualforce page as a PDF is intended for pages designed and optimized for print.
  • Standard components that aren’t easily formatted for print, or form elements like inputs, buttons, or any component that requires JavaScript to be formatted, shouldn’t be used. This includes, but isn’t limited to, any component that requires a form element.
  • PDF rendering doesn’t support JavaScript-rendered content.
  • Font used on the page must be available on the Visualforce PDF rendering service. Web fonts aren’t supported.
  • If the PDF fails to display all of the page’s text, particularly multi-byte characters such as Japanese or accented international characters, adjust the fonts in your CSS to use a font that supports them. For example:
    1<apex:page showHeader="false" applyBodyTag="false" renderAs="pdf">
    2    <head>
    3        <style>
    4            body { font-family: 'Arial Unicode MS'; }
    5        </style> 
    6    </head>
    7    <body>
    8    
    9    これはサンプルページです。<br/>
    10    This is a sample page: API version 28.0
    11    
    12    </body>
    13</apex:page>
    “Arial Unicode MS” is currently the only font supported for extended character sets that include multi-byte characters.
  • If you use inline CSS styles, you must set the API version to 28.0 or greater, set <apex:page applyBodyTag="false">, and add static, valid <head> and <body> tags to your page, as in the example above.
  • The maximum response size when creating a PDF must be below 15 MB before being rendered as a PDF. This is the standard limit for all Visualforce requests.
  • The maximum file size for a generated PDF is 60 MB.
  • The maximum total size of all images included in a generated PDF is 30 MB.
  • PDF rendering doesn’t support images encoded in the data: URI scheme format.
  • Note that the following components do not support double-byte fonts when rendered as a PDF:
    • <apex:pageBlock>
    • <apex:sectionHeader>
    These components aren’t recommended for use in pages rendered as a PDF.

Adding Styles and Images

Attachments can also use stylesheets to change the way your data is presented. Styles are associated with attachments the same way as they are in Visualforce email templates, either as inline code, or by using a custom component.

Attachments rendered as PDFs can reference static resources through the $Resource global variable. This enables you to refer to an image or stylesheet within the body of the PDF.

For example, the following attachment includes a logo in the PDF:
1<messaging:attachment renderAs="PDF" filename="cases.pdf">
2		<html>
3			<body>
4			<img src = "{!$Resource.logo}" />
5			...
6			</body>
7		</html>
8	</messaging:attachment>
This attachment references a stylesheet you have saved as a static resource:
1<messaging:attachment renderAs="PDF">
2		<html>
3		<link rel='stylesheet' type='text/css' href='{!$Resource.EMAILCSS}' />
4			<body>
5			...
6			</body>
7		</html>
8	</messaging:attachment>

Referencing static resources on a remote server can increase the time it takes to render a PDF attachment. You can’t reference remote resources when creating PDF attachments in an Apex trigger; doing so will result in an exception.

Warning