Rikarda: Mail-Anhang auslesen und auf Server speichern

Beitrag lesen

Hallo Ihr Lieben,

habe mir ein Script gebastelt, mit dem ich per imap meine Mails abrufe und dann PDF-Anhänge auf dem Server speichere. Das Ganze funktioniert auch prima, allerdings nur dann, wenn die eMail nur einen Anhang hat. Sobald mehrere PDFs anhängen klappt es nicht mehr. :-(

Könnt Ihr mi helfen?

Danke schon jetzt für Eure Bemühungen.

LG, Rikarda

Hier mein Script:

  
<?  
//Mailbox  
$cfgMailserver = "{localhost:143/novalidate-cert}INBOX";  
$cfgMailuser = "user";  
$cfgMailpassword = "pass";  
$cfgAttachDir = ".";  
  
  
//Mailbox öffnen  
$mbox = imap_open($cfgMailserver,$cfgMailuser,$cfgMailpassword);  
$gesamtemail = imap_num_msg($mbox);  
  
echo 'Insgesamt: '.$gesamtemail.'<hr>';  
  
//eMails einzeln abarbeiten  
for ($num = 1; $num <= $gesamtemail; $num++)  
{  
 $h = @imap_header($mbox,$num);  
  
 //Sender  
 $mailfrom = trim(strtolower($h->from[0]->mailbox."@".$h->from[0]->host));  
 $mailfromname = trim(($h->from[0]->personal));  
  
 echo 'von: '.$mailfrom.'<br>';  
  
 //Empfänger  
 $mailto = trim(strtolower($h->to[0]->mailbox."@".$h->to[0]->host));  
 $mailtoname = trim(($h->to[0]->personal));  
  
 echo 'an: '.$mailto.'<br>';  
  
 //Subject  
 $mailsubjectarray = imap_mime_header_decode($h->subject);  
 $mailsubject = trim($mailsubjectarray[0]->text);  
  
 echo 'Betreff: '.$mailsubject.'<br>';  
  
 //Antwort senden an  
 $mailreply = trim(strtolower($h->reply_to[0]->mailbox."@".$h->reply_to[0]->host));  
 $mailreplyname = trim(($h->reply_to[0]->personal));  
 if(trim($mailreply)){$mailfrom = $mailreply;}  
 if(trim($mailreplyname)){$mailfromname = $mailreplyname;}  
  
 //Sendezeitpunkt  
 $mailtime = trim($h->udate);  
  
 echo 'Zeit: '.$mailtime.'<br>';  
  
 //Format  
 $structure = imap_fetchstructure($mbox, $num);  
 //Bodyformat  
 if($structure->type == 1)  
 {  
  //Body Multipart, Attachments  
  $mailbody = imap_fetchbody($mbox,$num,'1',FT_INTERNAL);  
 }  
 else  
 {  
  //Body plain  
  $mailbody = imap_body($mbox,$num,FT_INTERNAL);  
 }  
 $mailbody = trim(quoted_printable_decode($mailbody));  
  
 //Attachements  
 $attach = "";  
 $att = "";  
 $struct = imap_fetchstructure($mbox,$num);  
 $contentParts = count($struct->parts);  
 if ($contentParts >= 2)  
 {  
  for ($i=2;$i<=$contentParts;$i++)  
  {  
   $att[$i-2] = imap_bodystruct($mbox,$num,$i);  
  }  
 	for ($k=0;$k<sizeof($att);$k++)  
  {  
 	 if ($att[$k]->parameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value    == "US-ASCII")  
   {  
 	  if ($att[$k]->parameters[1]->value != "")  
    {  
     $attach[$k] = $att[$k]->parameters[1]->value;  
    }  
 	 }  
   elseif ($att[$k]->parameters[0]->value != "iso-8859-1" && $att[$k]->parameters[0]->value != "ISO-8859-1")  
   {  
 	  $attach[$k] = $att[$k]->parameters[0]->value;  
 	 }  
 	}  
 }  
  
 if (sizeof($attach) > 0)  
 {  
  for ($j=0;$j<sizeof($attach);$j++)  
  {  
   if(trim($attach[$j]))  
   {  
    //Original Dateiname  
    $filename = trim($attach[$j]);  
  
    echo 'Anhang '.$j.': '.$filename.'<br>';  
  
    //Dateityp  
    $filetype = strtolower((eregi_replace("\.","",strrchr($filename,"."))));  
  
    echo 'Anhang-Typ: '.$filetype.'<br>';  
  
    //Wenn PDF-Datei vorhanden, Datei speichern  
    if($filetype == "pdf")  
    {  
      //Attachment holen  
      $fileContent = imap_fetchbody($mbox,$num,$j+2);  
      $fileContent = imap_base64($fileContent);  
  
      //Attachment speichern  
      $filename_save = $mailtime."_".$filename;  
      $fp = fopen($cfgAttachDir."/".$filename_save ,"wb+");  
      fwrite ($fp,$fileContent);  
      fclose($fp);  
    }  
   }  
  }  
 }  
  
 //eMail löschen  
// imap_delete($mbox,$num);  
  
echo "<hr>";  
  
}  
imap_expunge ($mbox);  
imap_close ($mbox);  
@mysql_close($conn);  
?>