public function updateMail($single_message) { $client = $this->getClient(); $service = new Google_Service_Gmail($client); $userId = 'me'; $messageId = $single_message->getId(); $threadId = $single_message->getThreadId(); $historyId = $single_message->getHistoryId(); $labelIds = $single_message->getLabelIds(); $headers = $single_message->getPayload()->getHeaders(); foreach ($headers as $header) { if ($header->getName() == 'Subject') { $subject = $header->getValue(); } if ($header->getName() == 'From') { $from = $header->getValue(); } if ($header->getName() == 'To') { $to = $header->getValue(); #var_dump($to); die; } if ($header->getName() == 'Date') { $message_date = $header->getValue(); $time = date('Y-m-d H:i:s', strtotime($message_date)); } } $prefix = array("Re: ", "Fwd: "); $new_subject = str_replace($prefix, "", $subject); $fromCheck = InboxMail::where('from_mail', $from)->get(); $subjectCheck = InboxMail::where('subject', $new_subject)->get()->first(); if (count($fromCheck) > 0 && count($subjectCheck) > 0) { $old_thread_id = $subjectCheck->thread_id; $single_message->setThreadId($old_thread_id); $threadId = $single_message->getThreadId(); } $body = $single_message->getPayload()->getBody(); $body_new = $this->decode_body($body['data']); if (!$body_new) { $parts = $single_message->getPayload()->getParts(); foreach ($parts as $part) { if ($part['body']) { $body_new = $this->decode_body($part['body']->data); if ($body_new === true) { break; } } if ($part['parts'] && !$body_new) { foreach ($part['parts'] as $p) { if ($p['mimeType'] === 'text/plain' && $p['body']) { $body_new = $this->decode_body($p['body']->data); break; } } } if ($body_new) { break; } } } $body = $body_new; unset($attachment); $parts = $single_message->getPayload()->getParts(); foreach ($parts as $part) { if ($part->getFilename() != null && strlen($part->getFilename()) > 0) { $filename = $part->getFilename(); $attId = $part->getBody()->getAttachmentId(); $attachPart = $service->users_messages_attachments->get($userId, $messageId, $attId); $attachPart = strtr($attachPart->getData(), "-_", "+/"); $code_base64 = $attachPart; $code_binary = base64_decode($code_base64); $file_ext = new SplFileInfo($filename); $file_ext = $file_ext->getExtension(); $file_hash = hash('sha256', $attId); //var_dump($attId); die; $file_location = public_path() . '/assets/dist/support/attach/' . $filename; #var_dump($file_ext); die; file_put_contents($file_location, $code_binary); #echo "Your attachment ". $filename." with id ".$attId." saved succesfully at ".$file_location; $attachment[] = array('filename' => $filename, 'attachmentId' => $attId, 'filelocation' => $file_location); /* $image= imagecreatefromstring($code_binary); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image);*/ } } var_dump($attachment); die; }
public function updateMessage() { $client = $this->getClient(); $service = new Google_Service_Gmail($client); $userId = 'me'; $list = $service->users_messages->listUsersMessages($userId, ['maxResults' => 1000]); $messageList = $list->getMessages(); foreach ($messageList as $mlist) { $idCheck = InboxMail::where('message_id', $mlist->id)->get(); if (count($idCheck) == 0) { $optParamsGet2['format'] = 'full'; $single_message = $service->users_messages->get('me', $mlist->id, $optParamsGet2); #var_dump($single_message); die; #var_dump($single_message->getPayload()->getFilename());die; #$raw = $single_message->getRaw(); // while using $optParamsGet2 "format" is "raw" instead of "full" $messageId = $single_message->getId(); $threadId = $single_message->getThreadId(); $historyId = $single_message->getHistoryId(); $labelIds = $single_message->getLabelIds(); #var_dump(json_decode(json_encode($labelIds))); die; $headers = $single_message->getPayload()->getHeaders(); foreach ($headers as $header) { if ($header->getName() == 'Subject') { $subject = $header->getValue(); } if ($header->getName() == 'From') { $from = $header->getValue(); } if ($header->getName() == 'To') { $to = $header->getValue(); #var_dump($to); die; } if ($header->getName() == 'Date') { $message_date = $header->getValue(); $time = date('Y-m-d H:i:s', strtotime($message_date)); } } $prefix = array("Re: ", "Fwd: "); $new_subject = str_replace($prefix, "", $subject); $fromCheck = InboxMail::where('from_mail', $from)->get(); $subjectCheck = InboxMail::where('subject', $new_subject)->get()->first(); if (count($fromCheck) > 0 && count($subjectCheck) > 0) { $old_thread_id = $subjectCheck->thread_id; $single_message->setThreadId($old_thread_id); $threadId = $single_message->getThreadId(); } /* body message */ $body = $single_message->getPayload()->getBody(); #var_dump($this->decode_body("PGRpdiBkaXI9Imx0ciI-dW5yZWFkIG1lc3NhZ2UgYm9keTwvZGl2Pg0K")); die; $body_new = $this->decode_body($body['data']); #var_dump($body_new); die; #var_dump($single_message->getPayload()->getParts()); die; if (!$body_new) { $parts = $single_message->getPayload()->getParts(); foreach ($parts as $part) { if ($part['body']) { $body_new = $this->decode_body($part['body']->data); if ($body_new === true) { break; } } if ($part['parts'] && !$body_new) { foreach ($part['parts'] as $p) { if ($p['mimeType'] === 'text/plain' && $p['body']) { $body_new = $this->decode_body($p['body']->data); break; } } } if ($body_new) { break; } } } $body = $body_new; //attachment success unset($attachment); $parts = $single_message->getPayload()->getParts(); foreach ($parts as $part) { if ($part->getFilename() != null && strlen($part->getFilename()) > 0) { $filename = $part->getFilename(); $attId = $part->getBody()->getAttachmentId(); $attachPart = $service->users_messages_attachments->get($userId, $messageId, $attId); $attachPart = strtr($attachPart->getData(), "-_", "+/"); $code_base64 = $attachPart; $code_binary = base64_decode($code_base64); $file_ext = new SplFileInfo($filename); $file_ext = $file_ext->getExtension(); $file_hash = hash('sha256', $attId); $file_location = public_path() . '/attachId/'; #var_dump($file_ext); die; file_put_contents($file_location, $code_binary); #echo "Your attachment ". $filename." with id ".$attId." saved succesfully at ".$file_location; $attachment[] = array('filename' => $filename, 'attachmentId' => $attId, 'filelocation' => $file_location); // $image= imagecreatefromstring($code_binary); // header('Content-Type: image/jpeg'); // imagejpeg($image); // imagedestroy($image); } } /* remove re and fwd from subject $subject = preg_replace('/^Re: /', '', $subject); $subject = preg_replace('/^Fwd: /', '', $subject);*/ $thread_id = MailSupport::where('thread_id', $threadId)->get(); if (!count($thread_id) && $labelIds['0'] == 'INBOX') { $this->autoMessage($from, $to, $subject, 123, 'test', 123); } $inboxmail = new InboxMail(); $inboxmail->message_id = $mlist->id; $inboxmail->thread_id = $threadId; $inboxmail->history_id = $historyId; if (isset($labelIds['0'])) { $inboxmail->label = $labelIds['0']; } $inboxmail->subject = $subject; $inboxmail->from_mail = $from; $inboxmail->to_mail = $to; $inboxmail->body = $body; if (isset($attachment)) { $inboxmail->attachment = json_encode($attachment); } $inboxmail->time = $time; $inboxmail->save(); } } }