如何使用mailparser php

时间:2013-11-11 12:33:50

标签: php

我试图在浏览器中显示email / home / mail / 02092290663929254284:2,S的电子邮件标题,使用php和mailpaser。我正在使用此代码

    <?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once('/usr/local/MimeMailParser.class.php');
$path = '/home/mail/02092290663929254284:2,S';
$Parser = new MimeMailParser();
$Parser->setPath($path);

$to = $Parser->getHeader('to');
$delivered_to = $Parser->getHeader('delivered_to');
$from = $Parser->getHeader('from');
$subject = $Parser->getHeader('subject');
$text = $Parser->getMessageBody('text');
$html = $Parser->getMessageBody('html');
$attachments = $Parser->getAttachments();
$headers=$Parser->getHeadersRaw('headers');

echo $Parser->getHeadersRaw(); # print email header

foreach($headers as $headersvalue){
    echo "$headersvalue<br>";
}

?>

但是我收到了错误 警告:为/usr/local/test.php

中的foreach()提供的参数无效

为什么?怎么解决?

谢谢

2 个答案:

答案 0 :(得分:1)

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once('/usr/local/MimeMailParser.class.php');
$path = '/home/mail/02092290663929254284:2,S';
$Parser = new MimeMailParser();
$Parser->setPath($path);

$to = "to@to.com" //Reciver email address
$form   =   "sender@sender.com" // Sender email address
$subject    =   "Email Subject"; //Email Subject
$emailBody  =   "Email Message"; // Your email message

$to = $Parser->getHeader($to);
$delivered_to = $Parser->getHeader('delivered_to');
$from = $Parser->getHeader($form);
$subject = $Parser->getHeader($subject);
$text = $Parser->getMessageBody('text');
$html = $Parser->getMessageBody($emailBody);
$attachments = $Parser->getAttachments();
$headers=$Parser->getHeadersRaw('headers');

echo $Parser->getHeadersRaw(); # print email header

foreach($headers as $headersvalue){
    echo "$headersvalue<br>";
}

?>

立即尝试。

$ to是Receiver电子邮件地址
$ form是发件人电子邮件地址
$ subject是电子邮件主题
$ email正文是您的电子邮件

答案 1 :(得分:1)

$Parser->getHeadersRaw();不是数组。而且,您正在使用$Parser->getHeadersRaw('headers');。输出的内容如下:var_dump($Parser->getHeadersRaw('headers'));

以下是一个示例用法:

<?php

require_once('MimeMailParser.class.php');

$path = 'path/to/mail.txt';
$Parser = new MimeMailParser();
$Parser->setPath($path);

$to = $Parser->getHeader('to');
$from = $Parser->getHeader('from');
$subject = $Parser->getHeader('subject');
$text = $Parser->getMessageBody('text');
$html = $Parser->getMessageBody('html');
$attachments = $Parser->getAttachments();

?>