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

Sending Visualforce page by email


I'm brand new in the world of visualforce. I made a VF-page on a custom object, rendered as pdf. This works. Now I'd like to have the possibility to send this VF-page easily to a contact person as an attachment. Can someone help me please ? What do I have to add to the VF-page itseld, what to 'components', what to 'classes' etc... ?

Many thanks in advance.



Hi beHappy,


Just u need to write a email attachment method and send email method in the class 

In the visualforce page create a button send email call the email method in that action.

Then u can send ur vf page as email.

Just check the above link for more reference.





Many thanks for this quick reply. I'll try it out and I'll give you feedback.


Hi Anilbathula,

I'm trying out what's explained in the hyperlink you sent me but am facing some problems. Like I told, It's all brand new for me and I've nearly no experience at all.  Am I right that I must have the following ?

-VF page named attachmentpdf

- VF page named sendmail

- VF component named attachment (error : unknown property 'account')

- Apex class named sendEmail (any help for the testclass ?)

It would be great if you could set me on the right way.

Many thanks in advance.




Hi here is a simplified version :- 


Since your PDF visual force page is working you need to create one more visual force page and its associated controller:- 


VF Page:- 

<apex:page controller="sendEmail">
    <apex:pageBlock title="Send an Email to Contact">
            <apex:commandButton value="Send Email" action="{!send}"/> 



public class sendEmail {
    public String subject{get;set;}
    public String body{get;set;}
	public List <String> ToAddresses{get;set;}
    public sendEmail() 
		ToAddresses = new List <String>();
		ToAddresses.add(''); //Add your custom logic to extract email of contact  

    public PageReference send() 
        // Define the email 
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        PageReference pdf =  Page.attachmentPDF;//Replace attachmentPDF with the page u have rendered as PDF

        // Take the PDF content
        Blob b = pdf.getContent();

        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();

        // Sets the paramaters of the email 
        email.setSubject( subject );
        email.setToAddresses( ToAddresses );
        email.setPlainTextBody( body );

        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); // Sends the email 
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
        return null;




Many thanks for your quick reply. I've following Compile Error : Incompatible types since an instance of Schema.SObjectField is never an instance of String at line 17 column 38. This is the following line :

pdf.getParameters().put ('id', (String);

Can you also suggest me a testclass please ?

Sorry for all these questions, but I've a lot to learn and I'm sure I'm here by the right people to do that.




Try Replacing that line by - 




As for Test Class the below is a great way to get started -



thanks for you reply. I tried your correction and now I've following error : Compile Error: Incompatible value type Schema.SObjectField for MAP<String,String> at line 17 column 29

I'll go to your hyperlink for the testclasses. thx