如何将我的imap连接提供给php-mime-mail-parser

时间:2016-03-03 17:24:49

标签: php email parsing imap

我已经下载了php-mime-mail-parser(https://github.com/php-mime-mail-parser/php-mime-mail-parser),我很难搞清楚如何将我的imap连接连接到包来解析收到的电子邮件。

几年前我成功地做到了这一点,我无法记住我是怎么做到的。

我的BROKEN代码如下:

 $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;

任何帮助都会受到赞赏

1 个答案:

答案 0 :(得分:0)

看起来似乎是能够在将文本传递给php-mime-mail_parser之前将imap_header与imap_body合并。

完整的代码如下:

   $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;