0

好的,我正在尝试在 php 中使用 imap 命令从邮箱中读取电子邮件,然后对它们做一些事情(没关系)。我得到了发送主机、主题和发件人,然后是电子邮件的正文。如果电子邮件是 mime 类型的'quoted-printable for Text/Html',那么我可以使用 imap_qprint($text) 很好地阅读它。但是,如果是 mime 类型“8bit for Text/Html”,我尝试使用 imap_8bit($text) 仍然会得到垃圾。

这是代码

<?php
$codes = array("7bit","8bit","binary","base64","quoted-printable","other");
$stt = array("Text","Multipart","Message","Application","Audio","Image","Video","Other");



# Connect to the mail server and grab headers from the mailbox

$mail = imap_open('{remote.nh-group.co.uk:110/pop3}', 'itsupport', 'xxxx');
$headers = imap_headers($mail);

?>
<html>
<head>
<title>Reading a Mailbox including multipart emails from within 
PHP</title>
</head>
  <body>
  <?
$pictures = 0;
$html = "";
# loop through each email

for ($n=1; $n<=count($headers); $n++) {
$header = imap_header($mail, $n);

  $st = imap_fetchstructure($mail, $n);
    $multi = $st->parts;
    $nparts = count($multi);
    if ($nparts == 0) {
            $html2 .=  "* SINGLE part email<br>";
    } else{
            $html2 .=  "* MULTI part email<br>";
    }

# look at the main part of the email, and subparts if they're present

    for ($p=0; $p<=$nparts; $p++) {
            $text =imap_fetchbody($mail,$n,$p);

            if ($p ==  0) {
                    $it = $stt[$st->type];
                    $is = ucfirst(strtolower($st->subtype));
                    $ie = $codes[$st->encoding];
            } else {
                    $it = $stt[$multi[$p-1]->type];
                    $is = ucfirst(strtolower($multi[$p-1]->subtype));
                    $ie = $codes[$multi[$p-1]->encoding];
            }

# Report on the mimetype

            $mimetype = "$it/$is";
            $mimeshow .=  "<br /><b>Part $p ... ";
            $mimeshow .=  "Encoding: $ie for $mimetype</b><br />";

# decode content if it's encoded (more types to add later!)
         if ($ie == "8bit") {
                   $body = imap_8bit($text);
                    }
            if ($ie == "base64") {
                    $body = imap_base64($text);
                    }
            if ($ie == "quoted-printable") {
                    $body = imap_qprint($text);
                    }


# If it's a .jpg image, save it (more types to add later)

            if ($mimetype == "Image/Jpeg") {
                    $picture++;
                    $fho = fopen("imx/mp$picture.jpg","w");
                    fputs($fho,$realdata);
                    fclose($fho);
                    # And put the image in the report, limited in size
                    $html2 .= "<img src=/demo/imx/mp$picture.jpg width=150><br />";
            }
# Add the start of the text to the message
            $shorttext = substr($text,0,8000);
            if (strlen($text) > 8000) $shorttext .= " ...\n";
            $html2.=  nl2br(htmlspecialchars($shorttext))."<br>";

    }
echo $mimeshow.'<br>';  
$subject=$header->subject;
echo '<br>Subject: '.$subject.'<br>';
echo '<br>Sender: '.$header->senderaddress.'<br>';

$splitheader = get_object_vars($header);

$host=$splitheader[from][0]->host;
echo '<br>Header: '.$host.'<br>';
echo $body.'<br>';   


}
?>
</body>
</html>   

我的收件箱中有两封电子邮件。第一个编码为单部分 8 位,第二个编码为多部分,带引号打印

结果如下


第 0 部分 ... 编码:Text/Html 的 8 位

主题:这是对票证内容的测试票证#5916 (#5916#)

发件人:NHG & Clinicare IT & T 支持

标题:nh-group.co.uk

MIME 版本:1.0 收到:来自 hsmx05.antispameurope.com (83.246.65.101) 由 remote.nh-group.co.uk (192.168.1.19) 与 Microsoft SMTP 服务器 (TLS) id 14.3.123.3;2017 年 11 月 1 日,星期三 19:03:01 +0000 收到:来自 delivery.mailspamprotection.com (184.154.208.35),作者为 mx-gate68-hz2.hornetsecurity.com;2017 年 11 月 1 日星期三 20:03:01 +0100 收到:来自 ns1.ukm17.siteground.biz ([77.104.173.217] helo=3Dukm17.siteground.biz)=09by se6.mailspamprotection.com with esmtps (TLSv1.2: ECDHE-RSA-AES256-GCM-SHA384:256)=09(Exim 4.89)=09(envelope-from )=09id 1e9yHu-0004pY-L8=09for it.support@nh-group.co.uk; 2017 年 11 月 1 日星期三 14:02:57 -0500 收到:来自 [127.0.0.1] (port=3D57646 helo=3Dukm17.siteground.biz)=09by ukm17.siteground.biz with smtp (Exim 4.89_3-0adfb60-XX) =09(信封来自)=09id 1e9yHt-0003cX-9r=09for it.support@nh-group.co.uk; 2017 年 11 月 1 日,星期三 19:02:53 +0000 至:https://github.com/PHPMailer/PHPMailer) X-Mailer: Awesome Support/4.3.2 Content-Type: text/html; charset=3D"utf-8" Content-Transfer-Encoding: 8bit X-AntiAbuse: 添加此标头是为了跟踪滥用情况,请将其包含在 a= ny 滥用报告中 X-AntiAbuse: Primary Hostname - ukm17.siteground.biz X- AntiAbuse:原始域 - nh-group.co.uk X-AntiAbuse:发起者/呼叫者 UID/GID - [47 12] / [47 12] X-AntiAbuse:发件人地址域 - ukm17.siteground.biz X-Get-Message -Sender-Via: ukm17.siteground.biz: none X-Originating-IP: 77.104.173.217 X-SpamExperts-Domain: ukm17.siteground.biz X-SpamExperts-Username: 77.104.173.217 Authentication-Results: mailspamprotection.com; auth=3Dpass smtp.auth=3D77.= 104.173.217@ukm17.siteground.biz X-SpamExperts-Outgoing-Class: ham X-SpamExperts-Outgoing-Evidence: Combined (0.09) X-Recommended-Action: 接受 X-Filter -ID:

第 0 部分 ... 编码:Multipart/Alternative 的 7 位

第 1 部分 ... 编码:7bit for Text/Plain

第 2 部分 ... 编码:Text/Html 的引号可打印

主题:测试
发件人:Will Evans
标题:tyxan.com
测试
--
Will Evans
m:+xxxxxxxx
w:网址


如您所见,第二封电子邮件 $body 为我提供了一些我可以使用的普通格式文本。然而,第一个给了我......嗯,一些东西!

任何想法都非常感谢。

4

0 回答 0