2016-12-12 18 views
0

電子メールアカウントに新しいメッセージがないかどうかチェックし、各電子メールから.zipと.pdf添付ファイルをダウンロードしようとします。電子メールから添付ファイルを抽出する - 添付ファイルのファイル名を取得できない

[1] => stdClass Object 
     (
      [type] => 3 
      [encoding] => 3 
      [ifsubtype] => 1 
      [subtype] => PDF 
      [ifdescription] => 0 
      [ifid] => 0 
      [bytes] => 132780 
      [ifdisposition] => 1 
      [disposition] => attachment 
      [ifdparameters] => 1 
      [dparameters] => Array 
       (
        [0] => stdClass Object 
         (
          [attribute] => filename 
          [value] => some_filename.pdf 
         ) 

       ) 

      [ifparameters] => 1 
      [parameters] => Array 
       (
        [0] => stdClass Object 
         (
          [attribute] => name 
          [value] => some_filename.pdf 
         ) 

       ) 

     ) 

    [2] => stdClass Object 
     (
      [type] => 3 
      [encoding] => 3 
      [ifsubtype] => 1 
      [subtype] => ZIP 
      [ifdescription] => 0 
      [ifid] => 0 
      [bytes] => 43170 
      [ifdisposition] => 1 
      [disposition] => attachment 
      [ifdparameters] => 1 
      [dparameters] => Array 
       (
        [0] => stdClass Object 
         (
          [attribute] => filename 
          [value] => another_filename.zip 
         ) 

       ) 

      [ifparameters] => 1 
      [parameters] => Array 
       (
        [0] => stdClass Object 
         (
          [attribute] => name 
          [value] => another_filename.zip 
         ) 

       ) 

     ) 

あなたはそれがそれぞれの拡張子とファイル名を把握するのは簡単です見ることができるように:$structure->partsは次のようになり、ほとんどの電子メールについては

/* try to connect */ 
$inbox = imap_open($hostname, $username, $password) or die ('Cannot connect to domain:' . imap_last_error()); 

/* grab emails */ 
$emails = imap_search($inbox, 'ALL'); 

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

foreach ($emails as $email_number) { 
    $overview = imap_fetch_overview($inbox, $email_number, 0); 
    if ($overview [0]->seen) { 
     continue; 
    } 

    $structure = imap_fetchstructure($inbox, $email_number); 
    if (!property_exists($structure, 'parts')) { 
     continue; 
    } 
    //print_r($structure->parts); 
    //get attachments 
} 

:私はこれを行うには、次のコードを使用しています添付ファイル。

[1] => stdClass Object 
     (
      [type] => 3 
      [encoding] => 3 
      [ifsubtype] => 1 
      [subtype] => OCTET-STREAM 
      [ifdescription] => 1 
      [description] => =?utf-8?B?Q1RHIFF1ZXLDqXRhcm8gIC0gIC0gMTJfOF8xNi5wZGY=?= 
      [ifid] => 1 
      [id] => <[email protected]> 
      [bytes] => 44592 
      [ifdisposition] => 1 
      [disposition] => attachment 
      [ifdparameters] => 1 
      [dparameters] => Array 
       (
        [0] => stdClass Object 
         (
          [attribute] => filename 
          [value] => =?utf-8?B?Q1RHIFF1ZXLDqXRhcm8gIC0gIC0gMTJfOF8xNi5wZGY=?= 
         ) 

        [1] => stdClass Object 
         (
          [attribute] => size 
          [value] => 32586 
         ) 

        [2] => stdClass Object 
         (
          [attribute] => creation-date 
          [value] => Thu, 08 Dec 2016 22:16:31 GMT 
         ) 

        [3] => stdClass Object 
         (
          [attribute] => modification-date 
          [value] => Thu, 08 Dec 2016 22:16:31 GMT 
         ) 

       ) 

      [ifparameters] => 1 
      [parameters] => Array 
       (
        [0] => stdClass Object 
         (
          [attribute] => name 
          [value] => =?utf-8?B?Q1RHIFF1ZXLDqXRhcm8gIC0gIC0gMTJfOF8xNi5wZGY=?= 
         ) 

       ) 

     ) 

    [2] => stdClass Object 
     (
      [type] => 3 
      [encoding] => 3 
      [ifsubtype] => 1 
      [subtype] => OCTET-STREAM 
      [ifdescription] => 1 
      [description] => =?utf-8?B?Q1RHIFF1ZXLDqXRhcm8gIC0gIC0gMTJfOF8xNi56aXA=?= 
      [ifid] => 1 
      [id] => <[email protected]> 
      [bytes] => 10966 
      [ifdisposition] => 1 
      [disposition] => attachment 
      [ifdparameters] => 1 
      [dparameters] => Array 
       (
        [0] => stdClass Object 
         (
          [attribute] => filename 
          [value] => =?utf-8?B?Q1RHIFF1ZXLDqXRhcm8gIC0gIC0gMTJfOF8xNi56aXA=?= 
         ) 

        [1] => stdClass Object 
         (
          [attribute] => size 
          [value] => 8011 
         ) 

        [2] => stdClass Object 
         (
          [attribute] => creation-date 
          [value] => Thu, 08 Dec 2016 22:16:31 GMT 
         ) 

        [3] => stdClass Object 
         (
          [attribute] => modification-date 
          [value] => Thu, 08 Dec 2016 22:16:31 GMT 
         ) 

       ) 

      [ifparameters] => 1 
      [parameters] => Array 
       (
        [0] => stdClass Object 
         (
          [attribute] => name 
          [value] => =?utf-8?B?Q1RHIFF1ZXLDqXRhcm8gIC0gIC0gMTJfOF8xNi56aXA=?= 
         ) 

       ) 

     ) 

これらの添付ファイルを再度PDFやZIPであり、電子メールクライアントを使用しているとき、彼らは内の添付ファイルと同じように見える:しかし、最近私は$構造 - >の部分ではなく、このようになりますいくつかの電子メールを取得してきました他の電子メール。しかし、上記のように、ファイル名にblahblah.zipとblahblah.pdfの代わりに、 "=?utf-8?B?Q1RHIFF1ZXLDqXRhcm8gIC0gIC0gMTJfOF8xNi56aXA =?="のように表示されます。また、両方のサブタイプは 'zip'または 'pdf'ではなく 'OCTET-STREAM'です。だから、私はどの種類の添付ファイルが何であるか分からず、電子メールでは何もできません。

ご協力いただければ幸いです。要約すると、私はちょうど異なって行動している電子メールのこの特定のセグメントから添付ファイル情報を適切に抽出する方法を見つけようとしています。

答えて

3

これらはMIMEエンコードされたファイル名です。

=?utf-8?B? 

これは、UTF-8、Base64エンコードの文字列であることを意味します。

チェックアウトiconv_mime_decode

+0

ありがとう@HawkHogan。私の日を救った。 '$ resp = iconv_mime_decode($ string_encoded、ICONV_MIME_DECODE_CONTINUE_ON_ERROR、" ISO-8859-15 ");' 'エコー$ resp = utf8_encode($ resp);' – Duque

関連する問題