7

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?

ale
  • 6,369
  • 7
  • 55
  • 65
froadie
  • 79,995
  • 75
  • 166
  • 235
  • Can you please show your entire CFMAIL code? – BKK Jul 02 '12 at 21:15
  • Is there an error message? If so, what is it? If not, does the e-mail appear in your ColdFusion installation's Spool or Undelivr folders? – Sean Walsh Jul 02 '12 at 21:16
  • @s992 - There are no error messages, and the ajax submit processes successfully and executes the "success" function. I'm not sure how to check those folders – froadie Jul 02 '12 at 21:22
  • @froadie The folders will reside wherever you installed CF. For example, on my local development machine, they are both in C:\ColdFusion9\Mail\. – Sean Walsh Jul 02 '12 at 21:26
  • 3
    Did you check all of the error logs (exception, mail, etcetera) ? Also, any reason you are using `#form.uploadFile#` instead of reading the binary file into a variable and attaching it with `cfmailparam content`? Because `#form.uploadFile#` is just a temporary path. Uploaded files are automatically deleted at some interval. So if you are spooling mail, I suppose it is a technically *possible* the temp file could be deleted before the mail ever is sent. Though the more solid leads are checking the logs and `undelivr` folders. – Leigh Jul 02 '12 at 23:14
  • 1
    Since you are not specifying a mail server in the code, is there one specified in the ColdFusion Administrator? Have you tried sending plain emails? Do they arrive? Or just emails with attachments don't arrive? – BKK Jul 03 '12 at 02:22
  • @s992 - I don't currently have access to those folders, but I'm working on getting it. – froadie Jul 03 '12 at 07:05
  • @Leigh - thanks, maybe I'll just try reading it into a variable first – froadie Jul 03 '12 at 07:06
  • @BenKoshy - emails work, and emails with attachments work, just this particular example is not working. – froadie Jul 03 '12 at 07:07
  • @Leigh - reading to a variable and then attaching works fine. If you post it as an answer I'll accept it... although I'd still like to know what went wrong with the direct attachment :-/ Maybe, as you say, it was deleted before it could get around to sending. – froadie Jul 03 '12 at 07:17
  • If you can successfully send the same mail (without an attachment), the problem is likely what Mark and I described. The temp file is long gone when the spooler attempts to send the message. I would be surprised if there was no error recorded in *any of your log files. Are you sure you checked them all? Also, for future mail problems, do not forget about the `debug` setting. See the `cfmail` documentation for details on how it works and which log files are used. – Leigh Jul 03 '12 at 15:43

1 Answers1

9

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.

Mark A Kruger
  • 7,183
  • 20
  • 21