Monday, 13 March 2017

IMAP Gmail read messages details from inbox

IMAP Gmail read messages details

Check that IMAP is turned on
If Not, Please enable.


Library Code

/** functions used for getting message **/
function flattenParts($messageParts, $flattenedParts = array(), $prefix = '', $index = 1, $fullPrefix = true) {

    foreach($messageParts as $part) {
        $flattenedParts[$prefix.$index] = $part;
        if(isset($part->parts)) {
            if($part->type == 2) {
                $flattenedParts = flattenParts($part->parts, $flattenedParts, $prefix.$index.'.', 0, false);
            }
            elseif($fullPrefix) {
                $flattenedParts = flattenParts($part->parts, $flattenedParts, $prefix.$index.'.');
            }
            else {
                $flattenedParts = flattenParts($part->parts, $flattenedParts, $prefix);
            }
            unset($flattenedParts[$prefix.$index]->parts);
        }
        $index++;
    }

    return $flattenedParts;
           
}


function getPart($con, $messageNumber, $partNumber, $encoding) {
   
    $data = imap_fetchbody($con, $messageNumber, $partNumber);
    switch($encoding) {
        case 0:
            return $data;
            break; // 7BIT
        case 1:
            return $data;
            break;// 8BIT
        case 2:
            return $data; // BINARY
            break;
        case 3:
            return base64_decode($data);  // BASE64
            break;
        case 4:
            return quoted_printable_decode($data); // QUOTED_PRINTABLE
            break;
        case 5:
            return $data; // OTHER
            break;
    }   
   
}


function getFilenameFromPart($part) {

    $filename = '';
   
    if($part->ifdparameters) {
        foreach($part->dparameters as $object) {
            if(strtolower($object->attribute) == 'filename') {
                $filename = $object->value;
            }
        }
    }

    if(!$filename && $part->ifparameters) {
        foreach($part->parameters as $object) {
            if(strtolower($object->attribute) == 'name') {
                $filename = $object->value;
            }
        }
    }
   
    return $filename;
   
}

/** functions used for getting message **/



How to read specific message
$username = 'mygmail@gmail.com'; //Gmail address
$password = 'gma12#@c'; //Gmail password
error_reporting(0);

 $bodyId=35; //This is message Id

 //Read Message from indebox
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
   
   //Read Message from Trash
    // $hostname = '{imap.gmail.com:993/imap/ssl}[Gmail]/Trash';
 
   //Read Message from Sent
 //$hostname = '{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail';




$con = imap_open($hostname, $username, $password);
$messageNumber = $bodyId;
$structure = imap_fetchstructure($con, $messageNumber);
//pr($structure);die;

$message='';
if(!empty($structure->parts)){
 
 $flattenedParts = flattenParts($structure->parts); 
 foreach($flattenedParts as $partNumber => $part) {  
  switch($part->type) {
     
   case 0:
    // the HTML or plain text part of the email
    $message = getPart($con, $messageNumber, $partNumber, $part->encoding);
    // now do something with the message, e.g. render it
      
   break;
    
   case 1:
    // multi-part headers, can ignore
    
   break;
   case 2:
    // attached message headers, can ignore
   break;
    
   case 3: // application
   case 4: // audio
   case 5: // image
   case 6: // video
   case 7: // other
   //pr($part);
   //echo "
$$$$$";
    $filename = getFilenameFromPart($part);
    if($filename) {
     // it's an attachment
     $attachment = getPart($con, $messageNumber, $partNumber, $part->encoding);
        if (empty($filename))
      $filename = $filename;
       
     if (empty($filename))
      $filename = time() . ".dat";
     $folder = "attachment";
     if (!is_dir($folder)) {
      mkdir($folder);
     }
     $fp = fopen("./" . $folder . "/" . $messageNumber . "-" . $filename, "w+");
     fwrite($fp, $filename);
     fclose($fp);
     // now do something with the attachment, e.g. save it somewhere
     $message.='

' . $messageNumber . '-' . $filename . '
'; } break; } } }else{ $message = getPart($con, $messageNumber, 1, $structure->encoding); } if($message){ $message=$message; } echo $message; imap_close($con, CL_EXPUNGE);