visualforce email template with attachment

Create the Email template

<messaging:emailTemplate subject="offer Process" recipientType="Offer_Process__c"
 relatedToType="Offer_Process__c" replyTo="noreply@difc.com">
    <messaging:attachment renderAs="PDF" filename="LeadOfferUnitPDFTemplate.pdf">
        <c:offerProcessAttachment offerId="{!relatedTo.Id}"/>
    </messaging:attachment>
    <messaging:htmlEmailBody >
    <html>
        Please find your invoice attached.
    </html>
    </messaging:plainTextEmailBody>
</messaging:emailTemplate>

Create the component

<apex:component controller="offerUnitAttachmentController" access="global">
    <apex:attribute name="offerId" description="Contact Id" assignTo="{!leadObjectId}" type="Id" />
    <apex:outputText value="{!PageContents}" escape="false" />
</apex:component>

Controller

global class offerUnitAttachmentController {

  global String PageContents{ get; set; }
  global String leadObjectId{
    get;
    set {
        UpdateContents(value);
    }
  }

  public void UpdateContents(String contactObjectID) {
    try {
        PageReference pageRef = Page.LeadOfferUnitPDFTemplate;
        pageRef.getParameters().put('id',leadObjectId);
        PageContents = pageRef.getContent().toString().replace('<html style="display:none !important;">', '<html>');
    } catch(exception ex) {
        PageContents = 'An error has occurred while trying to generate this invoice.  Please contact customer service.' +
                       '\n\nError Message:' + ex.getMessage();
    }
  }
}

PDF page Template  LeadOfferUnitPDFTemplate

<apex:page controller="ReceiptController" applyHtmlTag="false" applyBodyTag="false" showHeader="false" sidebar="false">
<html>
<span>Dynamic PDF data:  <apex:outputText value="{!ContactName}" escape="false"/></span>
</html>

</apex:page>

No comments:

Post a Comment