[SalesForce] Sending pdf from static resources as attachment in visualforce email template

I am trying to add pdf from static resources to my Visual force email template attachment tag. VF email template which will be used by a workflow.I was able to render a image file from static resource as pdf, but how do I get an existing pdf as pdf attachment in email template.
apex:page tag are not vaild in email template.
Also I tried reading he pdf body in a controller but it is not in readable format.

Please let me know how do I attach the pdf from static resources in visual force email template

Best Answer

Here is a working solution. First I read the static resource PDF in a blob and attached that blob with the email. I hope this will help.

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setUseSignature(false);
email.setSaveAsActivity(true);
email.setSubject('Email using Apex');
String[] toAddresses = new String[] {'xxxxxxxx@gmail.com'};
email.setToAddresses(toAddresses);
email.setHtmlBody('<html><body>Hi <b>Saroj</b></body></html>');
StaticResource sr = [Select  s.Name, s.Id, s.Body From StaticResource s where s.Name = 'static_resource']; // 'static_resource' is the name of the static resource PDF.
Blob tempBlob = sr.Body;
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setBody(tempBlob);
efa.setFileName('attachment.pdf');
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
Messaging.SingleEmailMessage[] emailList = new Messaging.SingleEmailMessage[] {email};
Messaging.sendEmail(emailList);
Related Topic