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

时间:2017-07-13 14:56:29

标签: php email imap

我的问题很简单,我使用以下代码阅读电子邮件并下载附件。这不是我的代码,我没有写它,它是我在网上发现的通用imap阅读器。代码有点像它应该看到的那样,因为我在编写代码时可以阅读我的收件箱并下载附件。

当我尝试下载无法发送电子邮件的附件时问题就出现了。我可以看到这段代码至少通过执行电子邮件数组的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);
?>

php imap是否会忽略noreply电子邮件?我可以对此做些什么吗?任何建议和/或帮助表示赞赏。

这是gmail上“原始邮件”的副本

 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--
我不是疯了吗?内容类型是音频mp3,实际上有一个附件,对吗?

这是gmail的截图,看起来像是对我的附件....?gmail

    ["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" 
                            } 
                        }
                    }
                }
            }
        }
    }

0 个答案:

没有答案