2

我正在将收到的电子邮件传送到 handler.php。我可以成功地通过管道传输它并且它正在工作,但是当它从邮件标题中取出变量时,例如“主题”或“收件人”或“邮件正文”,我遇到了一些问题。这是我从这里得到的代码

这是代码:

 <?php
//Assumes $email contains the contents of the e-mail
//When the script is done, $subject, $to, $message, and $from all contain appropriate values

//Parse "subject"
$subject1 = explode ("\nSubject: ", $email);
$subject2 = explode ("\n", $subject1[1]);
$subject = $subject2[0];

//Parse "to"
$to1 = explode ("\nTo: ", $email);
$to2 = explode ("\n", $to1[1]);
$to = str_replace ('>', '', str_replace('<', '', $to2[0]));

$message1 = explode ("\n\n", $email);

$start = count ($message1) - 3;

if ($start < 1)
{
    $start = 1;
}

//Parse "message"
$message2 = explode ("\n\n", $message1[$start]);
$message = $message2[0];

//Parse "from"
$from1 = explode ("\nFrom: ", $email);
$from2 = explode ("\n", $from1[1]);

if(strpos ($from2[0], '<') !== false)
{
    $from3 = explode ('<', $from2[0]);
    $from4 = explode ('>', $from3[1]);
    $from = $from4[0];
}
else
{
    $from = $from2[0];
}
?> 

对于 Gmail 电子邮件,它可以很好地获取主题、发件人、收件人和邮件正文,但它不适用于来自 Yahoo 的传入电子邮件。

是否有任何与所有著名电子邮件服务提供商兼容的通用 php 类?如果有人从 RoundCube 或其他电子邮件发件人发送电子邮件怎么办?我怎样才能成功检测到变量?

谢谢!

4

2 回答 2

1

您在评论中描述的消息格式是多部分 Mime编码。

有很多事情需要考虑——如果电子邮件是 HTML纯文本格式,有嵌入的图像、附件等,该怎么办?

如果您使用的 PHP 版本是使用MailParse扩展构建的,那么它们应该为您提供一组相当简单的工具供您使用。

Google 代码上还有Mime 电子邮件解析器,我以前没有使用过,但看起来相当简单。

于 2012-09-25T23:09:49.143 回答
1

如果您需要一些非常简单的东西,这是一个起点:

list($headers, $message) = explode("\n\n", $email);

$header = imap_rfc822_parse_headers($headers);

// You can now access
$header->from;
$header->to;
$header->subject;

电子邮件部分(即使单独使用)也可以使用imap_rfc822_parse_adrlist().

于 2012-09-25T23:24:04.083 回答