mshannaq

using php to connect to imap mailbox and download attachments

Jun 27th, 2022 (edited)
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.85 KB | None | 0 0
  1. <?php
  2. //set_time_limit(3000);
  3.  
  4. /* connect to gmail with your credentials */
  5. $hostname = '{massarcloud.sa:993/imap/ssl}INBOX';
  6. $username = '[email protected]';
  7. $password = 'PASSWORD';
  8.  
  9. /* try to connect */
  10. $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
  11.  
  12. $emails = imap_search($inbox, 'FROM "[email protected]"');
  13.  
  14. /* if any emails found, iterate through each email */
  15. if($emails) {
  16.  
  17.     $count = 1;
  18.  
  19.     /* put the newest emails on top */
  20.     rsort($emails);
  21.  
  22.     /* for every email... */
  23.     foreach($emails as $email_number)
  24.     {
  25.  
  26.         /* get information specific to this email */
  27.         $overview = imap_fetch_overview($inbox,$email_number,0);
  28.  
  29.         $message = imap_fetchbody($inbox,$email_number,2);
  30.  
  31.         /* get mail structure */
  32.         $structure = imap_fetchstructure($inbox, $email_number);
  33.  
  34.         $attachments = array();
  35.  
  36.         /* if any attachments found... */
  37.         if(isset($structure->parts) && count($structure->parts))
  38.         {
  39.             for($i = 0; $i < count($structure->parts); $i++)
  40.             {
  41.                 $attachments[$i] = array(
  42.                     'is_attachment' => false,
  43.                     'filename' => '',
  44.                     'name' => '',
  45.                     'attachment' => ''
  46.                 );
  47.  
  48.                 if($structure->parts[$i]->ifdparameters)
  49.                 {
  50.                     foreach($structure->parts[$i]->dparameters as $object)
  51.                     {
  52.                         if(strtolower($object->attribute) == 'filename')
  53.                         {
  54.                             $attachments[$i]['is_attachment'] = true;
  55.                             $attachments[$i]['filename'] = $object->value;
  56.                         }
  57.                     }
  58.                 }
  59.  
  60.                 if($structure->parts[$i]->ifparameters)
  61.                 {
  62.                     foreach($structure->parts[$i]->parameters as $object)
  63.                     {
  64.                         if(strtolower($object->attribute) == 'name')
  65.                         {
  66.                             $attachments[$i]['is_attachment'] = true;
  67.                             $attachments[$i]['name'] = $object->value;
  68.                         }
  69.                     }
  70.                 }
  71.  
  72.                 if($attachments[$i]['is_attachment'])
  73.                 {
  74.                     $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
  75.  
  76.                     /* 3 = BASE64 encoding */
  77.                     if($structure->parts[$i]->encoding == 3)
  78.                     {
  79.                         $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  80.                     }
  81.                     /* 4 = QUOTED-PRINTABLE encoding */
  82.                     elseif($structure->parts[$i]->encoding == 4)
  83.                     {
  84.                         $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.  
  90.         /* iterate through each attachment and save it */
  91.         foreach($attachments as $attachment)
  92.         {
  93.             if($attachment['is_attachment'] == 1)
  94.             {
  95.                 $filename = $attachment['name'];
  96.                 if(empty($filename)) $filename = $attachment['filename'];
  97.  
  98.                 if(empty($filename)) $filename = time() . ".dat";
  99.                 $folder = "attachment";
  100.                 if(!is_dir($folder))
  101.                 {
  102.                      mkdir($folder);
  103.                 }
  104.                 $fp = fopen("./". $folder ."/". $email_number . "-" . $filename, "w+");
  105.                 fwrite($fp, $attachment['attachment']);
  106.                 fclose($fp);
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112. /* close the connection */
  113. imap_close($inbox);
  114.  
  115. echo "all attachment Downloaded";
  116.  
  117. ?>
  118.  
Add Comment
Please, Sign In to add comment