初识GmailAPI(PHP)

本文介绍如何使用PHP和Google API获取Gmail中的未读邮件列表,包括邮件标题和摘要,适用于没有合适环境接收实时邮件的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值