За последние 24 часа нас посетили 21683 программиста и 1060 роботов. Сейчас ищут 714 программистов ...

выгрузка вложений из почты

Тема в разделе "Сделайте за меня", создана пользователем seriousdark, 4 мар 2018.

Метки:
  1. seriousdark

    seriousdark Новичок

    С нами с:
    3 янв 2018
    Сообщения:
    3
    Симпатии:
    0
    Здравствуйте, я в пхп полный 0 поставили задачу выгружать с почты вложения, я нашел рабочий скрипт только он не выгружает файлы с русским названием.
    я понимаю что нужно где то задать параметр iconv UTF-8 а от где и как хз, убил несколько дней на изучение пхп но толку 0.
    Помогите пожалуйста

    PHP:
    1. <?php
    2. $hostname = '{imap.ukr.net:993/imap/ssl/novalidate-cert}INBOX';
    3. $username = ''; # например somebody@gmail.com
    4. $password = '';
    5. $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
    6. $emails = imap_search($inbox,'ALL');
    7. $max_emails = 16;
    8.  
    9. if($emails) {
    10.     $count = 1;
    11.      rsort($emails);
    12.      foreach($emails as $email_number)
    13.     {
    14.         /* get information specific to this email */              
    15.         $overview = imap_fetch_overview($inbox,$email_number,0);
    16.         /* get mail message */
    17.         $message = imap_fetchbody($inbox,$email_number,2);
    18.         /* get mail structure */
    19.         $structure = imap_fetchstructure($inbox, $email_number);
    20.         $attachments = array();
    21.         /* if any attachments found... */
    22.         if(isset($structure->parts) && count($structure->parts))
    23.         {
    24.             for($i = 0; $i < count($structure->parts); $i++)
    25.             {
    26.                 $attachments[$i] = array(
    27.                     'is_attachment' => false,
    28.                     'filename' => '',
    29.                     'name' => '',
    30.                     'attachment' => ''
    31.                 );
    32.                 if($structure->parts[$i]->ifdparameters)
    33.                 {
    34.                     foreach($structure->parts[$i]->dparameters as $object)
    35.                     {
    36.                         if(strtolower($object->attribute) == 'filename')
    37.                         {
    38.                             $attachments[$i]['is_attachment'] = true;
    39.                             $attachments[$i]['filename'] = $object->value;
    40.                         }
    41.                     }
    42.                 }
    43.                 if($structure->parts[$i]->ifparameters)
    44.                 {
    45.                     foreach($structure->parts[$i]->parameters as $object)
    46.                     {
    47.                         if(strtolower($object->attribute) == 'name')
    48.                         {
    49.                             $attachments[$i]['is_attachment'] = true;
    50.                             $attachments[$i]['name'] = $object->value;
    51.                         }
    52.                     }
    53.                 }
    54.  
    55.                 if($attachments[$i]['is_attachment'])
    56.                 {
    57.                     $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
    58.                
    59.                     /* 4 = QUOTED-PRINTABLE encoding */
    60.                 if($structure->parts[$i]->encoding == 3)
    61.                    
    62.                        {
    63.                         $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
    64.                     }
    65.                     /* 3 = BASE64 encoding */  
    66.                  elseif($structure->parts[$i]->encoding == 4)
    67.                     {
    68.                         $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
    69.                     }
    70.                 }
    71.                
    72.                
    73.                 }
    74.         }
    75.         /* iterate through each attachment and save it */
    76.         foreach($attachments as $attachment)
    77.         {
    78.             if($attachment['is_attachment'] == 1)
    79.             {
    80.                 $filename = $attachment['name'];
    81.                   if(empty($filename)) $filename = $attachment['filename'];
    82.                 if(empty($filename)) $filename = time() . "dat";
    83.                    
    84.                 /* prefix the email number to the filename in case two emails
    85.                  * have the attachment with the same file name.
    86.                  */
    87.         $fp = fopen('/mnt/c/ubuntu/pochta/'. $filename, 'w+');
    88.               fwrite($fp, $attachment['attachment']);
    89.              fclose($fp);
    90.             }
    91.  
    92.         }
    93.         if($count++ >= $max_emails) break;
    94.     }
    95. }
    96. /* close the connection */
    97. imap_close($inbox);
    98. echo "Done\n";
    99. ?>
    .
     

    Вложения:

  2. MouseZver

    MouseZver Суперстар

    С нами с:
    1 апр 2013
    Сообщения:
    7.750
    Симпатии:
    1.322
    Адрес:
    Лень
    Мудрость приходит годами!