Php 如何使用Zend Framework 2解析原始电子邮件?
我将发送到服务器的电子邮件通过管道传输到我的Zend Framework 2索引(遵循MVC),然后发送到我的控制器Php 如何使用Zend Framework 2解析原始电子邮件?,php,email,zend-framework2,Php,Email,Zend Framework2,我将发送到服务器的电子邮件通过管道传输到我的Zend Framework 2索引(遵循MVC),然后发送到我的控制器 public function incomingMailAction() { $message =''; $stdin = fopen('php://stdin', 'r'); while($line = fgets($stdin)) { $message .= $line; } fclose($stdin);
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)
}
我可以处理数据库部分的存储,我只是不知道如何将原始消息转换成有用的内容(发送、发送、回复、抄送、密件抄送、标题、附件等)
谢谢<代码>公共函数incomingMailAction()
{
$message='';
$stdin=fopen('php://stdin","r",;
而($line=fgets($stdin)){
$email.=$line;
}
fclose($stdin);
$to1=爆炸(“\nTo:”,$email);
$to2=爆炸(“\n”,$to1[1]);
$to=str_replace('>','',str_replace('您可以使用
Zend\Mail\Message::fromString($rawMessage)
但它不会解码MIME正文。我也试图用ZF2解析电子邮件,但我在Zend Mail组件的源代码中发现一条注释,对邮件进行解码在todo列表中,尚未实现。目前似乎没有简单的方法来实现这一点
相反,我建议使用-我最终使用了该库。它使用了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扩展。我正在寻找一些解决方案我喜欢这样,但是使用Zend 2框架。我该如何解码它?并获取附件?这似乎是目前最好的解决方案。使用Zend1进行了尝试,最终使用了这种方法。非常好!
$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(),
);
}