Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I attach my uploaded file to an email?

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="[email protected]" 
        from="[email protected]" 
        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?

like image 763
froadie Avatar asked Jul 02 '12 21:07

froadie


People also ask

Why wont it let me attach a file to an email?

It's possible that one of your browser extensions is limiting the functionality of Gmail. Another reason why you can't attach files in Gmail is that your browser does not support the email service. If you want to access the best experience, opt for supported browsers like Edge, Chrome, Safari, and Firefox.

How do I upload a document and attach it to an email?

Attach a fileOn your computer, go to Gmail. Click Compose. Choose the files you want to upload. Click Open.

How do I fix unable to attach file?

One of the reasons you can't attach files in Gmail is that the browser is outdated. Therefore, you can update your browser to fix this problem. Here is the guide: Step 1: In the Google Chrome window, click the three-dot icon in the top-right corner, and then select the Update Google Chrome option.

Why will Outlook not let me attach a file?

You can only attach files to calendar events that you or a delegate created. The size limit for an email in Outlook.com is 29 MB. This includes all text, inserted and attached files. Resizing an image doesn't reduce its file size.


1 Answers

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.

like image 193
Mark A Kruger Avatar answered Sep 28 '22 01:09

Mark A Kruger