通过imaps下载嵌套的电子邮件附件

时间:2019-01-24 22:05:53

标签: php imap email-attachments

在意大利存在PEC邮件。 经典的PEC邮件具有以下结构

stdClass::__set_state(array(
   'type' => 1,
   'encoding' => 0,
   'ifsubtype' => 1,
   'subtype' => 'SIGNED',
   'ifdescription' => 0,
   'ifid' => 0,
   'ifdisposition' => 0,
   'ifdparameters' => 0,
   'ifparameters' => 1,
   'parameters' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'attribute' => 'protocol',
       'value' => 'application/pkcs7-signature',
    )),
    1 => 
    stdClass::__set_state(array(
       'attribute' => 'micalg',
       'value' => 'sha-256',
    )),
    2 => 
    stdClass::__set_state(array(
       'attribute' => 'boundary',
       'value' => '----3DFBCC4636D92DFF96EEC8A832D82F7E',
    )),
  ),
   'parts' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'type' => 1,
       'encoding' => 0,
       'ifsubtype' => 1,
       'subtype' => 'MIXED',
       'ifdescription' => 0,
       'ifid' => 0,
       'ifdisposition' => 0,
       'ifdparameters' => 0,
       'ifparameters' => 1,
       'parameters' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'attribute' => 'boundary',
           'value' => '----------=_1548319948-18043-42',
        )),
      ),
       'parts' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'type' => 1,
           'encoding' => 0,
           'ifsubtype' => 1,
           'subtype' => 'ALTERNATIVE',
           'ifdescription' => 0,
           'ifid' => 0,
           'ifdisposition' => 0,
           'ifdparameters' => 0,
           'ifparameters' => 1,
           'parameters' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'attribute' => 'boundary',
               'value' => '----------=_1548319948-18043-43',
            )),
          ),
           'parts' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'type' => 0,
               'encoding' => 4,
               'ifsubtype' => 1,
               'subtype' => 'PLAIN',
               'ifdescription' => 0,
               'ifid' => 0,
               'lines' => 13,
               'bytes' => 642,
               'ifdisposition' => 1,
               'disposition' => 'inline',
               'ifdparameters' => 0,
               'ifparameters' => 1,
               'parameters' => 
              array (
                0 => 
                stdClass::__set_state(array(
                   'attribute' => 'charset',
                   'value' => 'iso-8859-1',
                )),
              ),
            )),
            1 => 
            stdClass::__set_state(array(
               'type' => 0,
               'encoding' => 4,
               'ifsubtype' => 1,
               'subtype' => 'HTML',
               'ifdescription' => 0,
               'ifid' => 0,
               'lines' => 16,
               'bytes' => 498,
               'ifdisposition' => 1,
               'disposition' => 'inline',
               'ifdparameters' => 0,
               'ifparameters' => 1,
               'parameters' => 
              array (
                0 => 
                stdClass::__set_state(array(
                   'attribute' => 'charset',
                   'value' => 'iso-8859-1',
                )),
              ),
            )),
          ),
        )),
        1 => 
        stdClass::__set_state(array(
           'type' => 3,
           'encoding' => 3,
           'ifsubtype' => 1,
           'subtype' => 'XML',
           'ifdescription' => 0,
           'ifid' => 0,
           'bytes' => 1060,
           'ifdisposition' => 1,
           'disposition' => 'inline',
           'ifdparameters' => 1,
           'dparameters' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'attribute' => 'filename',
               'value' => 'daticert.xml',
            )),
          ),
           'ifparameters' => 1,
           'parameters' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'attribute' => 'name',
               'value' => 'daticert.xml',
            )),
          ),
        )),
        2 => 
        stdClass::__set_state(array(
           'type' => 2,
           'encoding' => 0,
           'ifsubtype' => 1,
           'subtype' => 'RFC822',
           'ifdescription' => 0,
           'ifid' => 0,
           'lines' => 281,
           'bytes' => 19248,
           'ifdisposition' => 1,
           'disposition' => 'inline',
           'ifdparameters' => 1,
           'dparameters' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'attribute' => 'filename',
               'value' => 'postacert.eml',
            )),
          ),
           'ifparameters' => 1,
           'parameters' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'attribute' => 'name',
               'value' => 'postacert.eml',
            )),
          ),
           'parts' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'type' => 1,
               'encoding' => 0,
               'ifsubtype' => 1,
               'subtype' => 'MIXED',
               'ifdescription' => 0,
               'ifid' => 0,
               'ifdisposition' => 0,
               'ifdparameters' => 0,
               'ifparameters' => 1,
               'parameters' => 
              array (
                0 => 
                stdClass::__set_state(array(
                   'attribute' => 'boundary',
                   'value' => '----=_Part_110291_1199576852.1548319948322',
                )),
              ),
               'parts' => 
              array (
                0 => 
                stdClass::__set_state(array(
                   'type' => 3,
                   'encoding' => 0,
                   'ifsubtype' => 1,
                   'subtype' => 'OCTET-STREAM',
                   'ifdescription' => 0,
                   'ifid' => 0,
                   'bytes' => 597,
                   'ifdisposition' => 1,
                   'disposition' => 'attachment',
                   'ifdparameters' => 1,
                   'dparameters' => 
                  array (
                    0 => 
                    stdClass::__set_state(array(
                       'attribute' => 'filename',
                       'value' => 'IT04599340967_03NR1_MT_001.xml',
                    )),
                  ),
                   'ifparameters' => 1,
                   'parameters' => 
                  array (
                    0 => 
                    stdClass::__set_state(array(
                       'attribute' => 'name',
                       'value' => 'IT04599340967_03NR1_MT_001.xml',
                    )),
                  ),
                )),
                1 => 
                stdClass::__set_state(array(
                   'type' => 3,
                   'encoding' => 3,
                   'ifsubtype' => 1,
                   'subtype' => 'OCTET-STREAM',
                   'ifdescription' => 0,
                   'ifid' => 0,
                   'bytes' => 15118,
                   'ifdisposition' => 1,
                   'disposition' => 'attachment',
                   'ifdparameters' => 1,
                   'dparameters' => 
                  array (
                    0 => 
                    stdClass::__set_state(array(
                       'attribute' => 'filename',
                       'value' => 'IT04599340967_03NR1.xml.p7m',
                    )),
                  ),
                   'ifparameters' => 1,
                   'parameters' => 
                  array (
                    0 => 
                    stdClass::__set_state(array(
                       'attribute' => 'name',
                       'value' => 'IT04599340967_03NR1.xml.p7m',
                    )),
                  ),
                )),
                2 => 
                stdClass::__set_state(array(
                   'type' => 0,
                   'encoding' => 4,
                   'ifsubtype' => 1,
                   'subtype' => 'PLAIN',
                   'ifdescription' => 0,
                   'ifid' => 0,
                   'lines' => 46,
                   'bytes' => 2286,
                   'ifdisposition' => 0,
                   'ifdparameters' => 0,
                   'ifparameters' => 1,
                   'parameters' => 
                  array (
                    0 => 
                    stdClass::__set_state(array(
                       'attribute' => 'charset',
                       'value' => 'UTF-8',
                    )),
                  ),
                )),
              ),
            )),
          ),
        )),
      ),
    )),
    1 => 
    stdClass::__set_state(array(
       'type' => 3,
       'encoding' => 3,
       'ifsubtype' => 1,
       'subtype' => 'PKCS7-SIGNATURE',
       'ifdescription' => 0,
       'ifid' => 0,
       'bytes' => 9316,
       'ifdisposition' => 1,
       'disposition' => 'attachment',
       'ifdparameters' => 1,
       'dparameters' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'attribute' => 'filename',
           'value' => 'smime.p7s',
        )),
      ),
       'ifparameters' => 1,
       'parameters' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'attribute' => 'name',
           'value' => 'smime.p7s',
        )),
      ),
    )),
  ),
))

