Reputation: 2239
I am retrieving messages from an Outlook account. I am trying to get inline files and attachments from those messages.
$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();
}
This code returns 'null' for $msg->getHasAttachments(); I would expect it to return a true or false.
The messages I am downloading from this folder have both inline attachments and mail attachments, so I am looking for a solution to both.
(Responses that point to specific points in the MS Graph PHP SDK documentation are very appreciated.)
Upvotes: 1
Views: 591
Reputation: 59328
This is the expected behavior, in your case getHasAttachments()
method returns null
since hasAttachments
is not included in $select
query option and therefore it is not requested from a server. It needs to be explicitly included, for example:
$messageQueryParams = array (
"\$select" => "hasAttachments,...",
//another params are omitted for clarity
);
Then whether a message contains attachments or not could be determined like this:
foreach($messages as $msg) {
if($msg->getHasAttachments() == true){
//...
}
There is one more issue with the provided example, to retrieve Message.attachments
reference property (or relationship property) it needs to be specified via $expand
query string parameter instead of $select
(refer official documentation for a more details), like this:
$messageQueryParams = array (
"\$expand" => "attachments",
//another params are omitted for clarity
);
Then attachments collection could be iterated like this:
foreach($messages as $msg) {
foreach($msg->getAttachments() as $attachment) {
//...
}
}
Upvotes: 1