Find your content:

Search form

You are here

Get VisualForce Email Template as Blob


Similar to how we can use apex to get the blob value of a VisualForce page, can we also do this on a VisualForce Email Template? We need to get the rendered template into an attachment record in Salesforce.

Attribution to: Joey Chan

Possible Suggestion/Solution #1

Select e.TemplateType, e.TemplateStyle, e.Subject, e.Name, e.Id, e.Body From EmailTemplate e

I tried this with this query .For Visualforce Email template I did not get anything in Body while for Text Template we get the body.

Hence i dont think it will be possible to extract data as blob for visualforce email template

Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #2

One approach might be to factor the bulk of your Email Template code (which of course is just VF markup anyway) into a Visualforce Component. Thus enabling you to share it by referencing this component between a Visualforce Page (to enable you to capture it on your attachments) and Email Templates (for when your emailing). For example.

<apex:component access="global">
  <apex:attribute name="targetObject" type="Test__c" description="The context of the outer page / email templates" access="global"/>
  <p>This is your new Component that is shared between your Email Template and Visualforce Page</p>
  <p>This is the name of your record {!targetObject.Name}</p>

Your email template would look like this...

<messaging:emailTemplate subject="Test" recipientType="User" relatedToType="Test__c">
    <messaging:plainTextEmailBody >
        <c:ShareComponent targetObject="{!relatedTo}"/>

Your VF page would look like this...

<apex:page standardController="Test__c">
    <c:ShareComponent targetObject="{!Test__c}"/>

Hope this helps!

Note: Note that Visualforce Components when delivered in Managed Packages are none editable, unlike Email Templates. You can work around this though, by copy and pasting the component code into a new one in the subscriber code and changing the template to point to the new component.

Attribution to: Andrew Fawcett
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content