Question

I have a form with a file input:

<input type="file" id="uploadFile" name="uploadFile" />

I submit the form using the ajaxForm method of the JQuery form plugin.

Then, in the code to handle the post, I read and process the file. I use cfspreadsheet to read the file directly from the file input field:

<cfspreadsheet 
  action="read" 
  src="#form.uploadFile#" 
  sheet="1" 
  query="spreadsheetData" 
  headerRow="1" 
  excludeHeaderRow="true"
>

This all works correctly.

I decided that I want to email the spreadsheet to the administrator as well. I thought I could accomplish this simply with a cfmail tag that includes the following cfmailparam tag:

<cfmail to="myEmailAddress@email.com" 
        from="fromEmail@email.com" 
        subject="Upload File" type="HTML">
    <cfmailparam file="#form.uploadFile#" />
    File processed successfully
</cfmail>

However, this is not working correctly - the email is not sent. What am I doing wrong?

Was it helpful?

Solution

Leigh's solution works well and you have probably already implemented in your code. I thought I'd put my 0.02 cents in about why this is a problem to begin with.

When you upload a file the file is placed in a temp folder location. If you do nothing with the file to put it in a final destination the file is deleted - probably at the end of your request.

Meanwhile the cfmailparam does not actually attach the file at runtime. It leaves it to the spooler process to do that. If you take a look in your ColdFusion installs "mail/spool" directory you will see a file with .cfmail extension. If you can't "catch" one before delivery check your undeliverable folder - there's bound to be a few hanging around in there.

The .cfmail file serves as an instruction to the spooler service which sends the mail. It has a subject, from, to, server address, body etc.

If you attach a file you will see something at the bottom of this file that looks like this:

file:  D:\jrun\temp\blah.tmp
file-type:  application/octet-stream;   name="I am the file you uploaded.tmp"
file-disposition:  attachment
remove:  false

At runtime CF grabs this file and does what Leigh is suggesting - places it as binary with a mailpart (base64 encoded) into the main body of the message. So what is happening is that by the time the spooler service gets around to attempting to open and attach this file the file is long gone because the request has ended. I also think that the file exists with a ".tmp" extension in this temporary directory - which is obviously not what you want to attach (but that could be a previous version of CF).

To fix it, first use cffile with the "upload" action to put the file in a real (instead of temporary) folder on the disk. Then use cfmailparam to attach the file. NOTE: The "remove" attribute set to yes will cause CF to delete the file once it has successfully sent the mail - which is the effect I believe you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top