#!/usr/local/bin/perl
use strict;         
             
## configuration                       
             
my $FROM = 'webmaster@your.site';      
my $TO = 'upload@your.site';           
my $SUBJECT = 'File upload';           
my $INCLUDE_META = 1;                  
             
## end configuration                   
             
use CGI qw(:all);                      
             
my @params = param();                  
if (my $error = cgi_error()) {         
  print header(-status => $error);     
  exit 0;    
}            
             
print        
  header,    
  start_html("Upload"),                
  h1("Upload"),                        
  hr,        
  start_multipart_form,                
  table(Tr(td(p('upload:')),           
           td(filefield('uploaded_file'))),                      
        Tr(td(p('email as type:')),    
           td(radio_group('type', [qw(binary text)]))),          
        Tr(td({ -colspan => 2 }, submit))),                      
  end_form,                  
  hr;        
             
if (@params) {              
  require MIME::Lite;                  
  if (my $file = param('uploaded_file')) {                      
    my $info = uploadInfo($file) or die "info?";                 
    my $msg = MIME::Lite->new          
      (Type => 'multipart/mixed',      
       From => $FROM, To => $TO, Subject => $SUBJECT);           
    if ($INCLUDE_META) {               
      $msg->attach                     
        (Type => 'TEXT', Encoding => '7bit',                     
         Data => [                     
                  "Upload info:\n",    
                  (map { "$_ => $info->{$_}\n" } sort keys %$info),                        
                  "ENV:\n",            
                  (map { "$_ => $ENV{$_}\n" } sort keys %ENV),   
                 ],                    
        );   
    }        
    $msg->attach                       
      ((param('type') eq 'text' ?      
        (Type => 'TEXT', Encoding => 'quoted-printable') :       
        (Type => 'BINARY', Encoding => 'base64')),               
        (FH => $file),                
      );              
    if ($msg->send_by_smtp('localhost')) {
      print p("Upload sent by email.");
    } else { 
      print                  
        p("An error occurred... here's what would have been sent:"),                       
        pre($msg->as_string);          
    }        
  }          
}            
print end_html;                        
                       