Receiving single mail via garethp / php-ews
code:
$api = MailAPI::withUsernameAndPassword($server, $username, $password);
$folder1 = $api->getFolderByDisplayName('PubFolder', DistinguishedFolderIdNameType::PUBLICFOLDERSROOT);
$subFolder1 = $api->getFolderByDisplayName($data['mailfolder'], $folder1->getFolderId());
$api->setFolderId($subFolder1->getFolderId());
$mail = $api->getMailItems($data['id']);
Mistake: Exception 'Error' with message 'Call to a member function toXmlObject() on string'
Description: I would like to receive one Mail item via id, however, since I need to call it from another page and load the content via JS, I cannot send the MailID object, which I can get using $singleMail->getItemId()
inside foreach. So I have to use $singleMail->getItemId()->getId()
which gives the ID as a string, however when trying to get mail via ID I get the above error.
So how should I proceed? Requesting all emails and loops until I find the ID again is not an option.
Am I using it getMailItems()
wrong? If so, please correct me.
Can I create the correct ID object?
Or is there an alternative way to request a unified mail via ID-String? If desired, I would look for a Manuel query building.
source to share
getMailItems
doesn't take an email id but a folder id and requires it to be an object ( $folderId
) for the first parameter and you give a string. If you go and check the source MailAPI::getMailItems
, you can see exactly why you are getting this error. On line 91 we see:
'FolderId' => $folderId->toXmlObject()
there your code is trying to call a member function from a string. I've searched a bit and I think you can filter mail by id using getMailItems
an optional parameter $options
.
First try setting the ItemId
mail id string value like this:
$mail = $api->getMailItems($subFolder1->getFolderId(), [
'ItemIds' => [
'ItemId' => $data['id'] // Or (new API\Type\ItemIdType($data['id']))
]
]);
If that doesn't work, try searching for mail again with this code:
$mail = $api->getItem(new API\Type\ItemIdType($data['id']));
I don't have an EWS to test this, but I think you will solve the problem.
source to share