Загрузить вложенное вложение электронной почты через imaps - PullRequest
0 голосов
/ 25 января 2019

В Италии существует почта УИК.Классическое почтовое сообщение имеет следующую структуру:

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']);
                }
            }
        } 
    } 
}

Но эта функция не получает правильное тело

Что такоеправильное тело для получения всех вложений

Каждый может также публиковать не итерационные решения

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...