Occasionally, I want to get the latest emails from Gmail in time, but if there is no suitable environment for the captain, this will be difficult. After all, Google is still somewhat difficult on the mainland.
So I thought I could use the PHP+Google API to get a simple message about Gmail.
Organizing tasks
PHP version > 5.4, and open cli and json extensions (usually all open, such as Pagoda, LNMP)
Composer relies on management tools (this article takes CentOS 7 as an example and can be deployed directly yum)
Google account with Gmail enabled
1, openGmail API switch
Https://developers.google.com/gmail/api/quickstart/php
1. Click on Enable the Gmail API
2. Create a project and fill in the project title
Self-willed
3. Get the Client ID and Client Secret
Click the button to download the user credential profile. Note: properly save these two important information.
Second, install the Google Client Library
1. Deploy composer installation tool
yum install composer -y
2.SSH login to the root directory of the site
For example, /www/wwwrooot/mygmailapi.com/
cd /www/wwwrooot/mygmailapi.com/
composer require google/apiclient:^2.0
3. Upload the sample code and the previous user credentials configuration file for save
The Gmail API gives a simple sample file: https://github.com/gsuitedevs/php-samples/blob/master/gmail/quickstart/quickstart.php
Download the sample code (quickstart.php) directly, and then upload the user credential configuration file (credentials.json) obtained in the first step to the server to execute the composer directory.
3, test
In order to ensure the safety, the sample code provided by the Gmail API enters the php_sapi_name, which is the detection of the boot environment.
Can only be debugged in the cli environment (commandline, command line), not allowed to test in the web environment
Therefore, we can execute this sample code independently in SSH through php, and then decide to install it into the consumer environment and then clear its sapi detection.
Execute under SSH
cd /www/wwwrooot/mygmailapi.com/
php -f quickstart.php
复制认证链接到浏览器中open,登录Google账号后会博得Token,将Token复制并粘贴回SSH窗口
此刻再次执行php -f quickstart.php
,便可看得Gmail的emailLabel
4、写代码
上述步骤只是测试通过php获得email里的Lable(标签),现时就开始写代码,获得email的其他信息,譬如未读邮件列表
能使用Gmail自带的email过滤器,将收件箱的未读邮件挑选出来,对应的挑选参数为:is:unread label:inbox
如果需要挑选出全部的未读邮件(包罗渣滓箱),只需免去label:inbox便可,如:is:unread
核心部分:
获得题目:
$opt_param = array();
//挑选参数
$opt_param['q'] = "is:unread label:inbox ";
$opt_param['maxResults'] = 10;
$results = $service->users_messages->listUsersMessages("me", $opt_param);
foreach($results as $mail){
$message = $service->users_messages->get("me", $mail['id']);
$headers = $message->getPayload()->getHeaders();
$subject = getHeader($headers, 'Subject');
echo $subject.'\r\n';
}
获得摘要:
$opt_param = array();
//挑选参数
$opt_param['q'] = "is:unread label:inbox ";
$opt_param['maxResults'] = 10;
$threads = $service->users_threads->listUsersThreads("me", $opt_param);
foreach ($threads as $thread) {
echo $thread->getSnippet() . '\r\n';
}
完整代码:
https://file.bugxia.com/s/QF7SrGCJmpytjxG
结果:
SSH下执行
#获得邮件题目
php -f getGmail.php title
#获得邮件摘要
php -f getGmail.php snippet
如果清除代码中的sapi检测,则能通过Web进行访问
原文链接:https://host.fubi.hk/foreshadowinghost/zhishiku/20181025/8372.html