检测电子邮件的哪个部分具有HTML格式的正文

时间:2014-04-24 14:38:20

标签: php email imap

我正在尝试阅读两种不同类型的电子邮件。

一种电子邮件在部件号2下面有html主体,我可以通过这种方式阅读,

imap_fetchbody($imap_connection, $imap_msgno, '2');

另一种有html主体在1.2部分下面,为此我使用以下内容来获取正文,

imap_fetchbody($imap_connection, $imap_msgno, '1.2');

现在我的问题是如何知道哪个部分有电子邮件的html正文?

2 个答案:

答案 0 :(得分:0)

我建议您使用一个处理此问题的库。

我正在处理邮件一段时间并不容易。可能有多个部分,HTML主体所在的位置(例如,当您回复某人的邮件时,有两个部分(原始邮件)和另一部分(您的响应)在两个不同的ID下。获取电子邮件正文的最佳方式是解析imap_fetchstructure($imap_connection, $imap_msgno);返回的对象。但是它很痛苦,我知道我在说什么。

回到我的建议,其中一个库是我的,您可以自由使用它:https://github.com/greeny/MailLibrary

答案 1 :(得分:0)

我正在读这两个并试图找出哪个有html,

这是我的黑客对我有用。

$message = imap_fetchbody($imap_connection, $imap_msgno, '2'); 
//read the body assuming its type 2

//apply respective decoding function here. eg, imap_base64 OR imap_8bit 
//OR imap_qprint based on the email's encoding info.

if($message == strip_tags($message)) { //checking if the $message has html tags.
    //if no html, read the other part.
    $message = imap_fetchbody($imap_connection, $imap_msgno, '1.2');
    // ..... 
}
相关问题