Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Sending an attachment with an e-mail

Engaged ,
Sep 07, 2006 Sep 07, 2006
Does anyone know how to send an attachment with an e-mail using a
form page that is on our web site? I am using the TYPE attribute of
the form tag as "File" to get the file I want to attach to the e-
mail. Below is the code I have. The first page is where the user can
click the browse button to search for a file to attach. The second
page is where the e-mail should be sent to. Thanks for your help.

Andy
TOPICS
Getting started
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 07, 2006 Sep 07, 2006
For openers, you need to add the following to your <FORM> tag:
multipart/form-data

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 07, 2006 Sep 07, 2006
Jdeline,
I'm not sure what you mean. What is this? How do you use it in the code? This was just a practice one anyway so I know how to do this on our web site.

Andy
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 07, 2006 Sep 07, 2006
First you need to add enctype="multipart/form-data" to your form tag on
the first page. This is required to actually have the browser send a
file from the client to the server.

Second you need to use the <cffile action="upload"....> tag to write the
file uploaded from the client to someplace on the server file system.

Then you can attach this saved file to the email with the <cfmailparam
...> tag.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 08, 2006 Sep 08, 2006
Ian,
Do I need to have this <cffile action="upload"....> ? Is there a way to just attach the attachment to an e-mail without uploading it to our server? Will it stay in there until I delete it then?

Andy
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 08, 2006 Sep 08, 2006
Ian,
Do I need to have this <cffile action="upload"....> ? Is there a
way to just attach the attachment to an e-mail without uploading it to
our server? Will it stay in there until I delete it then?

Andy

Yes you have to upload it. You do not have direct access to the
client's file system, that would be bad, Bad, BAD! Technically, the
file is already uploaded to a temp directory by the web server. The
<cffile ...> tag gives you access to this temp file in ColdFusion. I do
not know of any other way to access the temp file. Yes, It will stay
there until you delete it. That is easily accomplished with a <cffile
action="delete" ...> tag.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 08, 2006 Sep 08, 2006
Ian,
Do we have to set a folder up on our cold fusion server or do we just make a folder under our web site so when a user clicks on the browse menu, the file they attach has to be saved to our server first and then I can attach the saved file to an e-mail? How do we create this folder? Does it have to be an FTP site or just a folder on our web site? How do we make the permissions work correctly since a folder under our web site is not writable to except by the web editors that work here? Please let me know what I can do to get this to work. Thanks.

Andy
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 08, 2006 Sep 08, 2006
Ian,

Do we have to set a folder up on our cold fusion server or do we just
make a folder under our web site so when a user clicks on the browse
menu, the file they attach has to be saved to our server first and then
I can attach the saved file to an e-mail? How do we create this folder?
Does it have to be an FTP site or just a folder on our web site? How do
we make the permissions work correctly since a folder under our web site
is not writable to except by the web editors that work here? Please let
me know what I can do to get this to work. Thanks.

Andy

You can use any folder on the CF server or any system to which it has
access. By default the CF service runs as a localsystem user, so would
generally only have access to the local server file system. This is
determined in the Services panel on window systems, I'm not sure how it
works for other OS's.

If you are talking about the "Browse" button that is displayed in the
web page for the <input type="file" ..> that does not browse the server,
it browses the local user's system. You do not and can not influence
this in any way. When a user selects a file from their system and
submits the form, the file is uploaded to the webserver through the HTTP
response header or URL query string, depending on whether the form is a
post or get form.

You then use the <cffile action="upload" ...> tag on the form's action
page to get the file from the web server and write it to a location you
define the "file" parameter of the flag. This file system access is
done under the permissions of the ColdFusion server, again 'LocalSystem'
by default.

You can then use this newly saved file in you <cfmailparam ...> tag to
attach it to an email. After this if you like you can use a <cffile
action="delete" ...> to remove the file.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 08, 2006 Sep 08, 2006
Ian,
Your first paragraph is what I am talking about. I need a folder on the CF server for where the file being uploaded needs to be stored temporarily until the e-mail is sent with the attachement. How do I create this folder? Does the IT dept. have to set this up? Once it is on the CF server, how do I write the code for the destination of what the path is? <cfmailparam file = "#ExpandPath(".\")##TempFileName#" >

Andy
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 08, 2006 Sep 08, 2006
You can use any folder you like. Either one you already have or one you
set up. That choice is up to you.

You will then write your upload tag to use that directory.
<cffile
action="upload"
destination="c:\attachments"
filefield="mailAttachment"
nameconflict="makeunique">

This will return a structure named "file" with all the details about the
upload.

Then your attachment tag will use these results.
<cfmailparam
file="c:\attachments\#file.serverFile#">

The if you wanted to delete this file.
<cffile
action="delete"
file="c:\attachments\#file.serverFile#">

From this point on I think you would find the documentation for the
cffile and cfmailparam tags as well as the "Managing Files on the
Server" and "Sending and Receiving E-Mail" chapters of "Developing
ColdFusion MX Applications" to be very helpful. There is even code
samples complete with form and action pages for uploading files.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 09, 2006 Sep 09, 2006
To ensure your code is portable among different servers and configurations I would use ColdFusion's temp directory to store the file:

<cffile action="UPLOAD" filefield="mailAttachment" destination="#GetTempDirectory()#" nameconflict="MAKEUNIQUE">
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 21, 2006 Sep 21, 2006
Ian,
I finally got the code to work for uploading files. I put the code in there you sent to delete the file from the folder right away and it works, but the page won't go through if I don't upload anything. I just want to be able to send in a request without an attachment sometimes. How do I get this to work? Here is the delete code I've tried below. Here is the error message I get:

Cold Fusion Error Diagnostics Information:

Location: 64.122.92.67

Browser type: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)

Error occured at: Thu Sep 21 12:49:43 CDT 2006

Detailed diagnostics:
Variable TEMPFILENAME is undefined.
The error occurred on line 238.

URL of template that was accessed: http://www.ironwoodelectronics.com/quote.html

URL's string query:

Template being processed when error occured: /catalog/personal/quoteaction1.cfm

Andy
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 21, 2006 Sep 21, 2006
If you don't upload a file to attach there is not going to be a file
structure created. If no file structure is created you can not
reference it in file delete code.

So don't. Put a conditional statement around the delete file code so
that it does not try and delete a file that was never uploaded when no
attachment is submitted.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 21, 2006 Sep 21, 2006
LATEST
Slight problem. If you are intending to delete the file in the same template that generates the mail, your mail won't get sent. The file has to be in the expected location when the spool gets emptied.

We handle this with a nightly job.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources