+ Start a Discussion

Send Visualforce Email Template as PDF Attachment

I have a custom visualforce Email Template and want to email it but as an attachment.  Is this possible to do or what would be my best approach?  I could just create a regular visual force page and render as pdf I know but wondering if I can salvage the EmailTemplate in any way and send it as an attachment.  Thoughts??
Still need help, I found a method in blob that i used below, but for some reason the getHtmlBody returns null on my piece of mail:
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        String[] toAddresses = new String[] {'example@sfdc,com'};
        EmailTemplate template = [Select id from EmailTemplate where name=:'testing visualforce template'];

        Messaging.EmailFileAttachment attachmentFile = new Messaging.EmailFileAttachment();
	attachmentFile.setFileName('TestPDF.pdf'); // neat - set name of PDF

        Opportunity opp = [SELECT id, OwnerId from Opportunity where id=:jo.Opportunity__c]; 


        mail.saveAsActivity = false;

        String mailString = 'Testing';//mail.getHtmlBody();

and after that i send out the email, and i get the email as I should, the email template data, but there is nothing in the getHtmlBody or getPlainTextBody fields of the mail object.  how do i pull the data from the mail object that it received from the email template?
are you using  a static attachment or it has merge fields (dynamic) ?
It is dynamic.
You can make use of Visualforce Components inside your template to achieve this, below is a sample code:
Component Code:
<apex:component controller="OpptyProductController" access="global">
 <apex:attribute access="global" assignTo="{!opptyId}" name="opportunityId" type="String" required="true" description="Opportunity Id"/>
 <b>products Details</b>
 <table border="1" cellSpacing="0" cellpadding="0" width="100%" >
 <th>Product Name</th>
 <th>Sales Price</th>
 <apex:repeat value="{!OpptyProducts}" var="product">
 <td>{!product.Description }</td>

Sample Controller:
Public Class OpptyProductController {
   public String opptyId{get;set;} /** get set the variable for opportunity Id take value from visual component.**/
   public List<OpportunityLineItem> getOpptyProducts() {
        return [Select Quantity, UnitPrice, ListPrice,ServiceDate, Description ,PricebookEntry.Product2.Name
                From OpportunityLineItem where OpportunityId =: opptyId];

Finally the VF template:
<messaging:emailTemplate subject="Opportunity '{!relatedTo.Name}' Detail with Products" recipientType="User"
        relatedTo="opportunity >
<messaging:htmlEmailBody >
Hi {!relatedTo.Owner.name} ,<br/>
your opportunity '{!relatedTo.Name}' has beed closed won.<br/>
Please find their products detail as attachment.
<messaging:attachment renderAs="pdf"> <!-- Use the renderAs attribute here.-->
    <c:OpptyProduct opportunityId="{!relatedTo.Id}"></c:OpptyProduct><!--gives the reference of the component-->
Trigger the above stuff as below:
Id templateId = 'place_your_vf_template_id';
Id someOpportunity = 'place_your_record_id';
Messaging.singleEmailmessage em = new Messaging.singleEmailMessage();
em.setToAddresses( new List<String>{ 'example@example.org' } );
Messaging.sendEmail( new List<Messaging.Email>{em} );

Let me know the results.
Ravikiran N CRavikiran N C
@JJames , are you able to finish this requirement , I need some help on this , Please let us know your implementation