我正在使用PHP Gmail API来查看未读电子邮件。当我找到一个时,我只想获得电子邮件主题的最后一条消息中的内容。所以,这是我在两个电子邮件帐户之间构建的示例线程:
==================================================
MESSAGE ID: 15e9f58a64c41d19
THREAD ID: 15e9f58a64c41d19
DATE: 1505911938000
MESSAGE:
This is an email. Deal with it
==================================================
MESSAGE ID: 15e9f60859f747dd
THREAD ID: 15e9f58a64c41d19
DATE: 1505912456000
MESSAGE:
It's okay. I forgive you
On Wed, Sep 20, 2017 at 8:52 AM, User1 <user1@gmail.com> wrote:
> This is an email. Deal with it
>
==================================================
MESSAGE ID: 15e9f60cdbebde04
THREAD ID: 15e9f58a64c41d19
DATE: 1505912471000
MESSAGE:
I don't care what you think. This is over
On 9/20/2017 9:00:57 AM, User 2 <user2@gmail.com> wrote:
It's okay. I forgive you
On Wed, Sep 20, 2017 at 8:52 AM, User 1 <user1@gmail.com [mailto:user1@gmail.com]> wrote:
This is an email. Deal with it
因此,每个====================之间是一个线程的单个消息。我得到这样的信息:
function printThreads($service, $thread)
{
// Print the labels in the user's account.
$messages = $service->users_threads->get('me', $thread);
foreach ($messages as $message)
{
$params = [];
$params['format'] = 'full';
$result = $service->users_messages->get('me', $message->getId(), $params);
$payload = $result->getPayload();
$parts = $payload->getParts();
if (isset($parts[0]))
{
$body = $parts[0]['body']->data;
$san = strtr($body, '-_', '+/');
$decode = base64_decode($san);
print "==================================================\n";
print "MESSAGE ID: " . $message->getId() . "\n";
print "THREAD ID: " . $message->getThreadId() . "\n";
print "DATE: " . $message->getInternalDate() . "\n\n";
print "MESSAGE:\n\n";
print $decode;
print "\n";
}
}
}
但是,从打印结果中可以看出,线程中的每条消息都附加在线程中的先前消息中。因此,为了使我的轮询应用程序正常工作,我想从线程中的最后一条消息中获取此信息:
“我不在乎你的想法。这已经结束了”
API是否允许我这样做?我查看了消息和线程的内容,但我没有看到任何内容。我的另一个选择是运行一个正则表达式并将文件拆分为“2017年9月20日上午8:52,星期三,User1写道:”,但我认为这不是非常可靠。