$const{mail_to} = $1 if $mail =~ m/To: (.*?)\n/s; #### ($const{mail_to}) = $mail =~ m/To: (.*?)\n/s; #### use IO::File; use File::Copy; sub insert(\%) { my $hr = shift; $hr->{body} = substr $hr->{body}, 0, 1000000; #first MB(well, almost) of body dbconnect(); $statementhandler = $dbhandler->prepare( "INSERT INTO $dbtable VALUES(?,?,?,?,?,?,?,?)" ); $statementhandler->execute( undef, @{$hr}{qw(date from to cc subj hdrs body)} ); dbdisconnect(); } sub parse { my $file = shift or return; my $fh = IO::File->new($file, 'r') or die "Error reading $file - $!\n"; local $/; local $_ = $fh->getline or return; my %rec; ($rec{date}) = /Delivery-date: (.*?)\n/; ($rec{from}) = /From: (.*?)\n/; ($rec{to }) = /To: (.*?)\n/; ($rec{cc }) = /Cc: (.*?)\n/; ($rec{subj}) = /Subject: (.*?)\n/; ($rec{hdrs}) = /^(.*?)\n\n/; ($rec{body}) = /\n\n(.*?)$/; insert( %rec ); move( $file => $maildir.'parsed/'.$currentfile ) or unlink $file or die "Error moving/deleting $file - $!\n"; }