For anyone who has built a web page that has uploaded and sent files (such as sending resumes to the Human Resources department of a law firm), you probably know that one of the most frustrating things about sending mail from ColdFusion is that the mail doesn't get sent out immediately; it gets spooled. This means that your mail file sits in a directory until the mail service finds it and sends it out. Since sending mail is very much a "set it and forget it" function, the point of friction here is not obvious at first. It does, however, become very clear the second you want to delete the file you just sent out as an attachment.
When a user uploads a file purely for an email attachment, most likely, unless you have a fairly complex system, you don't want to keep that file on your server; so, after they upload it, you send out your mail and then delete the file. Then, you find out that your emails aren't being sent. It turns out, since the emails are spooled, you actually deleted the attachment file before the email was processed, which of course threw an error which you probably didn't see, because your ColdFusion page seemed to work just fine.
To get over this, we have learned to turn off spooling on emails that have "temporary" attachments, right? This works because the email is processed the second the CFMail tag is finished, in a SYNCHronous manor. This allows the code directly after the CFMail tag to assume the email has been completely sent out and therefore can execute without any precautions. But this never feels good; I hate the idea of forcing the mail service to act in a way that it is not optimized to do.
This is why the new Remove attribute of the CFMailParam tag (introduced by the ColdFusion 8.0.1 updater) is so exciting! It allows you to flag email attachments for deletion without you having to do anything or worry about spooling! The ColdFusion server takes care of all of this for you. Zero friction. All you have to do is include remove="true" on any CFMailParam file that needs to be deleted after the email has been processed.
Let's take a look at a really simple example (focusing on the new CFMailParam attribute, not on any proper form validation or file handling):
Launch code in new window » Download code as text file »
Here, I am just getting an email address and a file for attachment and sending it out. Notice that my CFMail tag makes no use of the SpoolEnable attribute. This is because the CFMailParam tag is using the new Remove attribute which will take care of the file system clean up for us after the mail has been sent.
This is awesome. Here's a great example of where a seemingly small change will end up making such a huge impact on coding effort.
As you are testing this new feature, you can actually see it work in real time. After I uploaded the file, one_curvey_line_up.jpg (and yes, I realized I misspelled curvy, don't rub it in), here is what my file system looks like:
| | | | ||
| | ![]() | | ||
| | | |
I uploaded the image to the current directory and there it is - after the ColdFusion page has been processed but the email itself has not been sent.
Then, I jumped over to my email and refreshed the screen. Still nothing, and indeed, switching back to my file server, I could see that the image file was still there. But then, a few seconds later, the email showed up in my account:
| | | | ||
| | ![]() | | ||
| | | |
Now that the mail file has been processed, I once again jumped back over to my file server to see that the image file had been successfully removed without me having to do any additional work:
| | | | ||
| | ![]() | | ||
| | | |
Woohoo! I hope I am not the only one who sees just how exciting this is. This is going to make life much much easier.
Download Code Snippet ZIP File
Comments (8) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Updated CFHttpSession.cfc With Spoofed Referer Can Log Into PayPal
Creating A "Remember Me" Login System In ColdFusion
How about when you are using a Bulk Email system in ColdFusion, how do you remove the attachments that could be sent using this?
Posted by Raul Riera on Apr 30, 2008 at 9:20 AM
@Raul,
Do you mean using the Query attribute of the CFMail tag to send a single email to many different people? I wonder what would happen. I have to assume that ColdFusion is smart enough to know how its attributes will interact, but I have not tested this.
Posted by Ben Nadel on Apr 30, 2008 at 9:23 AM
Yes thats what I meant, I will test it out later today to see what happens :D
Posted by Raul Riera on Apr 30, 2008 at 10:02 AM
Ben, great stuff as always. Here is another method of sending files in e-mail from memory. http://blog.pengoworks.com/index.cfm/2007/10/26/Using-CFMAIL-to-send-attachments-stored-in-memory
In case the host does not allow CFFILE or you just don't want the file io for what ever reason.
Posted by Ryan TJ on Apr 30, 2008 at 10:20 AM
@Raul, I would assume that it wouldn't be deleted until the final attachment got sent. This is within the same mail 'to/cc/bb' segment. If you're sending email #2 with the same attachment, then you're going to have to have a duplicate of that attachment as well.
Posted by Todd Rafferty on Apr 30, 2008 at 11:59 AM
@Ben,
Just out of curiosity, do you purposely avoid using <cfform> for any particular reason(s)? I'm not necessarily speaking of format="flash" either. I've just noticed several CF developers don't/aren't use/using it.
Posted by Steve Withington on May 1, 2008 at 2:29 PM
@Steve:
I can't speak for anyone else, but I personally try to avoid cfform where possible.
It does make life a lot easier in terms of automating client side validation and certain restrictions, but too often people forget that they need to implement server side validation too.
The main reason I avoid it, however, is that it has a history of automagically generating HTML that doesn't pass W3C validation - I understand that that's since been fixed in CF8, but nonetheless I prefer to have a finer degree of control over the content in my pages.
Posted by Jono on May 4, 2008 at 10:16 PM
@Steve,
I have never gotten into CFForm. I don't really avoid it for any particular reason. I do all my data validation on the server and like all my CSS, so I just never bothered looking into CFForm. Some people really like it.
Posted by Ben Nadel on May 5, 2008 at 1:56 PM