#!/usr/bin/perl -wT
use strict;

#use CGI::Debug;


#BEGIN{    warn "-PM1: $CGI::POST_MAX\n";}

use CGI;

#BEGIN{    warn "-PM2: $CGI::POST_MAX\n";}

BEGIN
{
    $CGI::POST_MAX = 1021;
    $CGI::DISABLE_UPLOADS = 0;
}

#BEGIN{    warn "-PM3: $CGI::POST_MAX\n";}

warn "4\n";
 warn "PM4: $CGI::POST_MAX\n";

use Data::Dumper;
warn Dumper $CGI::Q;

use MIME::Lite;

my $q = new CGI;

warn Dumper $q;

if(my $err = $q->cgi_error)
{
               print $q->header(-status=>$err),
                     $q->start_html('Problems'),
                     $q->h2('Request not processed'),
                     $q->strong($err);
               exit 0;

}

warn "5\n";
#BEGIN{    warn "-PM5: $CGI::POST_MAX\n";}

delete $ENV{PATH};
print $q->header;

if( $q->param('file') )
{
    my $msg = MIME::Lite->new(
	  From    =>'www-data@liljegren.org',
	  To      =>'jonas@liljegren.org',
	  Subject =>'A file to you',
	  Type    =>'multipart/mixed'
	 );

    ### Add parts (each "attach" has same arguments as "new"):
    $msg->attach(Type     =>'TEXT',
		 Data     =>"Here's the file you wanted"
		);

    my $conent_type = $q->uploadInfo($q->param('file'))->{'Content-Type'};
    $msg->attach(Type     => $conent_type,
		 FH       => $q->upload('file'),
		 Filename => $q->param('file'),
		 Disposition => 'attachment'
		);

    ### Send in the "best" way (the default is to use "sendmail"):
    $msg->send;

    print $q->start_html('File sent'),
      $q->h1('File sent'), $q->end_html;
}
else
{
    print $q->start_html('Upload form'),
      $q->h1('Upload form'),
	$q->start_multipart_form(),
	  $q->filefield('file'),
	    $q->submit, $q->end_form, $q->end_html;
}
__END__
