I'm getting error when trying to send visualforce page as email attachment.
public with sharing class PdfEmailController {
// public ID PAId {get;set;}
// public String email {get;set;}
public static void sendPdf(String PAId,String email) { // email needs to be fixed to encompass PM
PageReference pdf = Page.PA_Detail;
//PageReference pdf = Page.success;
// add parent id to the parameters for standardcontroller
pdf.getParameters().put('id',PAId);
pdf.setRedirect(true);
// the contents of the attachment from the pdf
Blob body;
try {
// returns the output of the page as a PDF
body = pdf.getContent();
// need to pass unit test -- current bug
} catch (Exception e) {
System.debug('Exception'+e);
//body = Blob.valueOf('Some Text');
}
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
//attach.contentType('application/pdf');
attach.setFileName(PAId + '.pdf');
// attach.setInline(false);
attach.setBody(body);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] { email });
mail.setSubject('PA has been Approved');
mail.setHtmlBody('Here is the copy of approved PA! Check the attachment!');
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with PDF sent to '+email));
// return null;
}
}