PDF attachment to email - PDF attachment returning error when opening

I have spent the morning playing around with this and working through a previous post topic from Dan Vega (Google Groups) on the exact same issue I am having.

It looks like Dan was able to get it sorted, but I haven’t been so successful.

I am trying to attach a PDF to an email on the fly:

`

var mail = mailservice.newMail(to=manager.getEmail(),
from=user.getEmail(),
subject=“Staff Leave Form”);

mail.setBody( renderer.renderView(view=“email_templates/staffLeaveForm”) );
mail.setType(“HTML”);
mail.addMailParam(
file=“SampleLeaveForm.pdf”,
type=“application/pdf”,
content=renderer.renderLayout(view=“email_templates/staffLeaveFormAttachment”)
);
mailService.send( mail );

`

I’m injecting the renderer into my controller with:

property name="renderer" inject="coldbox:plugin:renderer";

The mail sends, and attaches file as a PDF fine, but when I open the PDF, I get the error:

Adobe Reader could not open 'SampleLeaveForm.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).

I’ve tried everything in Dan Vega’s post, as well as the example Lui provided, plus more, but just can’t get the PDF opening without error.

Is anyone able to see what I might be missing?

Many Thanks!

Jason

Aand what about the view, what is it doing?

Haven’t done this for awhile but doesn’t this have to be binaryEncoded to be attached?

At the moment, the view just contains some text “This is the PDF”.

How do I binaryEncode the PDF when generating this way? I haven’t been able to find anything on that in relation to mailparam?

Thanks Andrew!

Try using cfdocument to do this for now.

That’s what I am doing, but when I do that, it actually renders the PDF and stops continuing on with the event.

To keep the task going, I am just going to go back to the old way of saving the PDF to tempDirectory and attaching the file, but would be great to be able to just attach a PDF without having to save it to disk first.

Thanks Anyway Andrew.

Jason

The only way you can stop that, is to hold the PDF in memory or as you where doing creating a temp file for it to be attached. I have always done the temp file, but generally stay away from dynamically created, so I am going by what I have read in the past.

Ben has this article, where he says that you use the name attribute and it is stored in a variable.

http://www.bennadel.com/blog/1700-ask-ben-creating-a-pdf-and-attaching-it-to-an-email-using-coldfusion.htm

I am guessing that with the render layout is also not the right option. I think you need the one where it looks like this instead.

event.renderData(type = “json”, data = data);

Where you change the type and the data to suit what you need for the mail to work.

Andrew, thanks for your help. In the end I have settled for Ben’s Approach. Not ideal, and requires an “include”, but seems the tidiest way I was able to achieve objectives.

So I now have:

`

include “/intranet/views/email_templates/staffLeaveFormAttachment.cfm”;

var mail = mailservice.newMail(to=rc.manager.getEmail(),
from=rc.manager.getEmail(),
subject=“Staff Leave Form for: #prc.user.getFullName()#”);
mail.setBody( renderer.renderView(view=“email_templates/staffLeaveForm”) );
mail.setType(“HTML”);

//attach leave form
mail.addMailParam(
file=“SampleLeaveForm.pdf”,
type=“application/pdf”,
content=leaveFormPDF
);

mailService.send( mail );

`

And am using <cfdocument in “/intranet/views/email_templates/staffLeaveFormAttachment.cfm” to save the document to a variable:

<cfdocument format="PDF" name="leaveFormPDF">

Again, a bit untidy, but at least allows me to generate and attach PDF doc on the fly.

Thanks again!

Jason