View Full Version : Cold Fusion email form with attachment
09-06-2003, 12:17 PM
I have a simple form to email written in cold fusion consisting of textboxes and textareas. The form input goes directly to email without cgi-bin etc...
Is it possible to include an attachment field with this simple format? All I want is the file to arrive attached to the email. I have searched and searched but cannot find anything.
Hope someone can help.
P.S. Will post the form code if necessary.
09-07-2003, 05:10 PM
You didn't mention what version of CF you are using, but for at least v4.5+, try this:
SUBJECT="Test sending attachment">
Here it comes.
The path specified must be the full path to that file. Important note: the file specified must already reside on the CF server, so if you want to allow a user to attach a file to an email, you will need to first upload that file to the webserver (or a network drive accessible to the server). If your host doesn't allow the use of CFFILE and you want the user to upload the files for attachment, you might have a problem.
You can include as many CFMAILPARAM tags as you need, one per file being attached.
Hope this helps!
11-27-2003, 01:16 PM
Thanks so much for your reply. It didn't realise it was there until now.
I am still struggling to sort this out. I now have access to a cgi-bin if this helps. I have posted the form code below and would love to be able to include an attachment field where the user attaches their own file.
Here we go, I hope you can help:
<cfparam name="task" default="cancel">
<cfparam name="form.qtype" default="">
<cfparam name="form.name" default="">
<cfparam name="form.fromemail" default="">
<cfparam name="form.comments" default="">
<CFFORM action="contact.cfm?#request.addtoken#" method="POST">
<input type="hidden" name="name_required" value="Please enter your name.">
<input type="hidden" name="fromemail_required" value="Please enter your email.">
<input type="hidden" name="comments_required" value="Please enter a message.">
<cfif lcase(trim(task)) is "send message">
<cfmail to="#trim(request.email_active)#" from="#trim(request.email_active)#" subject="User Comments [ #trim(form.qtype)# ]" server="#trim(request.mailserver)#">
<CFMAILPARAM NAME="Reply-To" VALUE=" #trim(form.fromemail)#">
A user from #trim(request.sitename)# is sending the following message.
Question Type: #trim(form.qtype)#
Thank you, your message has been sent. <cfoutput>#trim(form.name)#</cfoutput>
Name:<input type="text" name="name" size="30">
Email:<input type="text" name="fromemail" size="30">
Question Type:<select name="qtype" size="1">
<option value="General Question"selected>General Question</option>
<option value="Site Problems">Site Problems</option>
<option value="Call Me">Call Me</option></select>
Comments:<textarea cols=28 rows=5 name="comments"></textarea>
<input type="submit" value="Send Message"name="task">
11-28-2003, 06:00 PM
I don't know if this can help you, but I came across this cgi script that allows the user to upload and attach from their own computer.
I am just about to learn CF and PHP and I can only install cgi/perl, not write anything (maybe next year!)
This uses a PM module instead of the sendmail server so maybe you can see how it is done, or combine the scripts - I have no idea, but nothing else has been posted in reply to your above question so good luck!
12-02-2003, 09:26 AM
I think the first problem with your code is that you are trying to do too much in one .cfm template. You can't include your <CFMAIL> block within the <CFFORM> block because it will never execute. The variables are not set until the user clicks on the "Send Message" button.
If you split the code into at least 2 smaller files you should be able to get it working. Put the <CFFORM> into the first file, and have the action="mailit.cfm". Then the "mailit.cfm" file would contain the <CFMAIL> block.
In your <CFFORM> statement, it looks like you are trying to pass a parameter in the URL. Try instead to pass the parameter as a hidden value, like
<input type="hidden" name="method_type" value="#request.addtoken#"> .
Finally, you had asked about sending an attachment with the email, but in your <CFMAILPARAM> statement you don't mention a filename. Not sure what you are trying to do there, but if you can get the mail part going, let me know and we can work from there.
Hope this helps!
12-04-2003, 11:28 AM
Thanks for your comments. I have taken the code from a working form on my site. There doesn't seem to be any problems when submitting it and sends the information correctly.
My problem is that I would like users to be able to attach a file from their own computer and submit it with the form. Your know the usual 'browse' button and then have the file sent to me as an attachment to the email.
There is no reference to any file in the code given as I wouldn't know where to start!
Hope you can help.
12-04-2003, 01:09 PM
Sorry, I obviously misinterpreted your code. Actually I tried to execute it and couldn't, so I'm missing something important! But to get back to the file attachment problem, here's my understanding of what is needed.
You should be able to gather the information from your user in one form, but you will have to re-organize the programming of that form as I had previously indicated. Your form statement will look like this:
<FORM ACTION="process.cfm" ENCTYPE="multipart/form-data" METHOD="post">
... gather your data ...
<INPUT NAME="filename" SIZE=50 TYPE=FILE>
Within the "process.cfm" file, you will need to upload the file to the webserver:
<CFFILE ACTION="upload" DESTINATION="d:\inetpub\wwwroot\yourdirectory\" NAMECONFLICT="MAKEUNIQUE" FILEFIELD="filename">
Then you will be able to utilize the <CFMAIL></CFMAIL> block with the <CFMAILPARAM FILE="d:\inetpub\wwwroot\yourdirectory\filename.ext">
As I understand it, the problem here is that ColdFusion executes on the webserver, not on the client's machine. That means the file must first be uploaded to the webserver. It's an extra step for you but not for the user.
Now, if someone else knows of a different way to do this, I'd be interested in hearing it, too!
12-04-2003, 04:38 PM
That's correct. The attachment location is first sent to the server via the form post and then the cffile uploads it and then it can be stuck in the e-mail.
Currently that is the ONLY way to do it via coldfusion.
12-04-2003, 06:22 PM
When you say 'filename' does this mean that the user can upload any file or a specific filename. You see I need them to be able to upload their CV so the filename will always be different.
12-04-2003, 07:12 PM
filename is just the name of the field. The code is specifying an input field. You would put the local path to the file and then it would be uploaded after the post
12-04-2003, 10:42 PM
When the user inputs the file, whatever that may be, your <INPUT> statement
<INPUT NAME="filename" SIZE=50 TYPE=FILE>
names it "filename" as a variable name. So it's irrelevant what the actual name is, you have "aliased" it to be named "filename". That way, when you reference it, you don't care what they have named it.