php upload email attachment to file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    php upload email attachment to file

    here is a php script which saves email attachments to files into a specified directory.

    PHP Code:
    <?php

    $subject_password 
    'asdfdfgg'/* set upload password for subject, the emails with upload files should have this password in the subject */

    $upload_path './uploads'//withoult last slash and chmod 0777


    $mbox imap_open("{mail.yourhost.com:143/imap/notls}INBOX""upload@yourhost.com""password")or die("Connection to server failed");
    $headers imap_headers($mbox) or die("Couldn't get emails");
    $numEmails sizeof($headers);
    for(
    $i 1$i <= $numEmails$i++) 
    {

        
    $header imap_headerinfo($mbox$i); 

        
    $from $header->fromaddress

        
    $subject strip_tags($header->subject); 
        
        
    $date $header->date;
        
        
    $info imap_fetchstructure($mbox$i);
        

         if(
    stristr($subject$subject_password))
        {
      
    // find out how may parts the object has
      
    $numparts count($info->parts);
      
      for(
    $k=0;$k<$numparts;$k++)
      {
       if(
    $info->parts[$k]->dparameters[0]->attribute=='filename')
       {
        
        
    //attachment to array
        
    $a['file_size'] = $info->parts[$k]->bytes;
        
    $a['file_type'] = $info->parts[$k]->subtype;
        
    $a['file_name'] = $info->parts[$k]->dparameters[0]->value;
        
    $a['file_from'] = $header->from[0]->mailbox "@" $header->from[0]->host;
        
    $a['file_content'] = base64_decode(imap_fetchbody($mbox$i$k+1));
        
    $att[] = $a;
       }
      }
        }

        
    imap_delete($mbox, (int)$i);
    }
    imap_expunge($mbox);
    imap_close($mbox);

    for(
    $l=0;$l<count($att);$l++)
    {
        
    file_put_contents($upload_path.'/'.$att[$l]['file_name'], $att[$l]['file_content']);
    }

    ?>
    Advertise your mobile site for FREE with AdTwirl

Working...
X