PHP Imap忽略来自noreply电子邮件的附件

PHP Imap忽略来自noreply电子邮件的附件,php,email,imap,Php,Email,Imap,我的问题很简单,我使用下面的代码来阅读电子邮件和下载附件。这不是我的代码,不是我写的,是我在网上找到的一个通用imap阅读器。代码在某种程度上可以正常工作,因为我在阅读收件箱和下载附件时没有遇到任何问题,正如编写此代码时所做的那样 当我试图下载noreply电子邮件的附件时,问题就出现了。我可以看到,这段代码至少是通过对电子邮件数组执行var_dump或print_r来读取消息,但是它完全忽略了这些消息具有附件的事实,并且没有对其执行任何操作。就好像noreply邮件的附件不存在一样,但是如果我

我的问题很简单,我使用下面的代码来阅读电子邮件和下载附件。这不是我的代码,不是我写的,是我在网上找到的一个通用imap阅读器。代码在某种程度上可以正常工作,因为我在阅读收件箱和下载附件时没有遇到任何问题,正如编写此代码时所做的那样

当我试图下载noreply电子邮件的附件时,问题就出现了。我可以看到,这段代码至少是通过对电子邮件数组执行var_dump或print_r来读取消息,但是它完全忽略了这些消息具有附件的事实,并且没有对其执行任何操作。就好像noreply邮件的附件不存在一样,但是如果我登录gmail,附件就会清晰可见

有人知道为什么这段代码会忽略noreply电子邮件附件吗

如果我将noreply电子邮件转发到此地址,并且它显示为来自我的转发,它会很好地看到附件。这是一个很普通的地址,它的行为就像它的隐形地址一样,即使在gmail中将它标记为重要地址也无济于事

我尝试了许多我在网上找到的php类的迭代来读取电子邮件附件,它们都表现得好像附件不存在一样

<?
    $host     = '{imap.gmail.com:993/imap/ssl}INBOX';
    $login    = 'xxxx@gmail.com';
    $password = 'xxxxx';

    $inbox = imap_open($host, $login, $password) or die("can't connect: " . imap_last_error());

    $emails     = imap_search($inbox, 'ALL');
    /* useful only if the above search is set to 'ALL' */
    $max_emails = 16;

    /* put the newest emails on top */
    rsort($emails);

    /* if any emails found, iterate through each email */
    if ($emails) {

        $count = 1;

        /* put the newest emails on top */
        rsort($emails);

        /* for every email... */
        foreach ($emails as $email_number) {

            /* get information specific to this email */
            $overview = imap_fetch_overview($inbox, $email_number, 0);

            /* get mail message, not actually used here.
            Refer to http://php.net/manual/en/function.imap-fetchbody.php
            for details on the third parameter.
            */
            $message = imap_fetchbody($inbox, $email_number, 2);

            /* get mail structure */
            $structure = imap_fetchstructure($inbox, $email_number);

            $attachments = array();

            /* if any attachments found... */
            if (isset($structure->parts) && count($structure->parts)) {
                for ($i = 0; $i < count($structure->parts); $i++) {
                    $attachments[$i] = array(
                        'is_attachment' => false,
                        'filename' => '',
                        'name' => '',
                        'attachment' => ''
                    );

                    if ($structure->parts[$i]->ifdparameters) {
                        foreach ($structure->parts[$i]->dparameters as $object) {
                            if (strtolower($object->attribute) == 'filename') {
                                $attachments[$i]['is_attachment'] = true;
                                $attachments[$i]['filename']      = $object->value;
                            }
                        }
                    }

                    if ($structure->parts[$i]->ifparameters) {
                        foreach ($structure->parts[$i]->parameters as $object) {
                            if (strtolower($object->attribute) == 'name') {
                                $attachments[$i]['is_attachment'] = true;
                                $attachments[$i]['name']          = $object->value;
                            }
                        }
                    }

                    if ($attachments[$i]['is_attachment']) {
                        $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i + 1);

                        /* 3 = BASE64 encoding */
                        if ($structure->parts[$i]->encoding == 3) {
                            $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                        }
                        /* 4 = QUOTED-PRINTABLE encoding */
                        elseif ($structure->parts[$i]->encoding == 4) {
                            $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                        }
                    }
                }
            }

            /* iterate through each attachment and save it */
            foreach ($attachments as $attachment) {
                if ($attachment['is_attachment'] == 1) {
                    $filename = $attachment['name'];
                    if (empty($filename))
                        $filename = $attachment['filename'];

                    if (empty($filename))
                        $filename = time() . ".dat";

                    /* prefix the email number to the filename in case two emails
                     * have the attachment with the same file name.
                     */
                    $fp = fopen('./' . $email_number . "-" . $filename, "w+");
                    fwrite($fp, $attachment['attachment']);
                    fclose($fp);

                }

            }

            if ($count++ >= $max_emails)
                break;
        }

    }

    imap_close($inbox);
