#!/usr/bin/perl # script by for mailing form data # # This is a cgi script in perl for mailing data from a HTML form. # The HTML form must be in such a way, that it is submitted # with POST as multipart/form-data. # The form data (including submitted files) is then sent via e-mail. # # Wolfgang Hartmut Nitsche (2012) # http://www.stanford.edu/~nitsche/ # http://nitsche.mobi/ ############################### # # # SPECIFY YOUR E-MAIL ADDRESS # # # ############################### # Here you can decide to which e-mail address the data will be sent. # For example # $EMailAddress = 'nobody@example.com'; # meens that it will be sent to nobody@example.com . # If you are concerned that robots might read this script to collect # e-mail addresses for sending spam, you can also write your address # in a more difficult to understand way, for example # $EMailAddress = 'nob'.'ody'.char(64).'exa'.'mple.com'; # is also acceptable. $EMailAddress = 'nobody@example.com'; ################################################### # # # READ IN THE SUBMITTED DATA AND USE PERL MODULES # # # ################################################### $QueryString = $ENV{'QUERY_STRING'}; $ScriptFilename = $ENV{'SCRIPT_FILENAME'}; $ScriptUrl = $ENV{'SCRIPT_URI'}; use MIME::QuotedPrint; use MIME::Base64; use CGI; $data = CGI->new; ############################################################## # # # SHOW SOURCE CODE OF THIS SCRIPT IF REQUESTED AND THEN EXIT # # # ############################################################## if($QueryString eq 'source') { print "Content-type: text/plain\n\n"; open(FILE, "<$ScriptFilename"); while() { print $_; } close(FILE); exit; } ################################### # # # PROCESS THE SUBMITTED FORM DATA # # # ################################### $boundary = 'stanford_1868_1884=leeland'; $SummaryMessage = "[START]\n\n"; $AttachedFiles = "\n\n"; @ListOfNames = $data->param; foreach (@ListOfNames) { $Name = $_; $Value = $data->param($Name); $SummaryMessage = $SummaryMessage . $Name . ' :' . "\n"; $SummaryMessage = $SummaryMessage . $Value . "\n"; $FileHandle = $data->upload($Name); if (defined $FileHandle) { $FileData = ''; $IOHandle = $FileHandle->handle; while($bytesread = $IOHandle->read($buffer, 1024)) { $FileData = $FileData.$buffer; } $FileSize=length($FileData); $FilteredName = substr($Name.'_____________________', 0, 20); $FilteredValue = substr('_____________________'.$Value, -20); $FilteredFileName = $FilteredName.'_'.$FilteredValue; $FilteredFileName =~ s/[^\.\w]/_/g; $FilteredFileName =~ s/_+/_/g; $SummaryMessage = $SummaryMessage . "(File of $FileSize Bytes received.)\n"; $AttachedFiles = $AttachedFiles . "--$boundary\n"; $AttachedFiles = $AttachedFiles . "Content-Type: application/octet-stream\n"; $AttachedFiles = $AttachedFiles . "Content-disposition: attachment;\n"; $AttachedFiles = $AttachedFiles . "\tfilename=\"$FilteredFileName\"\n"; $AttachedFiles = $AttachedFiles . "Content-Transfer-Encoding: base64\n"; $AttachedFiles = $AttachedFiles . "\n"; $AttachedFiles = $AttachedFiles . encode_base64($FileData); $AttachedFiles = $AttachedFiles . "\n\n"; } $SummaryMessage = $SummaryMessage . "\n- - - - - - - - - -\n\n"; } $SummaryMessage = $SummaryMessage . "[END]"; ############### # # # SEND E-MAIL # # # ############### open(MAIL, '|/usr/sbin/sendmail -t') or die "ERROR: Sendmail has not been opened\n"; print MAIL "To: $EMailAddress\n"; print MAIL "Subject: form data\n"; print MAIL "Mime-Version: 1.0\n"; print MAIL "Content-Type: multipart/mixed; boundary=\"$boundary\"\n"; print MAIL "\n"; print MAIL "--$boundary\n"; print MAIL "Content-Type: text/plain; charset=utf-8\n"; print MAIL "Content-Transfer-Encoding: quoted-printable\n"; print MAIL "\n"; print MAIL encode_qp($SummaryMessage); print MAIL "\n\n"; print MAIL $AttachedFiles; print MAIL "--$boundary--\n"; print MAIL "\n"; close(MAIL) or die "ERROR: Sendmail has not been closed\n"; ############################# # # # DISPLAY CONFIRMATION PAGE # # # ############################# $SummaryMessageAsHTML = $SummaryMessage; $SummaryMessageAsHTML =~ s/&/&/g; $SummaryMessageAsHTML =~ s//>/g; $SummaryMessageAsHTML =~ s/\n/\n
\n/g; print "Content-type: text/html; charset=utf-8\n\n"; print ''."\n"; print ''."\n"; print ''."\n"; print 'Form submission'."\n"; print ''."\n"; print ''."\n"; print ''."\n"; print '

Form succesfully submitted

'."\n"; print '

Your form has been submitted, and the following data has beeen received:

'."\n"; print '

'."\n"; print $SummaryMessageAsHTML ."\n"; print '

'."\n"; print '

You can view the source of the cgi script which collects the form data.

'."\n"; print ''."\n"; print ''."\n"; ####### # # # END # # # #######