/** * Get all Threads in the user's mailbox. * * @param Google_Service_Gmail $service Authorized Gmail API instance. * @param string $userId User's email address. The special value 'me' * can be used to indicate the authenticated user. * @return array Array of Threads. */ function listThreads($service, $maxResults) { $user = '******'; $pageToken = NULL; $opt_param = array(); $opt_param['includeSpamTrash'] = false; // $opt_param['maxResults'] = $maxResults; $opt_param['maxResults'] = 5; $opt_param['labelIds'] = 'INBOX'; $threads = array(); $threadsResponse = $service->users_threads->listUsersThreads($user, $opt_param); if ($threadsResponse->getThreads()) { $threads = array_merge($threads, $threadsResponse->getThreads()); } $threadsList = new ThreadList(); $mimeTypes = array("image/png", "image/bmp", "image/gif", "image/jpeg", "image/tiff"); foreach ($threads as $thread) { $threadItem = thread_get($service, $user, $thread->getId()); $threadObject = new Thread($threadItem->getId(), $threadItem->getHistoryId(), $threadItem->getSnippet()); $threadMessages = $threadItem->getMessages(); foreach ($threadMessages as $threadMessage) { $threadMessageObject = new Message($threadMessage->getId(), $threadMessage->getHistoryId(), $threadMessage->getSizeEstimate(), $threadMessage->getSnippet(), $threadMessage->getThreadId()); foreach ($threadMessage->getLabelIds() as $label) { $threadMessageObject->addLabels($label); } $payloads = $threadMessage->getPayload(); // gets parts $payloadObject = new Payload($payloads->getFilename(), $payloads->getMimeType(), $payloads->getPartId()); $payloadBody = $payloads->getBody(); $payloadBodyObject = new Body($payloadBody->getSize(), $payloadBody->getData(), $payloadBody->getAttachmentId()); $payloadObject->setBody($payloadBodyObject); $payloadHeaders = $payloads->getHeaders(); foreach ($payloadHeaders as $payloadHeader) { $payloadObject->addHeaders($payloadHeader->getName(), $payloadHeader->getValue()); } $payloadParts = $payloads->getParts(); foreach ($payloadParts as $payloadPart) { $payloadPartObject = new Part($payloadPart->getPartId(), $payloadPart->getMimeType(), $payloadPart->getFilename()); $partHeaders = $payloadPart->getHeaders(); foreach ($partHeaders as $partHeader) { $payloadPartObject->addHeaders($partHeader->getName(), $partHeader->getValue()); } $partBody = $payloadPart->getBody(); $mimeType = $payloadPart->getMimeType(); $fileName = $payloadPart->getFilename(); $partBodyData = $partBody->getData(); if ($mimeType == "text/html") { $partBodyData = stripStyle($partBodyData); } // get attachment if (in_array($mimeType, $mimeTypes)) { $payloadObject->setHasAttachments(true); $messageId = $threadMessage->getId(); $attachmentId = $partBody->getAttachmentId(); // $messageId = "1517ad66755a3a4e"; // $attachmentId = "ANGjdJ_NHguibEZ714ypsmh4AaLozb2ljIe9UnAwH9txKGemrt_5LedqDr3KZ6RLyw40lb5n584YB5ZKMOtiw5o9OT7ClArfD8PDezzdA7cOyf6HW0CNwK_L_vRDPiEoMBRfQljfntWQoC9oAr1C-PjC-wmMuEMDzhezghpQ5vqpKV4nBGhFZ8AHf4QAwvrtT0T8dgn3L6uKGce2WEAkUMKSdCDDAruntSb4cUA9joHouiJ9zFSzQz0Wwc4Ru2MSofkPMWi9F1f2pb1MdCgzUTMgfSEfoLbJGSYgNen5WES7t8OO8B1SPn2STcHOeNg"; $attachment = getAttachment($service, $user, $messageId, $attachmentId); $image = getAttachmentAndWrite($service, $user, $messageId, $attachmentId, $fileName, $mimeType); $payloadObject->addImage($image); $payloadPartObject->addBody(new Body($partBody->getSize(), $partBodyData, $partBody->getAttachmentId(), $attachment, $image)); } else { $payloadPartObject->addBody(new Body($partBody->getSize(), $partBodyData, $partBody->getAttachmentId())); } // ---------------------------------------------------------- $payloadPartsSub = $payloadPart->getParts(); foreach ($payloadPartsSub as $payloadPartSub) { $payloadPartSubObject = new Part($payloadPartSub->getPartId(), $payloadPartSub->getMimeType(), $payloadPartSub->getFilename()); $partHeaders = $payloadPartSub->getHeaders(); foreach ($partHeaders as $partHeader) { $payloadPartSubObject->addHeaders($partHeader->getName(), $partHeader->getValue()); } $partBody = $payloadPartSub->getBody(); $partBodyData = $partBody->getData(); if ($payloadPartSub->getMimeType() == "text/html") { $partBodyData = stripStyle($partBodyData); } $payloadPartSubObject->addBody(new Body($partBody->getSize(), $partBodyData, $partBody->getAttachmentId())); $payloadPartObject->addParts($payloadPartSubObject); } // ----------------------------------------------------------------------- $payloadObject->addParts($payloadPartObject); } if ($payloadObject->getHasAttachments()) { foreach ($payloadObject->getParts() as $key => $parts) { if ($parts->mimeType == "multipart/alternative") { foreach ($parts->getParts() as $key1 => $part) { if ($part->mimeType == "text/html") { $data = subImagesInHtml($part, $payloadObject->getImages()); $payloadObject->parts[$key]->parts[$key1]->body[0]->data = $data; } } } } } $threadMessageObject->setPayload($payloadObject); $threadObject->addMessage($threadMessageObject); } $threadsList->addThread($threadObject); } echo json_encode($threadsList); // echo json_last_error_msg(); }