我将发送到服务器的电子邮件通过管道传输到Zend Framework2索引(遵循MVC),然后发送到我的控制器。
public function incomingMailAction()
{
$message ='';
$stdin = fopen('php://stdin', 'r');
while($line = fgets($stdin)) {
$message .= $line;
}
fclose($stdin);
// Parse e-mail here and store in database (including attachments)
}
我可以处理在数据库部分的存储,我只是不知道如何获取原始消息,然后将其转换为有用的东西(收件人,发件人,ReplyTo,抄送,密件抄送,标题,附件...ect)。
谢谢!
发布于 2013-02-02 16:05:49
您可以使用Zend\Mail\Message::fromString($rawMessage);
,但它不会解码MIME正文。
发布于 2016-03-01 00:44:22
我也尝试过用ZF2解析电子邮件,但实际上我在Zend Mail组件的源代码中发现了一条注释,说明对消息的解码在待办事项列表中,而且还没有实现。目前似乎没有简单的方法来做到这一点。
取而代之的是,我推荐使用php-mime-mail-parser --我最终还是使用了那个库。它使用pecl扩展mailparse (您可能需要安装它)的功能,并且非常简单。下面是一些可以让你入门的例子:
$message = new \PhpMimeMailParser\Parser();
$message->setText($rawMail); // Other functions to set a filename exists too
// All headers are retrieved in lowercase, "To" becomes "to"
// and "X-Mailer" becomes "x-mailer"
$recipient = $message->getHeader('to');
$date = $message->getHeader('date');
$xmailer = $message->getHeader('x-mailer');
// All headers can be retrieved at once as a simple array
$headers = $message->getHeaders();
$recipient = $headers['to'];
// Attachments can be retrieved all at once as "Attachment" objects
$attachments = $message->getAttachments();
foreach($attachments as $attachment) {
$attachment_as_array = array(
'type' => $attachment->getContentType(),
'name' => $attachment->getFilename(),
'content' => (string)$attachment->getContent(),
);
}
由于该库使用PHP的现有扩展,并且在内存管理方面似乎非常高效,因此它可能比ZF更适合解析电子邮件-而且它也非常易于使用。对我来说,唯一的缺点是在每台服务器上额外安装mailparse pecl扩展。
发布于 2013-02-02 02:22:44
public function incomingMailAction()
{
$message ='';
$stdin = fopen('php://stdin', 'r');
while($line = fgets($stdin)) {
$email .= $line;
}
fclose($stdin);
$to1 = explode ("\nTo: ", $email);
$to2 = explode ("\n", $to1[1]);
$to = str_replace ('>', '', str_replace('<', '', $to2[0]));
list($toa, $tob) = explode('@', $to);
}
https://stackoverflow.com/questions/14652557
复制相似问题