?>

您能同时提供原始标题吗?(使用从gmail获取原始源代码,然后剪辑二进制数据)PHP工作正常。要么你的课坏了,要么你做错了什么。@Max对我来说没有问题。你能确认它至少应该看到附件吗?标题提示多部分/混合正确吗?我还尝试使用SSilence/php imap客户端,它会为附件返回空白数组。这仅仅意味着没有真正的附件吗?我担心边界前没有空行。您的IMAP服务器可能无法解释该消息,尽管gmail通常在这方面做得很好。你能得到协议跟踪吗?@Max我怎样才能得到协议跟踪?
 Delivered-To: xxxxxx@gmail.com
 Received: by x.x.x.x with SMTP id xxxxxxxxxxxx;
    Thu, 13 Jul 2017 07:37:27 -0700 (PDT)
 X-Received: by x.x.xx.x with SMTP id xxxxxxxxxxxxxxxxxx;
    Thu, 13 Jul 2017 07:37:27 -0700 (PDT)
 ARC-Seal: i=1; a=rsa-sha256; t=1499956647; cv=none;
    d=google.com; s=arc-20160816;
    b=GZYuJrDpYptarZPpUW0/GAIJgDB9cs+EHlVRug/BTaaHE6jobO9/3jBqYaSmpV/wGQ
     KM6X1eVdLgy0cFetmzAxV4d2ocg9oLgpk34Q4WobbU/YqHsu0hN+Qves2+3MDUPW1ohe
     I2ZuDuZWMJEVOLaUoMBvZCr+baUyGsK/Z0RwkkIeLZSJz8KU5Fp7uP0SNOuPzu0L/lgC
     AeWqWWZwjF79YJpQX9+qkml54ARgF8wWkxsyBUy05X7uHN1xPs0/aglCL5Qnpf02Aucx
     26KmRk+2hDtAydlwCdlg8aAwANNsxYoOKmifcJxhzDsHS4bfcxqxLXNrpQqtFG9iEB9p
     U/Sw==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; 
s=arc-20160816;
    h=message-id:subject:date:to:from:mime-version
     :arc-authentication-results;
    bh=ohea4Dm6L7cRDWaoxA4kjgSY+yfMNdq2sByJoPX07W8=;
    b=bESVgQCdCXN4ZVgRcIUX+tymYO/SuSdW7EBg0sIqjcaxhpdaPj29pKM/cv5zpIz8qX
     fy09T7i3tLN+XLGpA+Blk9lR5D511Nd4ra2URGNa2mudkMkVvCQ2X5seild1XC/V3rKf
     /0WJmIfUwYMZ7hUedajiFMF3HmrD37B+pr/+1q4LLuxzdBUkuJQbRCo0WBvsN2FqxeGz
     pr7W8XKYyFRmFJtlrTZ8crANU/W91P23B+YazI8rFD66GGEZ0HGmi5e7WeUWknO02IBz
     F6jvZ0AW+gkZJPS/J+fo0I7T/BR+0xZb8Hs+/UlAa2FxK9u/wUODdG7BlA+vfjEYcBql
     KQYg==
ARC-Authentication-Results: i=1; mx.google.com;
   spf=neutral (google.com: x is neither permitted nor denied 
by best guess record for domain of noreply@my.hostednumbers.com) 
smtp.mailfrom=noreply@my.hostednumbers.com
Return-Path: <noreply@my.hostednumbers.com>
Received: from mail1.patlive.local (mail1.patlive.com. [x])
    by mx.google.com with ESMTPS id r10si1078748vkf.224.2017.07.13.07.37.22
    for <xxxxxxxxxxxxxx@gmail.com>
    (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128);
    Thu, 13 Jul 2017 07:37:27 -0700 (PDT)
Received-SPF: neutral (google.com: x is neither permitted nor 
denied by best guess record for domain of noreply@my.hostednumbers.com) 
client-ip=xxxxxxx;
Authentication-Results: mx.google.com;
   spf=neutral (google.com: xxxxx is neither permitted nor denied 
 by best guess record for domain of noreply@my.hostednumbers.com) 
 smtp.mailfrom=noreply@my.hostednumbers.com
 Received: from MTAS2 (1xxxx3) by mail1.patlive.com (1xxxxx1) with 
 Microsoft SMTP Server id 8.3.485.1; Thu, 13 Jul 2017 10:37:21 -0400
