You need to sign in to do that
Don't have an account?
Amol Dixit
Problem with opening attachment pdf sent via email service.
Hi,
I want to send pdf attachment through email, which will execute on E-Mail service. I wrote class for that and it is sending pdf successfully, but i am getting error while opening the pdf as,
"adobe reader could not open attachment.pdf becauseit is eithernot a supportedfile type or because file has been damaged (for example, it was sent as an email attachment & was't correctly decoded."
Please help me out. Thank you so much..
global class DeliverTrackerbyEmail implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){ Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); String myPlainText = ''; // Add the email plain text into the local variable try { myPlainText = email.plainTextBody; } catch (System.StringException e) { } String fromMail=email.fromAddress; String submodule=myPlainText.substring(0,8); Resource__c res=[Select Name,Employee__r.Name__c From Resource__c Where Employee__r.Email_ID__c = :fromMail]; String ResourceNumber=res.Name; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); Resource__c ab=[select id from Resource__c where Name=:ResourceNumber limit 1]; // Reference the attachment page and pass in the account ID PageReference pdf = Page.attachmentPDF; pdf.getParameters().put('id',ResourceNumber); pdf.setRedirect(true); // Take the PDF content Blob b = pdf.getContent(); // Create the email attachment Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName('attachment.pdf'); efa.setBody(b); String addresses; String[] toAddresses = new String[]{fromMail}; mail.setSubject( 'Tracker ' ); mail.setToAddresses(toAddresses); mail.setPlainTextBody(' Hi,'+res.Employee__r.Name__c+' Please find the Attched Tracker with this mail. '); mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});// Sends the email Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); result.success = true; // Return the result for the Force.com Email Service return result; } static testMethod void testTasks() { // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail(); Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); // Create the plainTextBody and fromAddres for the test email.plainTextBody = 'Here is my plainText body of the email'; email.fromAddress ='rmencke@salesforce.com'; EmailServiceClass taskObj = new EmailServiceClass(); taskobj.handleInboundEmail(email, env); } }
Thank you,
Amol Dixit
In place of
u can try
Blob b = pdf.getContentAsPDF();
Hi, Thank you for quick response.
I have tried with "getContentAsPdf".
Now the pdf getiing open successfully. But contents are not getting into pdf, getting blank pdf. I have tried with small trial page it is working properly but in email service it is giving problem(Emplty pdf). Please help me out.
Thank you so much.
Amol Dixit.
Ok let me just share a example with u where i send a PDF as attachment in my own email service
2min
Page:-- (I use the same page as dual 1st i open it without parameter p it open as normal VF & after that i pass the paramter p in URL then it rerender as PDF
Class:-
I am facing the same issue . Could You please help me with the solution . . .
If you havent found sollution yet... I had a simmilar problem - despite rechecked my code for genereting PDF and sending it via email numerous times, pdf was corrupted whatever settings I've implemented...
There might be solution for this problem:
- "corrupted pdf" situation sometime happen of you call "send_email with render_pdf" from commandbutton from visualforce page;
- creating same button via "custom button" and putting it on standard page layout, the problem does not occur
Workaround: instead of putting <apex:commandbutton value="Send PDF Report" action="{!SendPDFToCustomer}" /> to you visualforce page
- create "intermediate" visualforce page that would call SendPDFToCustomer:
INTEMEDIATE VF Page: PDF_CUSTOMER
- in your visualforcepage then call this "intermediate" visualforce page, for example, via javascript: