#!/usr/bin/perl use strict; use warnings; use DBI; use File::Copy; my $dbh = undef; my $sth = undef; my %const = ( dbhost => 'localhost', dbname => 'mail', dblogin => 'mail', dbpassword => 'p4v1li0n', dbhandler => \$dbh, statementhandler => \$sth, statement => '', maildir => '/usr/db_mail/', currentfile => '', mail_datetime => '', mail_headers => '', mail_from => '', mail_to => '', mail_cc => '', mail_subject => '', mail_body => '', ); sub dbconnect { $const{dbhandler} = DBI->connect("DBI:mysql:$const{dbname}:$const{db +host}",$const{dblogin},$const{dbpassword}); } sub dbdisconnect { if($const{dbhandler}) { $const{statementhandler}->finish() if $sth; $const{dbhandler}->disconnect(); } } sub insert { $const{mail_body} = substr $const{mail_body}, 0, 1000000; #eerste MB $const{statement} = qq[INSERT INTO archive VALUES(?,?,?,?,?,?,?,?)]; dbconnect(); $const{statementhandler} = $const{dbhandler}->prepare($const{stateme +nt}); $const{statementhandler}->execute(undef,$const{mail_datetime},$const +{mail_from},$const{mail_to},$const{mail_cc},$const{mail_subject},$con +st{mail_headers},$const{mail_body}); dbdisconnect(); } sub parse { open(FILE, $_[0]) or return; # print "$_[0]\n"; my $mail = join('', <FILE>) if (-f $_[0]) && ($_[0] =~ /^$const{mail +dir}/); close(FILE); return if !$mail; local $/=undef; ($const{mail_datetime}) = $mail =~ m/Delivery-date: (.*?)\n/s; ($const{mail_from}) = $mail =~ m/From: (.*?)\n/s; ($const{mail_to}) = $mail =~ m/To: (.*?)\n/s; ($const{mail_cc}) = $mail =~ m/Cc: (.*?)\n/s; ($const{mail_subject}) = $mail =~ m/Subject: (.*?)\n/s; ($const{mail_headers}) = $mail =~ m/^(.*?)\n\n/s; ($const{mail_body}) = $mail =~ m/\n\n(.*?)$/s; insert(); unlink $_[0] if move($_[0],"$const{maildir}parsed/$const{currentfile +}") == 1; } opendir(MAILDIR, $const{maildir}) or exit 10; #Can't open maildir foreach my $thisfile (readdir(MAILDIR)) { $const{currentfile} = $thisfile; parse("$const{maildir}$thisfile"); } closedir(MAILDIR); exit 0;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Archive mail into a database
by jdporter (Paladin) on Jan 11, 2006 at 16:01 UTC | |
|
Re: Archive mail into a database
by jdporter (Paladin) on Jan 11, 2006 at 16:26 UTC | |
|
Re: Archive mail into a database
by jdporter (Paladin) on Jan 11, 2006 at 17:03 UTC | |
|
Re: Archive mail into a database
by zby (Vicar) on Jan 11, 2006 at 16:40 UTC |