function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raj R.Raj R. 

How to attach pdf from visualforce page in an automated email using apex class or trigger?

Hi,

We have a VisualForce (VF) page that renders as a pdf. I have an automated email that is going to be trigger when criteria A, b, and C are met. 

How can i attach the pdf from the VF page as an email attachment in the automated email that is sent from an apex trigger or class?
Best Answer chosen by Raj R.
Gyanender SinghGyanender Singh
Hi,

As per your requirement you have a vf page name is test and you want to send that page as attachment in the mail so for this please use this method in your apex class:
 
public PageReference sendEmail() 
    {    
        PageReference pdf;
        Blob attbody;
        Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        pdf = Page.TestVfPageName;
		pdf.setredirect(true);
		if(!Test.IsRunningTest()){
			attbody = pdf.getContent();
		    efa1.setFileName('test Vf Page Name.pdf');
			efa1.setBody(attbody);
			fileAttachments.add(efa1);
		}
        string htmlbody = '';
        String subject = 'Her you can set the subject of the mail';
        htmlbody = 'hi here you can set the body content which you want to sent in mail';
        email.setSubject(subject);
        email.setToAddresses(ToAddresses);
        email.setHtmlBody(htmlbody);
        email.setFileAttachments(fileAttachments);
		mails.add(email);  
        Messaging.sendEmail(mails);
		return null;
}



and in the constructor of the class fetch all email in the ToAddresses list like as:

 List <String> ToAddresses = new List <String>();
ToAddresses.add(obj.Email);

write this two lines in your constructor.

Please let me know if you have any other doubt and if this is the anser of your problem so please select this as the Best Answer.

Thanks
Gyani.

All Answers

Hargobind_SinghHargobind_Singh
Maybe not the exact answer you are looking for, but this might help: 

http://salesforce.stackexchange.com/questions/22251/insert-attachment-into-visualforce-email-template
 
Chandra Prakash PandeyChandra Prakash Pandey
Hi,

Try this code.
PageReference pdf =  Page.testPdfPage;
Blob pdfContent = pdf.getContent();
// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('Mail with PDF Attachment!');
String [] toAddresses = new String[] {'recepient address email Id'};
email.setToAddresses(toAddresses);
email.setPlainTextBody('Here is the body of the email');
// Create an email attachment
Messaging.EmailFileAttachment pdfAttachmentObj = new Messaging.EmailFileAttachment();
pdfAttachmentObj.setFileName('TestPDF.pdf'); // neat - set name of PDF
pdfAttachmentObj.setBody(pdfContent); //attach the PDF
email.setFileAttachments(new Messaging.EmailFileAttachment[] {pdfAttachmentObj});
// send it, ignoring any errors (bad!)
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

Regards,
Chandra Prakash
Gyanender SinghGyanender Singh
Hi,

As per your requirement you have a vf page name is test and you want to send that page as attachment in the mail so for this please use this method in your apex class:
 
public PageReference sendEmail() 
    {    
        PageReference pdf;
        Blob attbody;
        Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        pdf = Page.TestVfPageName;
		pdf.setredirect(true);
		if(!Test.IsRunningTest()){
			attbody = pdf.getContent();
		    efa1.setFileName('test Vf Page Name.pdf');
			efa1.setBody(attbody);
			fileAttachments.add(efa1);
		}
        string htmlbody = '';
        String subject = 'Her you can set the subject of the mail';
        htmlbody = 'hi here you can set the body content which you want to sent in mail';
        email.setSubject(subject);
        email.setToAddresses(ToAddresses);
        email.setHtmlBody(htmlbody);
        email.setFileAttachments(fileAttachments);
		mails.add(email);  
        Messaging.sendEmail(mails);
		return null;
}



and in the constructor of the class fetch all email in the ToAddresses list like as:

 List <String> ToAddresses = new List <String>();
ToAddresses.add(obj.Email);

write this two lines in your constructor.

Please let me know if you have any other doubt and if this is the anser of your problem so please select this as the Best Answer.

Thanks
Gyani.
This was selected as the best answer
mac adminmac admin
Hi Gyanender Singh,
I have tried your method and placed the two lines in my constructure but I'm getting error as Error: Compile Error: unexpected token: ')'
can you help me over here.

regards,
mac.