Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Upload

Status
Not open for further replies.

dulla

Technical User
Feb 3, 2003
54
0
0
Hello. I am trying send information with a file upload on a form straight into an email account. The fields get sent however the file upload wont get sent. Is there anyway to send information to an email account with the file upload to show up as an attachment? here is my html form. I have not selected ENCTYPE="multipart/form-data" because the other form information will not be sent then:


The reference cgi script (below) is called form.cgi. I need to know how to accomodate this form to allow file uploads. thank you.

#!/usr/bin/perl
##############################################################################
# Cliff's Form Mailer Version 1.0 #
# Copyright 1998 Shaven Ferret Productions #
# Created 6/4/98 #
# Available at #
##############################################################################
# COPYRIGHT NOTICE #
# Copyright 1998 Shaven Ferret Productions All Rights Reserved. #
# #
# This script can be used\modified free of charge as long as you don't #
# change this header thing, or the part that gives me credit for writing #
# script in the e-mail. If you really need to remove this part, go to #
# . By using this script #
# you agree to indemnifyme from any liability that might arise from its use. #
# In simple English, if this script somehow makes your computer run amuck #
# and kill the pope, it's not my fault. #
# #
# Redistributing\selling the code for this program without prior written #
# consent is expressly forbidden. #
##############################################################################

# Enter the location of sendmail.
$mailprogram = "/usr/lib/sendmail -t";

# Enter the fields that are required. They should each be in quotes and
# separated by a comma. If no fields are required, change the next line
# to @required = ();
@required = ();

# Enter your e-mail address. Be sure to put a \ in front of the @.
# (user@domain.com becomes user\@domain.com)
$youremail = "user955891\@marshonsfashions.com";

##############################################################################
# Congratulations! You've finished defining the variables. If you want to, #
# you can continue screwing with the script, but it isn't necessary. #
##############################################################################

# Put the posted data into variables

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

# Check for all required fields

foreach $check(@required) {
unless ($FORM{$check}) {
print "Content-type: text/html\n\n";
print &quot;<html><head><title>Missing Information</title></head>\n&quot;;
print &quot;<body><h1>Missing Information</h1><br>\n&quot;;
print &quot;I'm sorry, but it would appear that you've forgotten to\n&quot;;
print &quot;fill out the $check field. Please click\n&quot;;
print &quot;back and try again.\n&quot;;
print &quot;</body></html>\n&quot;;
exit;
}
}

# Check the senders email

if ($FORM{'email'}) {
unless ($FORM{'email'} =~ /\w+@\w+.\w+/) {
print &quot;Content-type: text/html\n\n&quot;;
print &quot;<html><head><title>Bad E-mail</title></head>\n&quot;;
print &quot;<body><h1>Bad E-mail</h1><br>The e-mail address that you've\n&quot;;
print &quot;entered, $FORM{'email'}, is invalid. Please click back and\n&quot;;
print &quot;try again.\n&quot;;
exit;
}
}

open (MAIL,&quot;|$mailprogram&quot;);
print MAIL &quot;To: $youremail\n&quot;;
print MAIL &quot;From: $FORM{'email'}\n&quot;;
print MAIL &quot;Subject: $FORM{'subject'}\n&quot;;
print MAIL &quot;Hello. The following information has been submitted:\n\n&quot;;
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
unless ($name eq &quot;response&quot; || $name eq &quot;email&quot; || $name eq &quot;subject&quot;) {
print MAIL &quot;$name: $value\n&quot;;
}
}
close MAIL;

if ($FORM{'response'} && $FORM{'email'}) {
open (RESPONSE, $FORM{'response'});
@response = <RESPONSE>;
close(RESPONSE);
open (MAIL,&quot;|$mailprogram&quot;);
print MAIL &quot;To: $FORM{'email'}\n&quot;;
print MAIL &quot;From: $youremail\n&quot;;
print MAIL &quot;Subject: $FORM{'subject'} -- Autoresponse\n&quot;;
foreach $line (@response) {
print MAIL &quot;$line&quot;;
}
print MAIL &quot;The person you are communicating with is using Cliff's\n&quot;;
print MAIL &quot;Form Mailer Script. If you would like to add this script\n&quot;;
print MAIL &quot;to your web site, you can find it and many more perl scripts at\n&quot;;
print MAIL &quot; close MAIL;
}

print &quot;Content-type: text/html\n\n&quot;;
print &quot;<html><head><title>Thank you!</title></head>\n&quot;;
print &quot;<body><h1>Thank you!</h1><br>Thanks for your input! \n&quot;;
if ($FORM{'response'} && $FORM{'email'}) {
print &quot;You should receive an autoresponse shortly.<p>\n&quot;;
}
print &quot;Please click back.\n&quot;;
 
If you do not set up the type as multipart/form-data the browser will not send the file. Period.

If you select this type the form data still goes out, your problem is probably your hand coded form parsing routine which really is not compliant.

Use CGI.pm, it handles all of this for you natively.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top