phpbar.de logo

Mailinglisten-Archive

[php] Threads aus Mails bilden

[php] Threads aus Mails bilden

Gerd Terlutter gerd at MplusB.de
Mit Sep 17 14:34:32 CEST 2003


David Danier wrote:
> Ich versuche derzeit ein ähnliches Script zu schreiben 
> 
> Aber was anderes:
> Kannst du mir einen Tip geben, wie man die Headerfelder (To, From etc.) 
> richtig ausliest? 
...

> Beispiel für mehrzeiliges To:
> ----
> To: Max Mustermann <max at domain.de>,
>  susi at domain.de
> ----
> (so erhalten vom Mozilla Mailclient)
> 
> Die 2. Zeile ist eingerückt, das müsste eigentlich der passende Anhaltspunkt 
> sein, aber trotzdem klappt das alles nicht. 
Habe ein script in perl geschrieben, du könntest aber auch 
pear::mailparse verwenden. Hab dir mal ein funktionierendes script 
gelistet, müsstest du allerdings für deine verhältnisse anpassen. Wie 
gesagt, es arbeitet mit Archivierung und schreibt Eckdaten von mail in 
eine DB, die Paras solltest du Tessten und an deine Bedürfnisse 
anpassen. Hoffe es hilft.

Gruss,

Gerd

perlscript :
#!/usr/bin/perl
use Mail::Header;
use Mail::Address;
use DBI; #nur für DB-connections, doku
#error-codes:
#9=wrong params
#12=can't open file (mail)
#13=user must match 'crew' or 'bm'
#14=wrong 1.param for number of params
#15=no connection to the database
#16=error by prepare db-statement
#17=error by execute db-statement
#18=prepare,execute+selectrow_array failed(used to get a single entry)

