$body = $_POST['message']; $subject = $_POST['subject']; $mime->addTo($to); $mime->addBcc($bcc); $mime->addCc($cc); $mime->setTXTBody($body); $mime->setHTMLBody($body); $mime->setSubject($subject); $message_body = $mime->getMessage(); $encoded_message = base64url_encode($message_body); // Gmail Message Body $message = new Google_Service_Gmail_Message(); $message->setRaw($encoded_message); // Gmail Draft $draft_body = new Google_Service_Gmail_Draft(); $draft_body->setMessage($message); // Save as Draft $draft = $service->users_drafts->create('me', $draft_body); if ($draft->getId()) { $notice = '<div class="alert alert-success">Draft saved successfully!</div>'; } else { $notice = '<div class="alert alert-danger">Oops...something went wrong, try again later</div>'; } } } /** * Get the list of message ids and filter only messages in inbox under the primary category tab * Also limit the result to 5 and return only the message ids */ $list = $service->users_messages->listUsersMessages('me', ['maxResults' => 5, 'fields' => 'messages/id', 'q' => 'in:inbox category:primary']); $messageList = $list->getMessages();
/** * Create Draft email. * * @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. * @param Google_Service_Gmail_Message $message Message of the created Draft. * @return Google_Service_Gmail_Draft Created Draft. */ function createDraft($service, $user, $message) { $draft = new Google_Service_Gmail_Draft(); $draft->setMessage($message); try { $draft = $service->users_drafts->create($user, $draft); print 'Draft ID: ' . $draft->getId(); } catch (Exception $e) { print 'An error occurred: ' . $e->getMessage(); } return $draft; }