imap_open将消息正文显示为纯文本

时间:2014-02-05 15:34:09

标签: php

我在PHP中使用imap_open,但邮件正文显示为:

<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\" xmlns=\"http://www.w3.org/TR/REC-html40\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=us-ascii\">
<meta name=\"Generator\" content=\"Microsoft Word 14 (filtered medium)\">
<!--[if !mso]><style>v\\:* {behavior:url(#default#VML);}
o\\:* {behavior:url(#default#VML);}
w\\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
    {font-family:Calibri;
    panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
    {font-family:Tahoma;
    panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
    {font-family:Verdana;
    panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0cm;
    margin-bottom:.0001pt;
    font-size:12.0pt;
    font-family:\"Times New Roman\",\"serif\";}
a:link, span.MsoHyperlink
    {mso-style-priority:99;
    color:blue;
    text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
    {mso-style-priority:99;
    color:purple;
    text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
    {mso-style-priority:99;
    mso-style-link:\"Balloon Text Char\";
    margin:0cm;
    margin-bottom:.0001pt;
    font-size:8.0pt;
    font-family:\"Tahoma\",\"sans-serif\";}
span.EmailStyle18
    {mso-style-type:personal-reply;
    font-family:\"Calibri\",\"sans-serif\";
    color:#1F497D;}
span.BalloonTextChar
    {mso-style-name:\"Balloon Text Char\";
    mso-style-priority:99;
    mso-style-link:\"Balloon Text\";
    font-family:\"Tahoma\",\"sans-serif\";}
.MsoChpDefault
    {mso-style-type:export-only;
    font-size:10.0pt;}
@page WordSection1
    {size:612.0pt 792.0pt;
    margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
    {page:WordSection1;}
--></style>

......这继续

这是我正在使用的代码。

//try to connect
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect: ' . imap_last_error());
//grab emails
$emails = imap_search($inbox,'ALL');
//if emails are returned, cycle through each...
if($emails) {
    //put the newest emails on top
    rsort($emails);

    //for every email...
    foreach($emails as $email_number)  {
        //get information specific to this email
        $header=imap_headerinfo($inbox,$email_number);
        $structure = imap_fetchstructure($inbox,$email_number);
        $from = $header->from[0]->mailbox."@".$header->from[0]->host;

        $toaddress = array_shift($header->to);
        $toaddress->mailbox."@".$toaddress->host;
        $replyto=$header->reply_to[0]->mailbox."@".$header->reply_to[0]->host;
        $datetime=date("Y-m-d H:i:s",$header->udate);
        $subject=$header->subject;

        //get message body
        $message = imap_fetchbody($inbox, $email_number, 1.2);

        if(base64_decode($message, true)) {
            //message body if base64 encoded
            $message = base64_decode($message);
        } else {
            //message body is not base64 encoded
            $message = $message = imap_fetchbody($inbox, $email_number, 1);
        }

        $message = strip_tags($message);

    }
}

我添加了if语句,以查看邮件正文是否为base64编码,是否要对其进行解码,但有时仍显示如上所示。其他时候,它显示为纯文本,但有很大的换行符。

有没有办法让整个邮件正文成为纯文本?

3 个答案:

答案 0 :(得分:3)

尝试$msg = imap_fetchbody($inbox,$email_number,1.1);
而不是$msg = imap_fetchbody($inbox,$email_number,1.2);

http://php.net/manual/en/function.imap-fetchbody.php#89002上找到了有关此主题的更多信息:

imap-fetchbody()会将附加的电子邮件消息与其余电子邮件部分内联解码,但处理附加电子邮件时的工作方式与主电子邮件消息不一致。

如果电子邮件只包含文本正文并且没有任何mime附件,则imap-fetchbody()将为每个请求的部件号返回以下内容:

  • (空) - 整条消息
  • 0邮件标题
  • 1 - 正文

如果电子邮件是MIME格式的多部分邮件,并且包含纯文本和HTML的邮件文本,并且有一个file.ext附件,则imap-fetchbody()将返回类似于以下内容的内容:要求的部件号:

  • (空) - 整条消息
  • 0邮件标题
  • 1 MULTIPART / ALTERNATIVE
  • 1.1 - TEXT / PLAIN
  • 1.2 - TEXT / HTML 2 - file.ext

现在,如果您将上述电子邮件附加到包含纯文本和HTML消息文本的电子邮件中,imap_fetchbody()将使用此类型的部件号系统:

  • (空) - 整条消息
  • 0 - 邮件标题
  • 1 - MULTIPART / ALTERNATIVE
  • 1.1 - TEXT / PLAIN
  • 1.2 - TEXT / HTML
  • 2 - MESSAGE / RFC822(整个附加消息)
  • 2.0 - 附加邮件标题
  • 2.1 - TEXT / PLAIN
  • 2.2 - TEXT / HTML
  • 2.3 - file.ext

答案 1 :(得分:1)

正在使用fMailbox类来提取电子邮件,请参阅此链接以了解fmailbox的使用情况

http://flourishlib.com/docs/fMailbox

配置fmailbox很简单

<?php 
require_once('flourish/fCore.php');
require_once('flourish/fEmail.php');
require_once('flourish/fMailbox.php');
 $this -> mailbox = new fMailbox($emailType, $emailHost, $emailUsername, $emailPassword);
 $messageses = $this -> mailbox -> listMessages();
foreach ($messageses as $messages) {
            $messageinfo[] = $this -> mailbox -> fetchMessage($messages['uid']);
}
?>

它将与电子邮件相关的所有细节

array(
    'received' => '28 Apr 2010 22:00:38 -0400',
    'headers'  => array(
        'received' => array(
            0 => '(qmail 25838 invoked from network); 28 Apr 2010 22:00:38 -0400',
            1 => 'from example.com (HELO ?192.168.10.2?) (example) by example.com with (DHE-RSA-AES256-SHA encrypted) SMTP; 28 Apr 2010 22:00:38 -0400'
        ),
        'message-id' => '<4BD8E815.1050209@flourishlib.com>',
        'date' => 'Wed, 28 Apr 2010 21:59:49 -0400',
        'from' => array(
            'personal' => 'Will Bond',
            'mailbox'  => 'tests',
            'host'     => 'flourishlib.com'
        ),
        'user-agent'   => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.9) Gecko/20100317 Thunderbird/3.0.4',
        'mime-version' => '1.0',
        'to' => array(
            0 => array(
                'mailbox' => 'tests',
                'host'    => 'flourishlib.com'
            )
        ),
        'subject' => 'This message is encrypted'
    ),
    'text'      => 'This message is encrypted',
    'decrypted' => TRUE,
    'uid'       => 15,
    'text'      =>'The plaintext body',
    'html'      =>'The HTML body'
    'attachment'=>'if any as array'
);

答案 2 :(得分:1)

首先检查相关邮件的结构map-fetchstructure。 可能是空的附带或嵌入或转发的电子邮件......等等。

您可以使用bellow函数分隔嵌套子项数组中的消息,并选择所需的项目:

source:

function create_part_array($structure, $prefix="") {
    //print_r($structure);
    if (sizeof($structure->parts) > 0) {    // There some sub parts
        foreach ($structure->parts as $count => $part) {
            add_part_to_array($part, $prefix.($count+1), $part_array);
        }
    }else{    // Email does not have a seperate mime attachment for text
        $part_array[] = array('part_number' => $prefix.'1', 'part_object' => $obj);
    }
   return $part_array;
}
// Sub function for create_part_array(). Only called by create_part_array() and itself. 
function add_part_to_array($obj, $partno, & $part_array) {
    $part_array[] = array('part_number' => $partno, 'part_object' => $obj);
    if ($obj->type == 2) { // Check to see if the part is an attached email message, as in the RFC-822 type
        //print_r($obj);
        if (sizeof($obj->parts) > 0) {    // Check to see if the email has parts
            foreach ($obj->parts as $count => $part) {
                // Iterate here again to compensate for the broken way that imap_fetchbody() handles attachments
                if (sizeof($part->parts) > 0) {
                    foreach ($part->parts as $count2 => $part2) {
                        add_part_to_array($part2, $partno.".".($count2+1), $part_array);
                    }
                }else{    // Attached email does not have a seperate mime attachment for text
                    $part_array[] = array('part_number' => $partno.'.'.($count+1), 'part_object' => $obj);
                }
            }
        }else{    // Not sure if this is possible
            $part_array[] = array('part_number' => $prefix.'.1', 'part_object' => $obj);
        }
    }else{    // If there are more sub-parts, expand them out.
        if (sizeof($obj->parts) > 0) {
            foreach ($obj->parts as $count => $p) {
                add_part_to_array($p, $partno.".".($count+1), $part_array);
            }
        }
    }
}