-1

I'm trying to create a piece of code that will go into the mailbox and take out the attachments of a specific file. So far I am only able to view if there is an attachment or if there is not an attachment on the e-mail.

But I want it to be able to take the attachments out of the e-mail and then save them to a specified directory. The type of attachment I'm trying to take out is a .jpg

I've tried a bunch of different pieces of code that I've found on google and I've been trying to tailor it to fit into my code, but so far I have been unsuccessful in finding anything that works correctly.

I was wondering if anyone would be able to help me create a piece of code that would be able to take the attachments out of the emails and store them in a directory.

Thanks.

<?php

    /* connect to email */
    $hostname = '{*****.com:110/pop3}INBOX';
    $username = '*****';
    $password = '*****';

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

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

    // Search for the 39th email, which has an attachment
    $count = 39;


    // Fetch all the information about an email
    $attachment = imap_fetchstructure($inbox, $count);


    // find out how may parts the object has
    $numparts = count($attachment->parts);

// find if if multipart message
if ($numparts >= 2) {


   foreach ($attachment->parts as $part) {

      if ($part->disposition == "INLINE") {
         // inline message. Show number of lines

         printf("Inline message has %s lines<BR>", $part->lines);

      } elseif ($part->disposition == "ATTACHMENT") {
         // an attachment

         echo "Attachment found!";
         // print out the file name
         echo "Filename: ", $part->dparameters[0]->value;

      }

   }

}
    //}
    else {
   // only one part so get some useful info
   echo "No attachment";
}

imap_close($imap);
?>
aj mcgow
  • 1
  • 1
  • 1

1 Answers1

1

Instead of imap_search I used imap_check to retrieve messages overview, and the following worked. Go over messages found with imap_check, and this is how you extract the binary data of attachment:

    $mbox = imap_open( . . . . );
    $IMAPobj = imap_check($inbox);

    $start = $IMAPobj->Nmsgs-30;
    $end = $IMAPobj->Nmsgs;
    $result = imap_fetch_overview($inbox,"$start:$end",0);

    $count = $end;
    foreach ($result as $overview) {
        $parts = mail_mime_to_array($inbox, $count);
        foreach($parts as $part) {
            if(@$part['filename'] || @$part['name'] ) {

                $partName = $part['filename'] ? $part['filename'] : $part['name'];
                echo "Attachment name is " . basename($partName);
                echo "\n";

                if(preg_match( . . . write here a regex to detect ".jpg" in $partName . . .)) {
                    echo "Found file! Extracting binary data...";

                    $fileContents = $part['data'];
                    file_put_contents("attachment.jpg", $fileContents);
                }
            }
        }
    }
Sych
  • 1,849
  • 16
  • 19