public function getEmails($from) { // Get the API client and construct the service object. $client = $this->getClient(); $service = new \Google_Service_Gmail($client); $emails = $service->users_messages->listUsersMessages('me', array('q' => 'from:' . $from . ' is:unread')); $messages = array(); foreach ($emails->getMessages() as $msg) { /** @var Google_Service_Gmail_Message $msg */ $msg = $service->users_messages->get('me', $msg->getId()); /** @var Google_Service_Gmail_MessagePart $payload */ $payload = $msg->getPayload(); $payload = $payload->getParts()[0]; $payload = $payload->getParts()[0]; $rawData = $payload->getBody()->getData(); $sanitizedData = strtr($rawData, '-_', '+/'); $decodedMessage = base64_decode($sanitizedData); $decodedMessage = trim(preg_replace('#--.*#s', '', $decodedMessage)); $messages[] = $decodedMessage; // set read $mods = new Google_Service_Gmail_ModifyMessageRequest(); $mods->setRemoveLabelIds(array('UNREAD')); $service->users_messages->modify('me', $msg->getId(), $mods); } return $messages; }
/** * Modify the Labels a Message is associated with. * * @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 string $messageId ID of Message to modify. * @param array $labelsToAdd Array of Labels to add. * @param array $labelsToRemove Array of Labels to remove. */ function modifyMessage($service, $userId, $messageId, $labelsToAdd, $labelsToRemove) { $mods = new Google_Service_Gmail_ModifyMessageRequest(); $mods->setAddLabelIds($labelsToAdd); $mods->setRemoveLabelIds($labelsToRemove); try { $message = $service->users_messages->modify($userId, $messageId, $mods); } catch (Exception $e) { print G::LoadTranslation("ID_PMGMAIL_GENERAL_ERROR") . $e->getMessage(); } }
function modifyMessage($service, $userId, $messageId, $labelsToAdd, $labelsToRemove) { $mods = new Google_Service_Gmail_ModifyMessageRequest(); $mods->setAddLabelIds($labelsToAdd); $mods->setRemoveLabelIds($labelsToRemove); try { $message = $service->users_messages->modify($userId, $messageId, $mods); return $message; } catch (Exception $e) { //print 'An error occurred: ' . $e->getMessage(); } }
/** * 將郵件設定為 已讀 * - 將 label id "UNREAD" 移除即可 * * @return boolean */ public static function setMessageLabelToIsRead($messageId) { $mods = new Google_Service_Gmail_ModifyMessageRequest(); $mods->setRemoveLabelIds(['UNREAD']); try { $message = self::getService()->users_messages->modify('me', $messageId, $mods); return true; } catch (Exception $e) { echo 'Remove message lable error ' . $e->getMessage() . "\n"; return false; } }
public function setRead($messageId) { try { var_dump($labels); $client = unserialize($_SESSION['client']); $service = new Google_Service_Gmail($client); $user = '******'; $postBody = new Google_Service_Gmail_ModifyMessageRequest(); $array = array(); array_push($array, 'UNREAD'); $postBody->setRemoveLabelIds($array); $mail = $service->users_messages->modify($user, $messageId, $postBody); } catch (Exception $e) { return 'An error occurred: ' . $e->getMessage(); } }
$teamCli[$member] = ['name' => $member, 'hours' => $hours, 'hourDirection' => $hourDirection]; } } } $reader->close(); if (!empty($configuration['slackEndpoint']) && !empty($configuration['channel'])) { Cli::writeOutput('Notifying Slack team', Cli::COLOR_GREEN_DIM); // Instantiate with defaults, so all messages created // will be sent from 'Cyril' and to the #accounting channel // by default. Any names like @regan or #channel will also be linked. $client = new Client($configuration['slackEndpoint'], ['username' => 'HourBank', 'channel' => $configuration['channel'], 'icon' => ':clock4:', 'markdown_in_attachments' => ['fields']]); /** @var $client Message */ !$dryRun && $client->attach(['fallback' => $teamFallback, 'color' => 'bad', 'fields' => $team])->send($headLine); } if ($configuration['markAsDone']) { $modify = new Google_Service_Gmail_ModifyMessageRequest(); $modify->setRemoveLabelIds(['INBOX']); !$dryRun && $service->users_messages->modify($user, $message->getId(), $modify); Cli::writeOutput('Message marked as *done*', Cli::COLOR_YELLOW_DIM); } if ($configuration['removeMessage'] && !$dryRun) { !$dryRun && $service->users_messages->trash($user, $message->getId()); Cli::writeOutput('Message moved to trash!', Cli::COLOR_YELLOW_DIM); } if ($headLine) { Cli::writeOutput(String::newLine() . $headLine . String::newLine(), Cli::COLOR_WHITE_BOLD); } foreach ($teamCli as $member => $data) { Cli::writeOutput($member . ': ' . $data['hours'] . ' ' . $data['hourDirection'], $data['hourDirection'] == 'positivas' ? Cli::COLOR_GREEN : Cli::COLOR_RED); } Cli::writeOutput(String::newLine());