laravel - Retrieving e-mail attachments from outlook.com mailbox via EWS using PHP
Get the solution ↓↓↓I am developing an outlook add-in which is supposed to sync an email to another server for our use case. I was able to get the mail stored on the remote DB but the problem is with fetching attachments. Since an add-in can't itself send an attachment, I have to send the remote server some details such as (ews url, access token, attachment id etc) so that it can use that information the contact the outlook server for getting respective attachments. No matter how I tried it appears to be failing. This basically leaves me thinking does outlook.com supports EWS? Proper references are hard to find but some of those references say 'No'. But my belief is since outlook.com looks like a part of 'Exchange Online' (although free) then it should support EWS too. This is evident by the fact that I am able to retrieve an ewsUrl and an attachmentToken by executing getCallbackTokenAsync() method of the Office.context.mailbox object via my outlook add-in. The add-in documentation says:
"The Office.context.mailbox object provides the getCallbackTokenAsync function to get a token that the remote server can use to authenticate with the Exchange server."
So there it is. But when I try to execute a SOAP request against the ewsUrl with an XML payload (from my remote server), the outlook server endpoint (https://outlook.office365.com/EWS/Exchange.asmx) keeps returning an error 500 - internal server error. There's not much conclusive details as well in the response so I can track down if there's anything wrong with the request I send to it. I am really puzzled at this. Appreciate some help from somebody who has done this already. Below is the code block (Laravel) I execute for your reference:
// check for attachments
if(is_object($incoming->attachments))
{
// we have attachments
$attachments = $incoming->attachments->attachments;
$ewsToken = $incoming->attachments->attachmentToken;
$ewsUrl = $incoming->attachments->ewsUrl;
foreach($attachments as $k => $v){
Log::info('Attachment ID: '.$v->id);
$attachmentSoapRequest = <<<EOD
@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""https://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""https://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""
xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
<soap:Header>
<t:RequestServerVersion Version=""Exchange2013"" />
</soap:Header>
<soap:Body>
<GetAttachment xmlns=""http://schemas.microsoft.com/exchange/services/2006/messages""
xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
<AttachmentShape/>
<AttachmentIds>
<t:AttachmentId Id=""$v->id""/>
</AttachmentIds>
</GetAttachment>
</soap:Body>
</soap:Envelope>"
EOD;
try{
$response = Http::withBody($attachmentSoapRequest, 'text/xml')->withToken($ewsToken)->withOptions([
'debug' => true,
'allow_redirects' => false
])->post($ewsUrl);
// process response
if ($response->successful()) {
Log::info('received ews response!');
$responseEnvelope = $response->body();
Log::info($responseEnvelope);
} else {
$response->throw();
}
} catch (RequestException $e) {
Log::info('ews request error!');
Log::info($e->getMessage());
}
Log::info(print_r($response, true));
}
}
If somebody knows the answer, it doesn't has to be Laravel specific. Even a pure PHP solution is welcome.
Thanks.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function store() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.