如何将我的 imap 连接馈送到 php-mime-mail-parser 中



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

几年前我成功地做到了这一点,我不记得我到底是怎么做到的。

我的损坏代码看起来像:

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

        $Parser = new PhpMimeMailParserParser();
        $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;

任何帮助将不胜感激

似乎的

诀窍是在将文本传递给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 PhpMimeMailParserParser();
        $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;

最新更新