2016-03-03 69 views
0

我已經下載了php-mime-mail-parser(https://github.com/php-mime-mail-parser/php-mime-mail-parser),我很難搞清楚如何將我的imap連接連接到包解析傳入的電子郵件。如何將我的imap連接加入到php-mime-mail-parser中

我在很多年前成功地完成了這項工作,並且我不記得我是如何做到的。

我破碎的代碼如下所示:

$mbox = imap_open($host, $login, $password) or die("connection errror: " . imap_last_error()); 


     $Parser = new PhpMimeMailParser\Parser(); 
     $Parser->setStream($mbox); 
// We can get all the necessary data 
     $to = $Parser->getHeader('to'); 
     $from = $Parser->getHeader('from'); 
     $subject = $Parser->getHeader('subject'); 

     $text = $Parser->getMessageBody('text'); 
     $html = $Parser->getMessageBody('html'); 
     echo PHP_EOL . "*** $to : $from : $subject : $text" . PHP_EOL; 
// loop the attachments 
     $attachments = $Parser->getAttachments(); 

     if (count($attachments) > 0) { 
      //print_r($attachments); 
      //exit; 
      foreach ($attachments as $attachment) { 
       echo 'Filename : ' . $attachment->getFilename() . '<br />'; // logo.jpg 
       echo 'Filesize : ' . filesize($attach_dir . $attachment->getFilename()) . '<br />'; // 1000 
       echo 'Filetype : ' . $attachment->getContentType() . '<br />'; // image/jpeg 
      } 
      exit; 
     } 
     exit; 

任何幫助將appreciatd

回答

0

這似乎把戲是能夠與imap_body合併imap_header文字傳遞給PHP-MIME之前-mail_parser。

完整的代碼如下:

$mbox = imap_open($host, $login, $password) or die("connection errror: " . imap_last_error()); 
    for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++) { 

     $imap_body = imap_fetchheader($mbox, $jk) . imap_body($mbox, $jk); 

     $Parser = new PhpMimeMailParser\Parser(); 
     $Parser->setText($imap_body); 

     // We can get all the necessary data 
     $to = $Parser->getHeader('to'); 
     $from = $Parser->getHeader('from'); 
     $subject = $Parser->getHeader('subject'); 

     $text = $Parser->getMessageBody('text'); 
     $html = $Parser->getMessageBody('html'); 

     echo PHP_EOL . "*** $to : $from : $subject : $savedirpath" . PHP_EOL; 
     echo PHP_EOL . "**********************************" . PHP_EOL; 

     $Parser->saveAttachments($savedirpath); 
     $attachments = $Parser->getAttachments(); 

     if (count($attachments) > 0) { 
      //print_r($attachments); 
      //exit; 
      foreach ($attachments as $attachment) { 
       echo 'Filename : ' . $attachment->getFilename() . '<br />'; // logo.jpg 
       echo 'Filesize : ' . filesize($savedirpath . $attachment->getFilename()) . '<br />'; // 1000 
       echo 'Filetype : ' . $attachment->getContentType() . '<br />'; // image/jpeg 
      } 
     } 
    } 
    exit;