my $dbhost="localhost";
my $errno=0;
my $argc;
$argc=@ARGV;
my $user;
my $host;
my $tabletype;
my $message;
#print $argc;
if($argc<2){$errno=9;}
else{
   $tabletype=lc$ARGV[0];
   $message=$ARGV[1];
   $user=lc$ARGV[2];
   $host=$ARGV[3];
   my $table;
   my $usrtbl;
   if($argc==2 && $tabletype eq "ore"){
     $table="t_toshore";
     $usrtbl="shorereceiver";
   }
   elsif($argc==4 && $tabletype eq "ip"){
     $table="t_toship";
     $usrtbl="shipreceiver";
     if($user ne "crew"){
       if($user ne "bm"){$errno=13;}
     }
   }
   else{$errno=14;}
   open(MESSAGE,"<$message")
     or $errno=12;

   if($errno==0){
     my $head = new Mail::Header \*MESSAGE;
     my $size= -s $message;
     close(MESSAGE);
     $head->fold(4096);
#print "----\n";
#####1. check all 'To' fields from the mail
     my @tmptos;
     foreach my $item($head->tags){
       if ($item ne "To"){;}
       else {
	@tmptos= $head->get($item);
       }
     }
####define the variable and values for the database
     my $val1=$user;
     my $val2=$host;
     my $val3;
     my $val4=$head->get("Cc")?$head->get("Cc"):undef;
     my $val5=$head->get("BCc")?$head->get("BCc"):undef;
     my $val6=$head->get("From")?$head->get("From"):undef;
     my $val7=$size;
     my $val8=$head->get("Subject")?$head->get("Subject"):undef;
     my $val9=$message;
     my $val10=$head->get("Date")?$head->get("Date"):undef;
     my $val11=undef;
     my $val12=undef;
     my $val13=undef;
     my $val14=undef;

####define connection to the DB
     my $datasource="DBI:mysql:database=mbmailparse;host=$dbhost";
     my $user="mailuser";
     my $pw="mail";
####at first make one DB-entry for the mail(size,subject,...)
     my 
$dbh=DBI->connect($datasource,$user,$pw,{'PrintError'=>0,'RaiseError'=>0});
     if($DBI::errstr){
       #print "err by connecting!\n";
       exit(15);
     }else{
       #print "connection o.k.!\n";
     }
     my $sth=$dbh->prepare("INSERT INTO $table 
(user,host,cc,bcc,from_,size,".
			  "subject,mbmid,orgdate,indate,outdate,notevaluate,".
			  "remarks) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
     if($dbh->errstr){
       #print "err by prepare mail!\n";
       exit(16);
     }else{
       #print "prepare o.k.!\n";
     }
 
$sth->execute($val1,$val2,$val4,$val5,$val6,$val7,$val8,$val9,$val10,$val11,$val12,$val13,$val14);
     if($dbh->errstr){
       #print "err by exec mail!\n";
       exit(17);
     }else{
       #print "exec mail o.k.!\n";
     }
     $sth->finish();
     my $mailID=$dbh->selectrow_array("SELECT max(id) FROM $table");
     #print "$mailID letzte ID";
####check the differnt To: fields in a mail
     my @refs;
     my $tmpaddr;
     my $boolie=0;
     foreach my $from(@tmptos){
       my @addresses = Mail::Address->parse($from);
       foreach my $add(@addresses){
	my $addr=$add->address;
	@refs=(@refs, $addr);
       }
     }
####now make the DB-entries for the receiver, but for each only one
####e.g.:To: gerd at MplusB.de, "Gerd Terlutter" gerd at MplusB.de
####this is one person
     $sth=$dbh->prepare("INSERT INTO $usrtbl (receiver,mailid)VALUES(?,?)");
     if($dbh->errstr){
       #print "err by prepare for user!\n";
       exit(16);
     }else{
       #print "prepare for user o.k.!\n";
     }
     my $filled;
      my $idx=0;
     foreach(@refs){
       if($idx>0){
	$filled=$dbh->selectrow_array("SELECT receiver FROM $usrtbl WHERE 
receiver='$_' AND mailid=$mailID");
     if($dbh->errstr){
       #print "err by prepare+exec+selectrow_array(get ID)!\n";
       exit(18);
     }else{
       #print "mixing(get ID) o.k.!\n";
     }
	$filled="" unless defined $filled;
	if(defined($filled) && $filled ne $_){
	  $sth->execute($_,$mailID);
	  if($dbh->errstr){
	    #print "err by exec user!\n";
	    exit(17);
	  }else{
	    #print "exec user o.k.!\n";
	  }
	  $sth->finish();
	}
       }
       else{
	$sth->execute($_,$mailID);
	if($dbh->errstr){
	  #print "err by exec user!\n";
	  exit(17);
	}else{
	  #print "exec user o.k.!\n";
	}
	$sth->finish();
       }
       $filled=undef;
       $idx++;
     }
     $dbh->disconnect();#close the DB connection


#####DB-fields and their meaning
#user,===>first part of 'to',e.g. crew.XXX at XXX.com means 'crew'. other:'bm'
#host,===>2.nd part, a vessel-shortcut
#Cc,
#BCc,
#From,
#size,===>get it by savepath
#Subject,
#MBMid,===>savepath
#orgDate===>the date parsed from the mail(original-date)
#inDate,===>current date, leave blank (undef,NULL).mysql fills correct
#outDate,===>only if mail hold back
#notEvaluate,===>other headers
#remarks===>internal field,e.g. hold back a mail or to big
####test-end
   }
}
#print "errorcode:$errno\n";
exit($errno);




-- 
--------------------------------------------------------
# Gerd Terlutter        | Mueller+Blanck Software GmbH #
# gerd at MplusB.de        | Gutenbergring 38             #
# gerd.terlutter at web.de | D-22848 Noderstedt           #
# tel:0171/6992579      | tel:+49 40 500 171-1         #
# Buero:040/500171-17   | fax:+49 40 500 171-71        #
--------------------------------------------------------



php::bar PHP Wiki   -   Listenarchive