You need to sign in to do that
Don't have an account?
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});
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
Why do you have all your code to send the email **inside** the exception catch block ? Is that on purpose or a bug ?
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:
And here's the sender page & its controller :
Let me know if this helps.
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.
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.
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.