CFMAIL Attachment Missing

I was working on a project to create an Excel spreadsheet from a form that need to be mailed off to an email address. I used CFFILE to create the file, CFMAIL to mail the file, and CFFILE to delete the file from a temp directory. It all seemed to be working except the attachment wasn't there.

My suspicion was that CFMAIL was still working when CFFILE deleted the file. This happens because CFMAIL spools the email by writing it to a text file that ColdFusion monitors and generates emails from. This is fine if you just want to send a bunch of emails the emails can be written to the spool and your template can go on with its business. If you want to send an attachment then you need to have CFMAIL complete its spooling and mailing operations before deleting. This might be tricky to do but CFMAIL has a parameter called spoolEnable which has a default of yes. So to avoid the file deletion during a spooling delay just add spoolEnable="no" to your CFMAIL tag.

Example:

<cffile action="upload"
filefield="excel_upload"
destination="C:\temp"
accept="application/msexcel"
nameconflict="makeunique" />


<cfmail to="address@company.com"
from="#sender#"
subject="file attachment"
spoolEnable="no">

mail message
<cfmailparam file = "C:\temp\#file.serverfile#" disposition="attachment" />
</cfmail>

<cffile action="delete"
file="C:\temp\#file.serverfile#" />

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.