简体   繁体   English

使用PHP中的Gmail API阅读新邮件

[英]Read new messages using Gmail API in PHP

I need to read messages from user's inbox using gmail API client. 我需要使用gmail API客户端从用户的收件箱中读取消息。 And what I want is to get only new messages since last time I read them. 我想要的是自上次阅读以来仅获得新消息。 Sure, I don't want to read all messages and check which of them are new, I want to get just new messages in a query. 当然,我不想读取所有消息并检查其中哪些是新消息,我想在查询中获得新消息。 The best solution I could figure out so far is to star messages I have read and get not starred messages like this: 到目前为止,我能找到的最佳解决方案是给已阅读的邮件加注星标,而不是加注星标,例如:

$gmail->users_messages->listUsersMessages('me',['maxResults' => 10, 'q' => "in:inbox AND -in:starred"]);

What I don't like in this solution is that this affects message some way and assuming this email is used to read and send messages in common way (using web browser) I'm not sure if user can't star messages himself so my application consider it as read. 我在此解决方案中不喜欢的是,这会以某种方式影响消息,并且假设此电子邮件用于通过普通方式(使用网络浏览器)阅读和发送消息,我不确定用户是否不能自己给消息加注星标,应用程序将其视为已读。 I need to do something like read all messages, which were received after some time or may be where id is greater than id of last read message. 我需要执行一些操作,例如读取所有消息,这些消息是在一段时间后收到的,或者可能是id大于上次读取的消息的id。 Is there a way to do it? 有办法吗?

You could store seconds from the epoch last time your checked for new messages, and then use that next time you get new messages. 您可以在上次检查新邮件时存储从纪元开始的秒数,然后在下次收到新邮件时使用该秒数。

Let's say you checked for new messages at Fri, 27 Apr 2018 12:00:00 GMT , you would then write this in the next request: 假设您在Fri, 27 Apr 2018 12:00:00 GMT检查了新消息,然后将其写入下一个请求中:

$gmail->users_messages->listUsersMessages('me',['maxResults' => 10, 'q' => "in:inbox after:1524830400"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM