某些电子邮件无法访问 PHP IMAP 电子邮件附件

标签 php imap email-attachments

我正在尝试使用 imap 从电子邮件中获取电子邮件附件,

该脚本适用于大多数电子邮件,但一个发件人附件似乎有问题,无法检索 pdf 附件。

我的代码在这里

    $hostname = '{mail.testserver.com:993/tls}INBOX';
    $username = 'test@testserver.nl';
    $password = 's1M@F1225224';



    /* try to connect */
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Email Server: ' . imap_last_error());
    /* grab emails */
    $emails = imap_search($inbox,'FROM "info@test.com"');

     count($emails);


    /* if emails are returned, cycle through each... */
    if($emails) {

        /* begin output var */
        $output = '';
        $io=0;
        /* put the newest emails on top */
        rsort($emails);
        /* for every email... */
        foreach($emails as $email_number){
            echo $io++;
            echo '<br/>';
            $int='';
            /* get information specific to this email */
            $overview = imap_fetch_overview($inbox,$email_number,0);
            $message = imap_fetchbody($inbox,$email_number,1);


            $headerx = imap_headerinfo($inbox,$email_number,0); 
// get first mails header


         $structure = imap_fetchstructure($inbox, $email_number);


            //var_dump( $structure );
            $attachments = array();

            /* if any attachments found... */
            if(isset($structure->parts) && count($structure->parts)) 
            {

                for($i = 0; $i < count($structure->parts); $i++) 
                {
                    echo 'dump of mail structure <pre>';
                    var_dump($structure->parts[$i]);
                    echo '<pre>';

                    $attachments[$i] = array(
                        'is_attachment' => false,
                        'filename' => '',
                        'name' => '',
                        'attachment' => ''
                    );



                    if($structure->parts[$i]->ifdparameters) 
                    {
                        //dparameters
                        foreach($structure->parts[$i]->ifdparameters as $object) 
                        {
                            echo $object->attribute;
                            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']);
                        }
                    }
                }
            }

                echo '<pre>';
                    var_dump($attachments);
                    echo '<pre>';

                break;


                }

结构部分的var dump $structure->parts[$i]

    object(stdClass)#36 (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)#78 (2) {
      ["attribute"]=>
      string(8) "boundary"
      ["value"]=>
      string(51) "--boundary_278_245b5f5d-6ca6-49f9-adc5-9723088add21"
    }
  }
  ["parts"]=>
  array(2) {
    [0]=>
    object(stdClass)#74 (12) {
      ["type"]=>
      int(0)
      ["encoding"]=>
      int(4)
      ["ifsubtype"]=>
      int(1)
      ["subtype"]=>
      string(5) "PLAIN"
      ["ifdescription"]=>
      int(0)
      ["ifid"]=>
      int(0)
      ["lines"]=>
      int(14)
      ["bytes"]=>
      int(891)
      ["ifdisposition"]=>
      int(0)
      ["ifdparameters"]=>
      int(0)
      ["ifparameters"]=>
      int(1)
      ["parameters"]=>
      array(1) {
        [0]=>
        object(stdClass)#32 (2) {
          ["attribute"]=>
          string(7) "charset"
          ["value"]=>
          string(5) "utf-8"
        }
      }
    }
    [1]=>
    object(stdClass)#49 (11) {
      ["type"]=>
      int(1)
      ["encoding"]=>
      int(0)
      ["ifsubtype"]=>
      int(1)
      ["subtype"]=>
      string(7) "RELATED"
      ["ifdescription"]=>
      int(0)
      ["ifid"]=>
      int(0)
      ["ifdisposition"]=>
      int(0)
      ["ifdparameters"]=>
      int(0)
      ["ifparameters"]=>
      int(1)
      ["parameters"]=>
      array(2) {
        [0]=>
        object(stdClass)#38 (2) {
          ["attribute"]=>
          string(4) "type"
          ["value"]=>
          string(9) "text/html"
        }
        [1]=>
        object(stdClass)#67 (2) {
          ["attribute"]=>
          string(8) "boundary"
          ["value"]=>
          string(51) "--boundary_279_c12a0e86-9df1-42b4-a007-c75d5904de9f"
        }
      }
      ["parts"]=>
      array(2) {
        [0]=>
        object(stdClass)#58 (12) {
          ["type"]=>
          int(0)
          ["encoding"]=>
          int(4)
          ["ifsubtype"]=>
          int(1)
          ["subtype"]=>
          string(4) "HTML"
          ["ifdescription"]=>
          int(0)
          ["ifid"]=>
          int(0)
          ["lines"]=>
          int(43)
          ["bytes"]=>
          int(3263)
          ["ifdisposition"]=>
          int(0)
          ["ifdparameters"]=>
          int(0)
          ["ifparameters"]=>
          int(1)
          ["parameters"]=>
          array(1) {
            [0]=>
            object(stdClass)#25 (2) {
              ["attribute"]=>
              string(7) "charset"
              ["value"]=>
              string(5) "utf-8"
            }
          }
        }
        [1]=>
        object(stdClass)#69 (12) {
          ["type"]=>
          int(5)
          ["encoding"]=>
          int(3)
          ["ifsubtype"]=>
          int(1)
          ["subtype"]=>
          string(3) "PNG"
          ["ifdescription"]=>
          int(0)
          ["ifid"]=>
          int(1)
          ["id"]=>
          string(15) ""
          ["bytes"]=>
          int(16262)
          ["ifdisposition"]=>
          int(0)
          ["ifdparameters"]=>
          int(0)
          ["ifparameters"]=>
          int(0)
          ["parameters"]=>
          object(stdClass)#62 (0) {
          }
        }
      }
    }
  }
}

object(stdClass)#66 (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)#22 (2) {
      ["attribute"]=>
      string(8) "boundary"
      ["value"]=>
      string(51) "--boundary_281_2ef5384d-4be2-4276-b5be-0f73bcc0811d"
    }
  }
  ["parts"]=>
  array(2) {
    [0]=>
    object(stdClass)#70 (13) {
      ["type"]=>
      int(3)
      ["encoding"]=>
      int(3)
      ["ifsubtype"]=>
      int(1)
      ["subtype"]=>
      string(3) "PDF"
      ["ifdescription"]=>
      int(0)
      ["ifid"]=>
      int(1)
      ["id"]=>
      string(6) ""
      ["bytes"]=>
      int(277524)
      ["ifdisposition"]=>
      int(1)
      ["disposition"]=>
      string(10) "attachment"
      ["ifdparameters"]=>
      int(0)
      ["ifparameters"]=>
      int(1)
      ["parameters"]=>
      array(1) {
        [0]=>
        object(stdClass)#80 (2) {
          ["attribute"]=>
          string(4) "name"
          ["value"]=>
          string(15) "inv18701288.PDF"
        }
      }
    }
    [1]=>
    object(stdClass)#63 (13) {
      ["type"]=>
      int(3)
      ["encoding"]=>
      int(3)
      ["ifsubtype"]=>
      int(1)
      ["subtype"]=>
      string(12) "OCTET-STREAM"
      ["ifdescription"]=>
      int(0)
      ["ifid"]=>
      int(1)
      ["id"]=>
      string(6) ""
      ["bytes"]=>
      int(77098)
      ["ifdisposition"]=>
      int(1)
      ["disposition"]=>
      string(10) "attachment"
      ["ifdparameters"]=>
      int(0)
      ["ifparameters"]=>
      int(1)
      ["parameters"]=>
      array(1) {
        [0]=>
        object(stdClass)#55 (2) {
          ["attribute"]=>
          string(4) "name"
          ["value"]=>
          string(33) "Algemene leveringsvoorwaarden.pdf"
        }
      }
    }
  }
}

附件数组的var转储

array(2) {
  [0]=>
  array(4) {
    ["is_attachment"]=>
    bool(false)
    ["filename"]=>
    string(0) ""
    ["name"]=>
    string(0) ""
    ["attachment"]=>
    string(0) ""
  }
  [1]=>
  array(4) {
    ["is_attachment"]=>
    bool(false)
    ["filename"]=>
    string(0) ""
    ["name"]=>
    string(0) ""
    ["attachment"]=>
    string(0) ""
  }
}

有人知道这个邮件内容或脚本有什么问题吗,有没有办法获取附件,非常感谢

我认为附件对象结构与其他邮件不同,但我不确定如何获取它

最佳答案

检查您尝试附加的文件的pdf附件的大小,它可能超过了php.ini允许的限制

关于某些电子邮件无法访问 PHP IMAP 电子邮件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49205973/

相关文章:

Python IMAP : Cannot mark email as seen

android - 使用 MODE_WORLD_READABLE 创建的文件未通过电子邮件作为附件发送

php - PHP开发的版本控制

java - 用 Java 编写 imap 邮件服务器的库

php - 即(HTTPS): generating pdf from php file doesn't work

php - 如何使用 php imap 获取电子邮件的每个主题?

python-2.7 - 属性错误: list object has not attribute lstrip in sending an email with attachment

php - 如何使用 mailgun 批量发送带附件的邮件?

javascript - onclick 事件不起作用

php - 允许用户在搜索类型之间进行选择的内部连接不起作用