0

我正在从 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();
}

此代码为 $msg->getHasAttachments(); 返回“null”;我希望它返回真或假。

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

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

4

1 回答 1

1

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

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

然后可以像这样确定消息是否包含附件:

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

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

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

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

 foreach($messages as $msg) {
    foreach($msg->getAttachments() as $attachment) {
        //...
    }
 }
于 2019-03-22T15:00:39.140 回答