2010-11-24 52 views

回答

19

好PHP IMAP的功能是不好玩的工作。本頁中的用戶解釋了獲取電子郵件的不一致之處:http://php.net/manual/en/function.imap-fetchbody.php#89002

使用他的有用信息,我創建了一種可靠的方式來獲取電子郵件的正文文本。

$bodyText = imap_fetchbody($connection,$emailnumber,1.2); 
if(!strlen($bodyText)>0){ 
    $bodyText = imap_fetchbody($connection,$emailnumber,1); 
} 
$subject = imap_headerinfo($connection,$i); 
$subject = $subject->subject; 

echo $subject."\n".$bodyText; 
+0

非常感謝。 Google搜索了4個小時後,我找到了工作示例。 – 2012-09-14 19:18:56

0

你也可以嘗試這些

content-type:text/html 

$message = imap_fetchbody($inbox,$email_number, 2); 

content-type:plaintext/text 

$message = imap_fetchbody($inbox,$email_number, 1); 
1

我的解決方案(包括所有類型和字符集作品):

function format_html($str) { 
    // Convertit tous les caractères éligibles en entités HTML en convertissant les codes ASCII 10 en $lf 
    $str = htmlentities($str, ENT_COMPAT, "UTF-8"); 
    $str = str_replace(chr(10), "<br>", $str); 
    return $str; 
} 


// Start 

$obj_structure = imap_fetchstructure($imapLink, $obj_mail->msgno); 

// Recherche de la section contenant le corps du message et extraction du contenu 
$obj_section = $obj_structure; 
$section = "1"; 
for ($i = 0 ; $i < 10 ; $i++) { 
    if ($obj_section->type == 0) { 
     break; 
    } else { 
     $obj_section = $obj_section->parts[0]; 
     $section.= ($i > 0 ? ".1" : ""); 
    } 
} 
$text = imap_fetchbody($imapLink, $obj_mail->msgno, $section); 
// Décodage éventuel 
if ($obj_section->encoding == 3) { 
    $text = imap_base64($text); 
} else if ($obj_section->encoding == 4) { 
    $text = imap_qprint($text); 
} 
// Encodage éventuel 
foreach ($obj_section->parameters as $obj_param) { 
    if (($obj_param->attribute == "charset") && (mb_strtoupper($obj_param->value) != "UTF-8")) { 
     $text = utf8_encode($text); 
     break; 
    } 
} 

// End 
print format_html($text); 
相關問題