php

时间:2017-11-01 21:19:58

标签: php imap mime 8-bit quoted-printable

好的,我在php中尝试使用imap命令从邮箱中读取电子邮件,然后对它们执行某些操作(并不重要)。我收到了发送主机,主题和发件人,然后收到了电子邮件的正文。如果电子邮件是mime类型,则引用可打印的Text / Html'然后我可以使用imap_qprint($ text)读取它。但是,如果是mime类型' 8bit用于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部分......编码:8位文字/ Html

主题:这是对票证内容票号#5916(#5916#)

的测试 发送者:NHG&amp; Clinicare IT&amp; T支持

标题:nh-group.co.uk

MIME-Version:1.0收到:来自hsmx05.antispameurope.com(83.246.65.101)的remote.nh-group.co.uk(192.168.1.19),带有Microsoft SMTP Server(TLS)id 14.3.123.3; 2017年11月1日星期三19:03:01 +0000收到:来自delivery.mailspamprotection.com(184.154.208.35)by 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(信封来自)= 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要:主题:这是对票证内容的测试票证#5916(#5916#)X-PHP-Originating-Script:1028:class-phpmailer.php日期:星期三, 2017年11月1日19:02:53 +0000来自:NHG&amp; Clinicare IT&amp; T支持回复:NHG&amp; Clinicare IT&amp; T支持消息ID:&lt; 8611c203ec1ee6173e753fe4ae3b72c0@corporate.nhg-uk.com> X-Mailer:PHPMailer 5.2.22(https://github.com/PHPMailer/PHPMailer)X-Mailer:Awesome Support / 4.3.2 Content-Type:text / html;字符集= 3D&#34; UTF-8&#34;内容传输编码:8位X-AntiAbuse:此标题已添加到跟踪滥用行为中,请将其包含在= ny滥用行为报告中X-AntiAbuse:主要主机名 - 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:accept X-Filter -ID:EX5BVjFpneJeBchSMxfU5sGO2s1tBb9jz5hUPxV9beEXv9krsgRhBn0ayn6qsU = c7BhTjUfSYCP0C 5wUMIAUTyTj + Ouslq06 / 9 + FJoQ8Ic94JRiSFlfpo3YNCL6gbYMZBRNZBKQKXP9KnqRgOefBfIV =9升2lYoidX0QKN0Rp6WDa9d16bf9psWBhnIw8QWTHqFnHl2TRtBFGxCwNLr / WIXTmbEBG2scSEjB4 =抄送a3THUkvgvpnunQOsgZPpnjjHtFttCMKZw1tYrnr7rJHHUpEAaqg2HAG29ucnwP0M3OWpRjoXRu = WZ sGtazoE / vPfv1xaxhZcKP / NtHQBPU7HC7F6FoNMf / xm4p5WihZYpa + IEJ / oLosXQp02uZERM5C = VD wD65x9537 / 7rZ2H1fP4wGxurimbdDoD390nzzgliUnnZRwVrO92GYpEzQCukKCDE / yxRejx + ZE = H8 3D3uT7G9vVxSoPhN0btnfLiBKgWuU86DjRdgvyO / J5YD0SxG9XoKqN48Eo6Fw5tldaeQTD7oX0 =磅JnLsGIY0M3GhDMueGnYVlMD3E51XI8hjMr6 / q + SExB92w4mbOkVfblh5aYJR7 + ZK4eH0 + hutvA = HU mPi9Ge + 4V1D7jZKXiOlXLdJnBjsNteQCLP3pDMhin + HwPDUna1dtsRiS8klEbdK8qT / SBsZL7w = 6Z GAl0Dy9kR6GQ / UE9MSfeA9Gi3qOpaZFBK5mqSVIqVdOvR + fzwQc + c1DSL3 / EWMOJs7x5Cx3mJJ = WS y6OTbpWyPpCPz0F + cvcALIL N + OGG / Nnx0s / oQjWWHy7qx4hhxCQ7BHN3temh9QhbVJQq9MPSrP = 7L一个/ OGl9nRw / R + 05rAIG9Wqbj6q4DLIKylFpcDvqmbspbAlPF70Oi5kWG5WrRZ6uKzfnujlezufX = KT FyKo6u83H3djlBrH / 4eh02WHu233Y80OmAux3oN13 + ztUznegQV1FFKCsIxMyfJ8 + 4W3u3gmpA = NV 1DYnwFyGOs036orcJFgN1Xap1jpFsMB9RjtbAFxvtxLn + 3ki2FSeoRDxSTwJWw42swm4bO6gac = PM pzImK0dZ8eeB0y9de0 / NHY / Z5iLUIRMUDzeIb3XIhs66JoTzHLoQrA7fQYs8PbcVGX4Rd96fmx = 9N Rd596h9beN4f2wGT8e5STWXPcr9VvNyyyTnK4eETwT5QBDJUDAGkO208F / IJRA8ZF8C5AnJBxS = FD JKA40LeuppcmthdVdcHKN13hYPpnTUitS3rpEbXqWxAzJMHFAVLVMimS1MiSrJJu X-报告滥用-To:spam@quarantine1.mailspamprotection.com X-antispameurope-sender:tyxanhos@ukm17.siteground.biz X-antispameurope-recipient:it.support@nh-group.co.uk X-antispameurope-MSGID:3ed646545a31a61c9d2f470be8b360b1-3afa340f6fdab52b50 = c154a5acbc7065 X-antispameurope-Mailarchiv:电子邮件由www.antispameurope.com存档:= it.support@nh-group.co.uk X-antispameurope-Mailarchivtype:入站X-antispameurope-Virusscan:CLEAN X-antispameurope-disclaimer :这封电子邮件是s由www.antispameurope。= com封装mx-gate68-hz2上的电子邮件服务03396F0021B X-antispameurope-date:1509562979 X-antispameurope:INCOMING:X-antispameurope-Connect:delivery.mailspamprotection.com [184.154.208.35],T = LS = 3D1; EMIG = 3D0 X-antispameurope-detected-infomail:是X-antispameurope-WC:10:602:1:10043:0:145:0:0:0:0:0:0:7:2 :1:2:3:118:122:143 =:0:1:0:0:0:99:0:0:0:4:0:0:0:0:0:0:1:0: 0:0:0:1 X-antispameurope-Spamstatus:CLEAN X-antispameurope-REASON:得分:-6.1 = 09 * 0.0 HTML_MESSAGE BODY:消息中包含的HTML = 09 * 0.1 MIME_HTML_ONLY BODY:消息只有text / html MIME部分= 09 * -4.3 BAYES_00 BODY:贝叶斯垃圾邮件概率为0到1%= 09 * [得分:0.0005] = 09 * -1.9 ASE_FP_2009_01 ASE 2009 negativer得分返回路径:tyxanhos@ukm17.siteground.biz X-MS-Exchange -Organization-AuthSource:NHS-EX01.nhs.local X-MS-Exchange-Organization-AuthAs:Anonymous

第0部分......编码:7比特用于多部分/替代

第1部分...编码:文本/普通的7位

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

主题:测试
发信人:埃文斯将 标题:tyxan.com
测试
-
埃文斯会 m:+ xxxxxxxx
w:网址

正如你所看到的,第二封电子邮件$ body给了我一些我可以使用的正常格式化文本。第一个给了我......好吧,有些东西!

任何想法都非常感激。

0 个答案:

没有答案