MIME-Version: 1.0
From: "noreply@my.hostednumbers.com" <noreply@my.hostednumbers.com>
To: xxxxxxxxxx@gmail.com
Date: Thu, 13 Jul 2017 10:37:21 -0400
Subject: Your Voice Recording from xxxxxxxxxxxx
Content-Type: multipart/mixed; boundary="--boundary_529647_d1f7373e-194b-
4711-89de-c6cf1f12d209"
Message-ID: <1d5fabae-6801-4146-92bc-782e4338e584@MAIL1.patlive.local>
Return-Path: noreply@my.hostednumbers.com

----boundary_529647_d1f7373e-194b-4711-89de-c6cf1f12d209
Content-Type: multipart/alternative; boundary="--boundary_529646_c76337af-e5d5-4ccd-a09a-47bde8e77149"

----boundary_529646_c76337af-e5d5-4ccd-a09a-47bde8e77149
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

From:  xxxxxxxx
To:  xxxxxxxxx
Forwarded To:  xxxxxxxxxxx
Date:  Thursday, July 13, 2017 10:37 AM
Recording URL:  xxxxxxxxxxx
----boundary_529646_c76337af-e5d5-4ccd-a09a-47bde8e77149
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable

<html>
<head>
=09<title>Email</title>
=09</head>

<style type=3D"text/css">
=09body
=09{
=09=09margin: 0px; padding: 0px; text-align: center; color: #585858; font-f=
amily: Arial, Tahoma, Verdana, Helvetica, sans-serif; font-weight: normal; =
font-size: x-small; background:#FFFFFF;
=09}
=09p, b
=09{
=09=09color: #585858;
=09}
=09a:link, a:visited
=09{=09
=09=09color: #3399cc;
=09}
=09a:hover
=09{
=09=09color: #666666;
=09}
=09#bodyText
=09{
=09=09margin: 0px 10px 0px 10px; padding: 10px 10px 10px 10px; width: auto;=
 text-align:left; font-size: 10pt;
=09}
</style>

<body>
=09<div id=3D"bodyText">
=09=09<br><b>From:</b>  xxxxxx
=09=09<br><b>To:</b>  xxxxxxxx
=09=09<br><b>Forwarded To:</b>  xxxxxxxxxx
=09=09<br><b>Date:</b>  Thursday, July 13, 2017 10:37 AM
=09=09<br><b>Recording URL:</b>  <a href="xxxxxx"</a>
=09=09</p>
=09</div>
=09</body>
=09</html>
=09
----boundary_529646_c76337af-e5d5-4ccd-a09a-47bde8e77149--
----boundary_529647_d1f7373e-194b-4711-89de-c6cf1f12d209
Content-Type: multipart/mixed; boundary="--boundary_529648_5a6e09fa-c57c-4137-894e-b56991b5674f"

----boundary_529648_5a6e09fa-c57c-4137-894e-b56991b5674f
Content-Type: audio/mp3; name="247195094.mp3"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; read-date="13 Jul 2017 10:37:17 -0400"; modification-date="13 Jul 2017 10:37:16 -0400"; creation-date="13 Jul 2017 10:37:17 -0400"


