A while back a bug was filed with Adobe's bug tracker about CFMAIL not setting headers correctly for attachments and thus causing them not to be seen or able to open on Apple 'iDevices'. I had responded that this should not be a bug because setting the CFMAILPARAM tag correctly should be enough and pointed them to my own blog post about properly sending CFMAIL. Well, I was wrong and I was right. My previous blog post had a slightly incorrect example of how to setup the CFMAILPARAM tag and thus was causing this issue. Sorry about that :(. The email headers were having the Content-type set to "multipart/related" instead of "multipart/mixed" and while I could open the attachment in every email client out there the attachments would not open on my iPhone or iPad. Granted that when I wrote that original blog post I didn't own an iDevice and never tested the code for those.

The good news is that after a little testing with trial and error I discovered the error of my code. Great news! Attachments sent from CFMAIL in ColdFusion 9.0.1 and ColdFusion 10 work perfectly. I'm presuming this will work on previous versions with the CFMAILPARAM tag.

The code: From my previous post I had this example. It all looks great. But one item causes an issue. Setting the contentID attribute in the CFMAILPARAM tag causes the email content-type to be set as "multipart/related". By simply omitting that setting emails now have content-type "multipart/mixed" set in the email header. A small but significant difference when it comes to open email attachments on the iPad and iPhone.

view plain print about
1Old Code:
3<cfmail to="#form.mailto#" from="#form.mailFrom#" subject="#form.subject#" type="html">
4<cfmailparam file="/document/path/mypdf.pdf" contentID="#createUUID()#" disposition="attachment" type="application/PDF">
5<cfmailpart type="text/plain" charset="utf-8">#textmessage(mailmessage)#</cfmailpart>
6<cfmailpart type="text/html" charset="utf-8">#mailmessage#</cfmailpart>
9New and Correct Code:
11<cfmail to="#form.mailto#" from="#form.mailFrom#" subject="#form.subject#" type="html">
12<cfmailparam file="/document/path/mypdf.pdf" disposition="attachment" type="application/PDF">
13<cfmailpart type="text/plain" charset="utf-8">#textmessage(mailmessage)#</cfmailpart>
14<cfmailpart type="text/html" charset="utf-8">#mailmessage#</cfmailpart>

The Moral: You can always learn something by going back and reviewing what you did in the past and taking notes.

*Note: When I write little tools like this I'm usually writing it because there is something I absolutely need to see in order to complete another project. These little tools are not written as projects themselves and therefore may not be very pretty or as full featured as something that I was writing as a complete project. I just needed to get some code running that gave me back the data I needed to see. There are no warranties or promises. If you find is useful then great. If not, oh well. I know the code works on CF10 at the moment. I can not be certain if it still works on other versions of ColdFusion. * Any code posted may not be totally secure or production ready. Use at your own risk. ** Unless otherwise noted, this code shall be deemed Public Domain.