Reputation: 249
I'm developing a web interface for Exchange Web Services which should be able to save a mail item to eml format. I use PHP-EWS (https://github.com/jamesiarmes/php-ews) to establish a connection to the Exchange Server.
I know how such a file looks like, so I could download a mail item and generate an eml template with the data.
But I found this post: Save mail to msg file using EWS API. Colin talks about a mechanism which directly export a mail item into eml file. Is that possible in PHP, too?
Additionally I found another thing: https://github.com/jamesiarmes/php-ews/wiki/Email:-Set-Extended-MAPI-Properties. In this example somebody generates a mime content and set it to a new item. Is it possible to get the mime type (which looks like an eml file to me) for an existing item?
Thanks for any help!
Upvotes: 2
Views: 2092
Reputation:
To save a mail item in eml format you have to set the IncludeMimeContent property to true in the ItemShape element of the GetItem operation.
By doing so, you will get in the GetItem response a MimeContent element:
The MimeContent element contains the native Multipurpose Internet Mail Extensions (MIME) stream of an object that is represented in base64Binary format.
As an example, consider the following code:
<?php
function __autoload($class_name) {
$base_path = 'php-ews-master';
$include_file = $base_path . '/' . str_replace('_', '/', $class_name) . '.php';
return (file_exists($include_file) ? require_once $include_file : false);
}
/*
** Adjust these variables before running the script!
*/
$server = 'your_server';
$username = 'your_user';
$password = 'your_password';
$message_id = 'your_message_id';
$ews = new ExchangeWebServices($server, $username, $password);
//print_r($ews);
$request = new EWSType_GetItemType();
$request->ItemShape = new EWSType_ItemResponseShapeType();
$request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
$request->ItemShape->IncludeMimeContent = true;
$request->ItemIds = new EWSType_NonEmptyArrayOfBaseItemIdsType();
$request->ItemIds->ItemId = new EWSType_ItemIdType();
$request->ItemIds->ItemId->Id = $message_id;
$response = $ews->GetItem($request);
//echo '<pre>'.print_r($response, true).'</pre>';
if (($response->ResponseMessages->GetItemResponseMessage->ResponseCode == 'NoError') &&
($response->ResponseMessages->GetItemResponseMessage->ResponseClass == 'Success')) {
file_put_contents("test.eml", base64_decode($response->ResponseMessages->GetItemResponseMessage->Items->Message->MimeContent->_));
}
?>
Upvotes: 2