Update : Took advice of jdporter. Kept settings in hash for convenience.

Update : Added die() flowcontrol at close() by advice of DigitalKitty

Previously one huge mailbox file (think about 3GB) was used
to store the archived e-mail. To make searching in
this easier and to make future manipulation easier I wrote a little script that writes e-mail data (dumped by
the Exim MTA into files) into a MySQL db.

#!/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;

In reply to Archive mail into a database by jkva

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.