简体   繁体   中英

Mark email as read using microsoft graph api

I am writing a script that will use Microsoft Graph api ( using this library https://github.com/microsoftgraph/msgraph-sdk-php )

I managed to connect and search for the specific email, download the attachment but now I need to mark the email as read and set the flag but I have no idea how.

So far I have used this tutorial ( https://learn.microsoft.com/en-us/graph/tutorials/php?tabs=aad ) in order to connect and read the emails.

 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() );


            
        }
    }

}

This is the code I have at the moment. Right at the end of the function I am trying to set the isRead attribute.

If someone could give me some advice where I am going wrong that would be amazing and help me stop banging my head against the wall.

Thanks,

Turns out giving readwrite persmissions does help. I only had read permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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