|
|
Question : How to allow users to upload a file - via an email form - using a Perl script?
|
|
Ok, here is what I am trying to do ..
I have a standard type of Perl script that is parsing the information that comes in from an HTML form. Such as name, address, phone, email address, etc. But, I simply want to allow the user to browser through their computer and locate a file (on their computer) and include the file as an attachment to their email - when it is sent to me. For example, if someone wanted to attach a Microsoft Word copy of their resume along with their email.
What do I need to include in my Perl script ... and HTML form - that will allow this to work ... and also operate securely and handle possible error messages, etc., etc.
Here is my standard code.
Please let me know how to modify my Perl script (and HTML form) so I can incorporate the addition of a FILE attachment to the email.
Thanks.
HERE IS MY CURRENT PERL CODE:
#!/usr/local/bin/perl
&get_form_data(); &send_email; &print_thankyou_page;
sub get_form_data
{ # Get the input read(STDIN, $buffer, $ENV{ 'CONTENT_LENGTH' } );
# Split the name-value pairs @pairs = split (/&/, $buffer); foreach $pair (@pairs)
{ ($name, $value) = split(/=/, $pair);
# Un-Webify plus signs and %-encoding $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s///g; $FORM{$name} = $value; } }
sub send_email {
$to = "my\@emailaddress.com"; $mailprog = '/usr/lib/sendmail';
open(MAIL,"|$mailprog -t"); print MAIL "From: $FORM{'email'}\n"; print MAIL "To: $to\n"; print MAIL "Subject: Email Message \n\n"; # print out the form results print MAIL "First Name: \t$FORM{'first_name'} \n\n"; print MAIL "Last Name: \t$FORM{'last_name'} \n\n"; print MAIL "E-mail Address: \t$FORM{'email'} \n\n"; print MAIL "Status: \t$FORM{'status'} \n\n"; print MAIL "Other \(if other is selected for Status\): \t$FORM{'other'} \n\n"; print MAIL "Comments: \t$FORM{'comments'} \n\n"; print MAIL " \n"; close(MAIL); }
sub print_thankyou_page {
print "Content-type: text/html\n\n"; print "\n\n\n\n"; print "THANK YOU SUBMITTING YOUR INFORMATION.\n\n"; print "\n"; print "We have received your comments and will forward this to the webmaster.
\n"; print "Click below to return back: \n"; print "http://www.ourwebsite.com/\" TARGET=\"\_top\">Our Company
\n\n\n"; print "\n"; }
The HTML page can be found at:
http://www.piccorp.com/hr_feedback_form.html
=============================================
I'd greatly appreciate some help on how to add this feature.
Thanks ... Gary
PS: Depending upon what is involved to help me with this, I'd be very willing to increase the amount of points. Just let me know.
|
Answer : How to allow users to upload a file - via an email form - using a Perl script?
|
|
bobbyg1986,
... & here is the PERL code
======hr_feedback_form.pl #!/usr/local/bin/perl
$|++; ## Disable output buffering
print "Content-type: text/html\n\n";
use CGI;
## Create a new CGI object $query=new CGI;
## Hash with the keys as the field names as they appear in the HTML form ## The value is of the format Given name::mandatory -y or not -n ## Note- the file upload field will not appear here. %fields=( 'first_name' => 'First Name::y' ,'last_name' => 'Last Name::y' ,'email' => 'Email Address::y' ,'status' => 'Status::n' ,'other' => 'Other::n' ,'comments' => 'Comments::n' );
## All HTML form elements for file upload MUST start with this string $upload_file_prefix='file';
$max_size='1048576'; ## Max size of each uploaded file, in bytes $max_size='58000'; ## Max size of each uploaded file, in bytes
$mailprog ="/usr/lib/sendmail"; ## Full path to the sendmail MTA $to ='gary@your_Server.com'; ## Your email id.
$boundary = "Message-Boundary-" . time();
## Get a list of field names from the HTML form foreach ($query->param){ next if (!($query->param($_))); ## Ignore any empty values
if (/^$upload_file_prefix\d+/){ ## This is upload file field push(@upload,$_); }else{ push(@fields,$_); } }
## Perform basic validations here foreach $var (@fields){ next if (!$fields{$var}); ## Ignore submit button
($actual,$mandatory)=split(/::/,$fields{$var});
## Read the HTML form values into PERL variables. ## The name of the variable will be same as the form variable. ## Eg $first_name, $last_name etc... ${$var}=$query->param($var);
${$var}=~ s/^\s+//; ## Remove leading ... ${$var}=~ s/\s+$//; ## trailing white spaces.
if ($mandatory=~ /^y$/i){ ## Mandatory field if (!${$var}){ ## Empty!! not allowed print "$actual cannot be empty!! \n"; exit; } } }
## Now that the file has been uploaded to the server... ## ...send it out as an email attachment. open(MAIL, "| $mailprog -t "); print MAIL "To: $to\n"; print MAIL "From: <>Web Form\n"; print MAIL "Subject: HR Email Contact Form!\n"; print MAIL "MIME-Version: 1.0\n"; print MAIL "Content-type: Multipart/1;\n"; print MAIL "\tboundary=$boundary\n"; print MAIL "\n"; print MAIL "\n"; print MAIL "--$boundary\n";
## Add the text part of the message print MAIL "Content-type: text/plain; charset=US-ASCII\n";
print MAIL "Content-description: Mail message body\n"; print MAIL "Content-transfer-encoding: 7BIT\n"; print MAIL "\n";
## Now add the users input to the email foreach $var (@fields){ next if (!$fields{$var}); ## Ignore submit button
($actual,$mandatory)=split(/::/,$fields{$var});
## Read the HTML form values into PERL variables. ## The name of the variable will be same as the form variable. ## Eg $first_name, $last_name etc... ${$var}=$query->param($var);
${$var}=~ s/^\s+//; ## Remove leading ... ${$var}=~ s/\s+$//; ## trailing white spaces.
print MAIL "$actual: ${$var}\n"; } print MAIL "--$boundary\n";
## Now process all the uploaded files foreach $up (@upload){ $uploaded_file=$query->param($up); $tmp_uploaded_file=$query->param($up);
## Replace all \'s with /'s $tmp_uploaded_file=~ s/\\/\//g; @tmp_uploaded_file=split(/\//,$tmp_uploaded_file);
$filename = $tmp_uploaded_file[$#tmp_uploaded_file];
## Allow upload of ONLY those files that have the extension ... ## ...of pdf or htm. # if (!($uploaded_file =~ /\.pdf$/i || # $uploaded_file =~ /\.doc$/i || $uploaded_file =~ /\.txt$/i)){ # print "Invalid file type!!\n"; # exit; # }
$max_limit=0; ## Max limit reached? 0 = no, 1 = yes $body=""; ## The file type is valid (extension is valid!!). while($bytesread=read($uploaded_file,$data,1024)){ $size+=$bytesread; $body.=$data; if ($size > $max_size){ ## Limit exceeded print "$uploaded_file exceeded the maximum limit \n"; $body=""; $max_limit=1; last; } } close($uploaded_file);
if (!$max_limit){ print "$uploaded_file has been uploaded \n"; print "Size = ",$size," byte(s) \n"; ## Send the MIME details for the attachment. print MAIL "Content-Type: application/octet-stream; name=\"$filename\"; type=Unknown\n"; print MAIL "Content-transfer-encoding: BASE64\n"; print MAIL "Content-disposition: attachment\n"; print MAIL "\n"; $encoded_body= &my_encode_base64($body); ## Use base64 for encoding the contents ## Send out the contents. print MAIL $encoded_body."\n"; print MAIL "--$boundary\n"; } } print MAIL "--$boundary--\n"; close(MAIL);
print "Your question/comment has been sent. \n";
sub my_encode_base64 ($;$){ my $res = ""; my $eol = $_[1]; $eol = "\n" unless defined $eol; pos($_[0]) = 0; # ensure start at the beginning while ($_[0] =~ /(.{1,45})/gs) { $res .= substr(pack('u', $1), 1); chop($res); } $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs # fix padding at the end my $padding = (3 - length($_[0]) % 3) % 3; $res =~ s/.{$padding}$/'=' x $padding/e if $padding; # break encoded string into lines of no more than 76 characters each if (length $eol) { $res =~ s/(.{1,76})/$1$eol/g; } $res; }
|
|
|
|
|