<!--- Kill extra output. ---> <cfsilent> <!--- Param FORM variables. ---> <cfparam name="FORM.name" type="string" default="" /> <cfparam name="FORM.email" type="string" default="" /> <cfparam name="FORM.resume" type="string" default="" /> <!--- For the form submission flag, since we are asking it to be of type numeric, we have to catch the CFParam in case someone has hacked the HTML and altered the value being sent (a non-numeric value will throw a ColdFusion error). ---> <cftry> <cfparam name="FORM.submitted" type="numeric" default="0" /> <!--- Catch CFParam data type errors. ---> <cfcatch> <cfset FORM.submitted = 0 /> </cfcatch> </cftry> <!--- Define an array to catch the form errors. ---> <cfset arrErrors = ArrayNew( 1 ) /> <!--- Check to see if the form has been submitted. ---> <cfif FORM.submitted> <!--- Now that the form has been submitted, we need to validate the data. ---> <cfif NOT Len( FORM.name )> <cfset ArrayAppend( arrErrors, "Please enter your name" ) /> </cfif> <!--- Validate email. ---> <cfif NOT IsValid( "email", FORM.email )> <cfset ArrayAppend( arrErrors, "Please enter a valid email address" ) /> </cfif> <!--- When it comes to validating the resume, we want to check to see if they selected one. Then, once they selected one, we ONLY want to mess with it if there are no errors caused by other form fields. ---> <cfif NOT Len( FORM.resume )> <cfset ArrayAppend( arrErrors, "Please select a resume to upload" ) /> <cfelseif ArrayLen( arrErrors )> <!--- The file has been selected, but there are errors caused by other parts of the form validation. Therefore, we now have a file that is just sitting in our temp folder. Delete this file to keep a clean server. ---> <cftry> <cffile action="DELETE" file="#FORM.resume#" /> <cfcatch> <!--- File delete error. ---> </cfcatch> </cftry> <cfelse> <!--- The resume has been selected and there are no other errors caused by Form validation. Therefore, we can now deal with the file upload. There is a chance that the file upload will cause an error, so be sure to wrap all file actions in CFTry / CFCatch blocks. ---> <cftry> <cffile action="UPLOAD" filefield="resume" destination="#GetTempDirectory()#" nameconflict="MAKEUNIQUE" /> <!--- Now that we have the file uploaded, let's check the file extension. I find this to be better than checking the MIME type as that can be inaccurate (so can this, but at least it doesn't throw a ColdFusion error). ---> <cfif NOT ListFindNoCase( "pdf,doc,rtf", CFFILE.ServerFileExt )> <cfset ArrayAppend( arrErrors, "Only PDF, DOC, and RTF file formats are accepted" ) /> <!--- Since this was not an acceptable file, let's delete the one that was uploaded. ---> <cftry> <cffile action="DELETE" file="#CFFILE.ServerDirectory#\#CFFILE.ServerFile#" /> <cfcatch> <!--- File Delete Error. ---> </cfcatch> </cftry> </cfif> <!--- Catch any file errors. ---> <cfcatch> <!--- There was some sort of error with the file upload. Set the error and then try to delete the file. ---> <cfset ArrayAppend( arrErrors, "There was a problem uploading your resume" ) /> <!--- Try to delete the file. Again, we want to use CFTry / CFCatch whenever dealing with files, especially if we don't know if the file is even at the given path. ---> <cftry> <cffile action="DELETE" file="#CFFILE.ServerDirectory#\#CFFILE.ServerFile#" /> <cfcatch> <!--- File delete errors. ---> </cfcatch> </cftry> </cfcatch> </cftry> </cfif> <!--- Now that we have validated our form data, let's check to see if there are any form validation errors. Only if there are no errors do w want to continue processing the data - otherwise, we want to skip this next part and let the form re-render. ---> <cfif NOT ArrayLen( arrErrors )> <!--- Create a short hand for the file. ---> <cfset strFilePath = ( CFFILE.ServerDirectory & "\" & CFFILE.ServerFile ) /> <cfmail to="ben@xxxxxxxx.com" from="#FORM.email#" subject="Web Site Resume Submission" type="html"> <p> The following resumé has been submitted through the web site on #DateFormat( Now(), "mmm d, yyyy" )# at #TimeFormat( Now(), "h:mm TT" )#. </p> <p> Name: #FORM.name# </p> <p> Email: #FORM.email# </p> <p> Resumé: <em>See attached file</em> </p> <!--- Attach the file. ---> <cfmailparam file="#strFilePath#" /> </cfmail> <!--- Delete the resume file since we no longer need it on the server. HOWEVER, this will only work if the emails are NOT getting spooled. If the email is getting spooled, then we will end up deleting the file before it had a chance to get attached to the outgoing email and the mail will end up failing after it leaves ColdFusion. *** Put back in ONLY if SpoolEnable="no" in your CFMail tag. ---> <!--- <cftry> <cffile action="DELETE" file="#strFilePath#" /> <cfcatch></cfcatch> </cftry> ---> <!--- At this point, you would probably forward the user to another page using something like CFLocation. ---> </cfif> </cfif> </cfsilent> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Upload And Email A File ColdFusion Example</title> </head> <body> <cfoutput> <!--- Check to see if we have any form errors. ---> <cfif ArrayLen( arrErrors )> <h3> Please review the following: </h3> <ul> <cfloop index="intError" from="1" to="#ArrayLen( arrErrors )#" step="1"> <li> #arrErrors[ intError ]# </li> </cfloop> </ul> </cfif> <form action="#CGI.script_name#" method="post" enctype="multipart/form-data"> <!--- Our form submission flag. ---> <input type="hidden" name="submitted" value="1" /> <label for="name"> Name: <input type="text" name="name" id="name" value="#FORM.name#" /> </label> <br /> <label for="email"> Email: <input type="text" name="email" id="email" value="#FORM.email#" /> </label> <br /> <label for="resume"> Resumé: <input type="file" name="resume" id="resume" value="#FORM.resume#" /> </label> <br /> <input type="submit" value="Upload Resumé" /> </form> </cfoutput> </body> </html>