ms graph php sdk消息对象检查是否有附件失败

ms graph php sdk message object fails to check if there are attachments

我正在从 Outlook 帐户中检索邮件。我正在尝试从这些消息中获取内联文件和附件。

$graph = new Graph();
$graph->setAccessToken($this->getAccessToken());

$messageQueryParams = array (
    "$select" => "subject,receivedDateTime,from,sentDateTime,body,toRecipients,sender,uniqueBody,ccRecipients,bccRecipients,attachments",
    "$orderby" => "receivedDateTime DESC",
    "$top" => "200" 
);

$url = '/me/mailfolders/' . $folder . '/messages/delta';
$url_combiner = '?';

$getMessagesUrl = $url . $url_combiner . http_build_query($messageQueryParams);
$response = $graph->createRequest('GET', $getMessagesUrl)->execute();

$messages = $response->getResponseAsObject( \Microsoft\Graph\Model\Message::class );

foreach($messages as $msg) {
     echo $msg->getHasAttachments();
}

此代码 returns 'null' 用于 $msg->getHasAttachments();我希望它 return 是真还是假。

我从这个文件夹下载的邮件既有内联附件也有邮件附件,所以我正在寻找两者的解决方案。

(非常感谢指向 MS Graph PHP SDK 文档中特定点的回复。)

这是预期的行为,在您的情况下 getHasAttachments() 方法 returns null 因为 hasAttachments 不包含在 $select 查询选项中,因此 它不是从服务器请求的。需要明确包含,例如:

$messageQueryParams = array (
    "$select" => "hasAttachments,...",
    //another params are omitted for clarity
);

那么邮件是否包含附件可以这样判断:

foreach($messages as $msg) {
    if($msg->getHasAttachments() == true){
        //...
}

提供的示例还有一个问题,要检索 Message.attachments reference 属性(或关系 属性),它需要通过 $expand 查询字符串参数而不是 $select 指定(有关更多详细信息,请参阅 official documentation),如下所示:

$messageQueryParams = array (
     "$expand" => "attachments",
    //another params are omitted for clarity
); 

然后附件集合可以像这样迭代:

 foreach($messages as $msg) {
    foreach($msg->getAttachments() as $attachment) {
        //...
    }
 }