Sending e-mail attachments in CFMX without writing data to disk
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.

Comments
Thanks,!
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!
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.
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...
Thanks,
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"
Check it out at:
http://timarcher.com/?q=node/53">http://timarcher.com/?q=node/53
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.
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.
http://blog.pengoworks.com/blogger/index.cfm?actio...
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.
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..
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>