【问题标题】:Mark email as read using microsoft graph api使用 Microsoft Graph API 将电子邮件标记为已读
【发布时间】:2023-02-01 23:47:52
【问题描述】:

我正在编写一个将使用 Microsoft Graph api 的脚本(使用这个库https://github.com/microsoftgraph/msgraph-sdk-php

我设法连接并搜索了特定的电子邮件,下载了附件,但现在我需要将电子邮件标记为已读并设置标志,但我不知道如何操作。

到目前为止,我已经使用本教程 (https://learn.microsoft.com/en-us/graph/tutorials/php?tabs=aad) 来连接和阅读电子邮件。

 public static function getInbox() {
    $token = GraphHelper::getUserToken();
    GraphHelper::$userClient->setAccessToken($token);

    // Only request specific properties
    $select = '$select=from,isRead,receivedDateTime,subject,hasAttachments';
    // Sort by received time, newest first
    $orderBy = '$orderBy=receivedDateTime DESC';

    $filter = '$filter=isRead eq false';

   

    $requestUrl = '/me/mailFolders/inbox/messages?'.$filter.'&'.$select.'&'.$orderBy;
    $messages = GraphHelper::$userClient->createCollectionRequest('GET', $requestUrl)
                                   ->setReturnType(Model\Message::class)
                                   ->setPageSize(100)
                                   ->getPage();
    
    foreach ($messages as $message) {
        if(strpos($message->getSubject(), 'XML')!==false ){
            print('Message: '.$message->getSubject().PHP_EOL);echo PHP_EOL;
            $expand="microsoft.graph.itemattachment/item";
            
            $requestUrl = '/me/messages/'.$message->getId().'/attachments/?$expand=  '.$expand;
            
            $docDatas = GraphHelper::$userClient->createCollectionRequest('GET', $requestUrl)
                                ->setReturnType(Model\Message::class)
                                ->setPageSize(1)
                                ->getPage();
            
            $dat = $docDatas[0]->getProperties();
            
            //parseXmlOrder(base64_decode($dat['contentBytes']));

            $sendBody = array( 'isRead' => true );
            var_dump( GraphHelper::$userClient->createRequest('PATCH', '/me/messages/'.$message->getId())
                            ->attachBody($sendBody)
                            ->execute() );


            
        }
    }

}

这是我目前的代码。在函数的末尾,我试图设置 isRead 属性。

如果有人能给我一些我出错的建议,那将是惊人的,并帮助我停止用头撞墙。

谢谢,

【问题讨论】:

    标签: php microsoft-graph-api


    【解决方案1】:

    事实证明,授予读写权限确实有帮助。我只有读取权限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-15
      • 2018-10-13
      • 1970-01-01
      • 2022-10-14
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      相关资源
      最近更新 更多