Sending e-mail attachments in CFMX without writing data to disk

Categories: Java, Source Code, HTML/ColdFusion

One of the many projects I'm currently working on is some code to delay sending of e-mails until a specified window of time. We're generating some report data for clients during the offhours, but the clients want the results e-mailed no earlier than 8am.

Our reports often contain images or other attachments that need to be included e-mails. One issue I really don't like about the implementation of CFMAIL in CFMX is that it requires attachments to be written to disk before you can send the mail. This means if I want to use the CFMAIL tag to deliver delayed e-mails, I would to manage attachments until I'm sure the message is delivered. I don't like that solution, so I set out to see if there might be other ways of generating attachments from binary data in memory. This lead me to researching the JavaMail API—which is the API that CFMAIL uses behind the scenes.

I quickly learned that even CFMX v7.02 still uses JavaMail v1.3.1—which is an older version of the API. One of the issues in v1.3.1 is that it does not include any classes for taking a binary stream from memory and converting to an attachment. It comes with a FileDataSource class—which will read in a file and convert it to the correct data source. This might be the reason that Macromedia/Adobe requires the file to be written to disk.

Related Blog Entries

Comments

Ben Nadel's Gravatar Dan, very cool! I have never seen most of things you have written about. It's gonna take me some time to fully get through it, but this is waaaay cool.

Thanks,!
Sami Hoda's Gravatar Dan,

Excellent job. This is very useful, and I know there has been some rumblings of late inside Adobe on this topic. Great to see a solution!
Dan G. Switzer, II's Gravatar @Sami:

I will say this, one of the benefits of requiring a file on a physical disk, is if you're blasting out thousands of e-mails, it helps keeps the file size down of each message in the spool folder.

I'd just like to see the option to attach in memory binary objects using cfmailpart. It definitely would be helpful at times.

We've previous run into problems w/e-mail messages getting stuck in the queue and then having their attachment files being deleted, which means we can no longer send the e-mail. Very frustrating.
Charlie Arehart's Gravatar This is so great. Thanks, Dan.

I'll add that for readers who are interested in learning more about the ability of CFMX to permit you to write the results of CFDOCUMENT and CFREPORT to a variable, thus to be able to email this way, I did a blog entry on it recently:

http://carehart.org/blog/client/index.cfm/2007/1/1...
john's Gravatar Dan i have a question what about the file to be attach is on the other pc? how to handle this situation?

Thanks,
Dan G. Switzer, II's Gravatar @John:

CFFILE shouldn't have a problem accessing files on the network (well at least I've never seen a problem using Windows Servers.)

If you're having issues, just do a Google search for something like:

"cfmx accessing files on a mapped drive"
Tim Archer's Gravatar I also have an example showing how to use JavaMail to send emails with attachments. It also supports relaying through an SMTP server that requires authentication.
Check it out at:
http://timarcher.com/?q=node/53">http://timarcher.com/?q=node/53
Garth Gerstein's Gravatar Hey Dan,

Very cool. I am trying to solve this same problem, but using the soloution within Actionscript for Flash development. You can write image files to byte Arrays in Flash and I am trying to find a way to email that data directly from memory. I don't know if I can call up a Java API in Actionscript, but this was very helpfule in terms of my understandig the basic process. Thanks.
Dan G. Switzer, II's Gravatar @Garth:

What I'd recommend is using Flash Remoting (AMF) if your server provides support for it. Otherwise, use one of the other Remoting Services (i.e. Web Services) to talk with your server.
Dan G. Switzer, II's Gravatar I've added a new blog entry that shows a technique pointed out by Jon Wolski that uses just the CFMAIL and CFMAILPART tags:

http://blog.pengoworks.com/blogger/index.cfm?actio...
Chris Iregbu's Gravatar Dan,

This is great stuff! I have a requirement to send a list of reports via email to a client. I had tried various approaches until I came across your blog. It's a real time saver.

Thanks so much.
Susan Hansen's Gravatar This is GREAT! Thanks so much for sharing. I finally got it to work but now have a stupid question. Am I able to pull in data from a form and get this to email the form data? Example: Contact: #form.groupcontact#
My form is at www.rochesterpubliclibrary.org/apps/forms/AudSetup...
I fill in the data on the form, but the email/attachment just gives me Contact: #form.groupcontact# without the form data being filled it. I am a librarian, not a programmer so I'm sure i'm missing something..
Dan G. Switzer, II's Gravatar @Susan:

You need to wrap your output in <cfoutput> tags to have CF process those variables, thus:

Contact: #form.groupcontact#

would be:

<cfoutput>
Contact: #form.groupcontact#
</cfoutput>
Susan Hansen's Gravatar DUH.... THANK YOU! THANK YOU! THANK YOU!. I've been trying to get this to work FOREVER. You saved me countless hours of frustration. Now I can cross that one off the list...
Jose's Gravatar Very cool. Is it possible to specify a dataset (ie: query attribute in cfmail) using JavaMail? I'm looking for a faster alternative than CFMail.

Thanks
Jonathan's Gravatar Nice! Thanks.
Ali's Gravatar Thanks for posting this Dan.
I'm having a problem with send mixed content in my emails.
I have a combination of HTML with embedded images, as well as mutliple PDF attachments.
When I send it through CFMail, outlook does not display the paperclip showing that there are attachments with the email.

The only way I got the paperclip to show up was to use your JavaMail example, and then modify it.
I had to set the subType to mixed, and then I googled the JavaMail API to find out how to include an image attachment, in inline html, since your example did not explain that.

The problem with the JavaMail API version is that since the subtype is NOT "related", the image (which is the logo header of the html email) comes in as an attachment also.

I have found no solution for this on the internet. I'm surprised no one else has had an issue with this in CF.
It seems pretty common to me that people would be sending HTML emails that have images that have to be displayed inline, as well as multiple file attachments that need to be sent. However, for some reason, the out of the box solution with CFMail, causes outlook to not show the paperclip icon.
Dan G. Switzer, II's Gravatar @Ali:

What version of CF are you using?

CF8 and above should support attachments from memory via the <cfmailparam /> and that tag has a "disposition" attribute, which you can set to either "attachment" or "inline". This should allow you to do what you want.

So you should be able to do what you want w/CF8+.
Phil's Gravatar I've got the same issue as Ali. Running with CF8, HTML emails with inline logo and a separate attachment. I tried the disposition for the attachment as both attachment and inline, and still the paperclip icon does not appear (in MS Outlook express).

Add Comment

Leave this field empty


If you subscribe, any new posts to this thread will be sent to your email address.