----boundary_529648_5a6e09fa-c57c-4137-894e-b56991b5674f--
----boundary_529647_d1f7373e-194b-4711-89de-c6cf1f12d209--
    ["attachments"]=> array(0) { } 
    ["section"]=> array(5) {
            [0]=> string(1) "1"
            [1]=> string(3) "1.1"
            [2]=> string(3) "1.2"
            [3]=> string(1) "2"
            [4]=> string(3) "2.1" 
        } 
    ["structure"]=> object(stdClass)#30 (11) {  
        ["type"]=> int(1)
        ["encoding"]=> int(0)
        ["ifsubtype"]=> int(1)
        ["subtype"]=> string(5) "MIXED"
        ["ifdescription"]=> int(0)
        ["ifid"]=> int(0)
        ["ifdisposition"]=> int(0)
        ["ifdparameters"]=> int(0)
        ["ifparameters"]=> int(1)
        ["parameters"]=> array(1) {
            [0]=> object(stdClass)#31 (2) { 
                ["attribute"]=> string(8) "boundary"
                ["value"]=> string(54) "--boundary_542309_d05747ba-05b7-4337-ab17-bdb9f8f827bd" 
            } 
        }
        ["parts"]=> array(2) {
            [0]=> object(stdClass)#32 (11) { 
                ["type"]=> int(1)
                ["encoding"]=> int(0)
                ["ifsubtype"]=> int(1)
                ["subtype"]=> string(11) "ALTERNATIVE"
                ["ifdescription"]=> int(0)
                ["ifid"]=> int(0)
                ["ifdisposition"]=> int(0)
                ["ifdparameters"]=> int(0)
                ["ifparameters"]=> int(1)
                ["parameters"]=> array(1) { 
                    [0]=> object(stdClass)#33 (2) { 
                        ["attribute"]=> string(8) "boundary" 
                        ["value"]=> string(54) "--boundary_542308_68f3d65d-9307-4b78-a641-5c1d89dcdd21" 
                    } 
                } 
                ["parts"]=> array(2) { 
                    [0]=> object(stdClass)#34 (12) { 
                        ["type"]=> int(0) 
                        ["encoding"]=> int(4) 
                        ["ifsubtype"]=> int(1) 
                        ["subtype"]=> string(5) "PLAIN" 
                        ["ifdescription"]=> int(0) 
                        ["ifid"]=> int(0) 
                        ["lines"]=> int(2) 
                        ["bytes"]=> int(251) 
                        ["ifdisposition"]=> int(0) 
                        ["ifdparameters"]=> int(0) 
                        ["ifparameters"]=> int(1) 
                        ["parameters"]=> array(1) { 
                            [0]=> object(stdClass)#35 (2) { 
                                ["attribute"]=> string(7) "charset" 
                                ["value"]=> string(8) "us-ascii" 
                            } 
                        } 
                    } 
                    [1]=> object(stdClass)#36 (12) { 
                        ["type"]=> int(0) 
                        ["encoding"]=> int(4) 
                        ["ifsubtype"]=> int(1) 
                        ["subtype"]=> string(4) "HTML" 
                        ["ifdescription"]=> int(0) 
                        ["ifid"]=> int(0) 
                        ["lines"]=> int(13) 
                        ["bytes"]=> int(1198) 
                        ["ifdisposition"]=> int(0) 
                        ["ifdparameters"]=> int(0) 
                        ["ifparameters"]=> int(1) 
                        ["parameters"]=> array(1) { 
                            [0]=> object(stdClass)#37 (2) { 
                                ["attribute"]=> string(7) "charset"
                                ["value"]=> string(8) "us-ascii" 
                            } 
                        } 
                    } 
                } 
            } 
            [1]=> object(stdClass)#38 (11) { 
                ["type"]=> int(1) 
                ["encoding"]=> int(0) 
                ["ifsubtype"]=> int(1) 
                ["subtype"]=> string(5) "MIXED" 
                ["ifdescription"]=> int(0) 
                ["ifid"]=> int(0) 
                ["ifdisposition"]=> int(0) 
                ["ifdparameters"]=> int(0) 
                ["ifparameters"]=> int(1) 
                ["parameters"]=> array(1) { 
                    [0]=> object(stdClass)#39 (2) { 
                        ["attribute"]=> string(8) "boundary" 
                        ["value"]=> string(54) "--boundary_542310_9e05358b-ebf4-4c63-b75d-e846238f69aa" 
                    }
                } 
                ["parts"]=> array(1) { 
                    [0]=> object(stdClass)#40 (13) { 
                        ["type"]=> int(4)
                        ["encoding"]=> int(3) 
                        ["ifsubtype"]=> int(1) 
                        ["subtype"]=> string(3) "MP3" 
                        ["ifdescription"]=> int(0) 
                        ["ifid"]=> int(0) 
                        ["bytes"]=> int(8013576) 
                        ["ifdisposition"]=> int(1) 
                        ["disposition"]=> string(10) "attachment" 
                        ["ifdparameters"]=> int(1) 
                        ["dparameters"]=> array(3) { 
                            [0]=> object(stdClass)#41 (2) { 
                                ["attribute"]=> string(9) "read-date" 
                                ["value"]=> string(26) "14 Jul 2017 11:21:11 -0400" 
                            } 
                            [1]=> object(stdClass)#42 (2) { 
                                ["attribute"]=> string(17) "modification-date" 
                                ["value"]=> string(26) "14 Jul 2017 11:21:11 -0400" 
                            }
                            [2]=> object(stdClass)#43 (2) { 
                                ["attribute"]=> string(13) "creation-date" 
                                ["value"]=> string(26) "14 Jul 2017 11:21:11 -0400" 
                            } 
                        } 
                        ["ifparameters"]=> int(1) 
                        ["parameters"]=> array(1) { 
                            [0]=> object(stdClass)#44 (2) { 
                                ["attribute"]=> string(4) "name" 
                                ["value"]=> string(13) "247206024.mp3" 
                            } 
                        }
                    }
                }
            }
        }
    }