public function addProcessedLabel() { $labels = array(); $service = Mailbox::getGmailService(); try { $labelsResponse = $service->users_labels->listUsersLabels("me"); if ($labelsResponse->getLabels()) { $labels = array_merge($labels, $labelsResponse->getLabels()); } } catch (Excetion $e) { print 'An error occurred: ' . $e->getMessage(); } $processed_label = false; foreach ($labels as $label) { if ($label->getName() == 'PROCESSED') { $processed_label = true; $processed_label_id = $label->getId(); } } if (!$processed_label) { $label = new \Google_Service_Gmail_Label(); $label->setName("PROCESSED"); try { $label = $service->users_labels->create("me", $label); } catch (Exception $e) { print 'An error occurred: ' . $e->getMessage(); } } $mods = new \Google_Service_Gmail_ModifyMessageRequest(); $mods->setAddLabelIds([$processed_label_id]); try { $message = $service->users_messages->modify("me", $this->gmail_uid, $mods); } catch (Exception $e) { print 'An error occurred: ' . $e->getMessage(); } }
/** * Get a label ID for the given label name. Creates the label if it doesn't already exist. * * @param string $labelName Name of the new Label * @return string Label ID */ function getOrCreateLabelID($labelName) { global $ini; static $label = NULL; if ($label === NULL) { $results = $this->service->users_labels->listUsersLabels($ini['gmail_username']); foreach ($results->getLabels() as $thisLabel) { if ($thisLabel->getName() === $labelName) { $label = $thisLabel; break; } } } if ($label === NULL) { // Label didn't exist - create it $label = new Google_Service_Gmail_Label(); $label->setName($labelName); try { $label = $this->service->users_labels->create($ini['gmail_username'], $label); if (DEBUG) { print 'Label \'$labelName\' with ID: ' . $label->getId() . " created \n"; } } catch (Exception $e) { print 'Error: Exception - ' . $e->getMessage() . "\n"; } } return $label->getId(); }