我们可以看到我们有三个嵌套部分。 从这个答案开始 https://stackoverflow.com/a/11546986/10850224

为此,我要构建一个递归函数

function getAttachment($structure,$livel){
    global $attachments;
    global $email_number;
    global $inbox;
    global $index;
    if(isset($structure->parts) && count($structure->parts)) {
        for($i = 0; $i < count($structure->parts); $i++) {
            if(isset($structure->parts[$i]->parts)){
                $livel++;
                getAttachment($structure->parts[$i],$livel);

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

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

                        $attachments[$i]['is_attachment'] = true;
                        $attachments[$i] ['attachment']= $object->value;
                        echo "INDEX".$index."<br>";

                    }
                }
            }

            if($structure->parts[$i]->ifparameters) {
                foreach($structure->parts[$i]->parameters as $object) {
                    if(strtolower($object->attribute) == 'name') {
                        $attachments[$i]['is_attachment'] = true;
                        $attachments[$i] ['attachment']= $object->value;
                        echo "INDEX".$index."<br>";
                    }
                }
            }
            if($attachments[$i]['is_attachment']) {
                $str="1.3";
                $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $str);
                if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
                    $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                }
                elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
                    $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                }
            }
        } 
    } 
}

但是此功能无法获取正确的正文

获得所有附件的正确身体是什么

每个人都可以发布非迭代解决方案

0 个答案:

没有答案