#!/usr/bin/perl -Tw use diagnostics; use strict; use IO::All; use DBI; my $db = DBI->connect( "dbi:Pg:dbname=db;", "user", "password" ) or die( "error message ..." ); $db->{RaiseError} = 1; $db->{pg_server_prepare} = 1; $db->{AutoCommit} = 1; my @MAILDIRS = ( '/path/to/maildir/1/', '/other/path/to/maildir/2/', ); my $sql = " INSERT INTO tabmail(para,de,assunto,data,corpo,cabecalho) VALUES(?,?,?,?,?,?) "; my $sth = $db->prepare($sql); my $i = 0; foreach my $dir (@MAILDIRS ) { for my $mail ( io($dir)->all ) { print "\n", $i, ": ",io($mail)->filename,"\n"; my $flag = 0; my ($headers,$body); my ($from, $to,$date,$subject); foreach my $linha ( io($mail)->getlines ) { $from = $linha if $linha=~/^From: .*/; $to = $linha if $linha=~/^To: .*/; $date = $linha if $linha=~/^Date: .*/; $subject = $linha if $linha=~/^Subject: .*/; if ( $linha =~/[^\n].*/ && $flag != 1 ) { $headers .= $linha, } elsif ($linha=~/^\n/ && $flag != 1) { $flag =1; } else { $body .= $linha; } } $sth->execute( $to || "-", $from || "-", $subject || "-", $date || "-", $body || "-", $headers || "-" ); $i++; } }