anod / gmail-imap
基于 Zend Imap 库构建的用于通过 IMAP 与 Gmail 交互的 API
v1.3.1
2020-01-27 10:53 UTC
Requires
- php: ^7.2
- psr/log: ^1.1
- zendframework/zend-mail: 2.10.*
Requires (Dev)
- google/apiclient: ^2.0
- phpunit/phpunit: ^8
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-08-27 20:24:34 UTC
README
封装 Gmail IMAP API 的库。
功能
该库扩展了 Zend Imap 库,因此它提供了所有基本的 IMAP 功能。此外,它还提供了简单的 Gmail 特定 API,用于
- OAUTH2 认证
- 获取消息的 UID
- 与 Gmail 标签交互:检索、应用、删除
- 获取 Gmail 线程 ID
- 将 Gmail 消息 ID 表示形式进行转换的工具:从大整数到十六进制和相反
- 存档消息
待办事项
- 移动到收件箱
- 标记为已读/未读
要求
Composer
- Zend Imap Library
使用示例
<?php // \Anod\Gmail\Math::bchexdec("FMfcgxwGCkZzGTlZHvpgXBHPvqzkLKtC") == "1430824723191418813" $msgId = "1430824723191418813"; $email = "alex.gavrishev@gmail.com"; $protocol = new \Anod\Gmail\Imap(true /* debug */); $gmail = new \Anod\Gmail\Gmail($protocol); $gmail->setId("Example App", "0.1", "Alex Gavrishev", "alex.gavrishev@gmail.com"); $gmail->connect(); $gmail->authenticate($email, $token); $gmail->sendId(); $gmail->selectInbox(); $uid = $gmail->getUID($msgId); $gmail->applyLabel($uid, "Very Important"); // Apply label to a message with specific UID $gmail->removeLabel($uid, "Not Important"); // Remove label to a message with specific UID $message = $gmail->getMessageData($uid); // Retrieve message content $details = array( 'subject' => $message->getHeader('subject', 'string'), 'body' => $message->getContent(), 'from' => $message->getHeader('from', 'string'), 'to' => $message->getHeader('to', 'string'), 'thrid' => \Anod\Gmail\Math::bcdechex($message->getThreadId()), 'labels' => $message->getLabels() ); $gmail->archive($uid); // Archive the message
使用本地服务器获取令牌的示例 php -S localhost:8000
<?php session_start(); require_once __DIR__ . '/vendor/autoload.php'; // Based on https://developers.google.com/identity/protocols/OAuth2 $clientId = Clien Id from google console; $clientSecret = Client secret from google console; $redirectUri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; $client = new Google_Client([ 'client_id' => $clientId, 'client_secret' => $clientSecret, 'redirect_uri' => $redirectUri ]); // Scope for IMAP access $client->addScope("https://mail.google.com/"); if (isset($_GET['code'])) { $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); $_SESSION['gmail_token'] = $token; // redirect back to the example header('Location: ' . filter_var($redirectUri, FILTER_SANITIZE_URL)); exit; } if (!empty($_SESSION['gmail_token'])) { $client->setAccessToken($_SESSION['gmail_token']); if ($client->isAccessTokenExpired()) { unset($_SESSION['gmail_token']); } } else { $authUrl = $client->createAuthUrl(); } if ($authUrl) { $authUrl = $client->createAuthUrl(); header("Location: $authUrl"); exit; } $token = $client->getAccessToken()['access_token']; echo "Token: $token\n\n";
作者
Alex Gavrishev, 2013
许可
该库根据 MIT 许可证授权。