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
gbu.varungbu.varun 

Sending email with attachment

Hi,

 

I am creating an account and its associated contact and sending an email to contact with an attachment saying "Hello

Contact-Name". 

 

I am able to send an pdf with "Hello" but if tried to send "Hello-Contact-Name" then I am able to send a pdf Saying "Hello Contact-Name"  but mailed pdf is currupted.

 

For Attchment I created a pdf from another page "PdfPage" which renderAs="PDF"   and Contact is standardController for this page.

 


Code for generating pdf is as:

 

        PageReference pdf = Page.PdfPage;

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

      Blob blobContent;
        try{
            blobContent = pdf.getContent();
        System.debug(blobContent.size());
        }
        catch(Exception e){
            blobContent = Blob.valueOf('Some Text');

        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('Invoice.pdf');
        attachment.setbody(blobContent);

        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment});

 

Best Answer chosen by Admin (Salesforce Developers) 
gbu.varungbu.varun

Hi Saurabh,

 

Yes this is problem. We can not hardcode id. I have to send an an attachment to contact with his information. Is there any other way to do it.

All Answers

Saurabh DhobleSaurabh Dhoble

Why do you have all your code to send the email **inside** the exception catch block ? Is that on purpose or a bug ?

Saurabh DhobleSaurabh Dhoble

Apart from putting the code in an exception catch block, I did not find anything wrong with your code. Are you sure you are putting the renderAs="PDF" on your PDF page ? That was the only way I got a corrupted file. You might want to use the "getContentAsPDF" method instead of getContent(), if you just need PDF.

 

Below is my code that works fine. Please mark as answer if this answers your question.

 

Here's the PDF Page:

<apex:page standardController="Contact" renderAs="PDF">
    <apex:form>
        <apex:outputLabel>Hello </apex:outputLabel>
        <apex:outputField value="{!Contact.Name}" />
    </apex:form>
</apex:page>

 And here's the sender page & its controller :

 

<apex:page standardController="Account" extensions="EmailSenderController">
    <apex:form>
        <apex:commandButton value="Click to send" action="{!sendEmail}"/>
	</apex:form>
</apex:page>

 

public class EmailSenderController {
	public EmailSenderController(Apexpages.StandardController c)
    {}
    
    public void sendEmail()
    {
        PageReference pdf = Page.PDFPage;
        pdf.getParameters().put('id','003E000000HCekG');
		Blob blobContent;

        blobContent = pdf.getContent();
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String [] toAddress = new String[] {'saurabh.dhoble@gmail.com'};
        mail.setToAddresses(toAddress);
        mail.setReplyTo('saurabh.dhoble@gmail.com');
        mail.setPlainTextBody('Hello');
            
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('PDFPage.pdf');
        attachment.setbody(blobContent);

        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment});
		
        Messaging.sendEmail(new Messaging.SingleEmailMessage [] {mail});
    }
}

 

Let me know if this helps.

gbu.varungbu.varun

Hi,

 

Thanks Saurabh . I am sharing problem in detail.  I created two Visualforce pages and one Controller.

1. Controller is:

public class PdfDemo{

    public PdfDemo(ApexPages.StandardController controller) {

    }

    public pageReference save1(){

        /*Create Account*/
        Account acnt = new Account();
        acnt.Name = 'Test Account';
        insert acnt;
        
        /*Create Contact*/
        Contact con = new Contact();
        con.FirstName = 'Fname';
        con.LastName = 'Name';
        con.email = 'gbu.varun@gmail.com';
        con.AccountID = acnt.id;
        insert con;
        
        Contact testContact = [Select id,name,email from Contact where id=:con.id limit 1];
        
        
        /*Send Email*/
        String[] toAddresses = new String[] {testContact.email};
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toAddresses);
        mail.setHtmlBody('Hi <br/> This is body');

        PageReference pdf = Page.PdfPage;
        pdf.getParameters().put('id',(String)testContact.id);
        System.debug(pdf);
        Blob blobContent = pdf.getContent();

        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('Testing.pdf');
        attachment.setbody(blobContent);
        
        /*Email Attachment*/
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment});
        /* Send Email*/
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });         
        
        return Pdf;
    }
}

 

2. Visualforce Page for sending mail is :

 

<apex:page StandardController="Contact" extensions="PdfDemo" >
    <apex:form >
        <apex:pageBlock >
            <apex:pageblockButtons >
                <apex:commandButton action="{!save1}" value="Send Mail"/>
            </apex:pageblockButtons>
        </apex:pageBlock>
    </apex:form>    
</apex:page>

 

3.   Visualforce Page named "PdfPage"  for generating pdf is :

<apex:page StandardController="Contact" renderAs="pdf">
    Hi {!Contact.Name}
    <br/>
    This is pdf Page.
</apex:page>

 

This Page send a corrupted PDF file.

 

But if I remove {!Contact.Name} then  and change Code to the following then It send right pdf to me.

<apex:page StandardController="Contact" renderAs="pdf">
    Hi {!Contact.Name}
    <br/>
    This is pdf Page.
</apex:page>

 

 

 If I put some information of created Contact then it sends a corrupted file. For static values it is working fine.

 

 

Saurabh DhobleSaurabh Dhoble

I think the problem is that your inserted contact object is not available immediately to the PDFPage. Can you try hardcoding the ID value in the pdf.getParameters() method, something like pdf.getParameters().put('id', XXXXX)

 and see what happens.

gbu.varungbu.varun

Hi Saurabh,

 

Yes this is problem. We can not hardcode id. I have to send an an attachment to contact with his information. Is there any other way to do it.

This was selected as the best answer