1

*,

I want to upload daily some .EML-Files to my Exchange Server 2013. So I found from Google a PHP class from James Iarmes calles PHP-EWS. https://github.com/jamesiarmes/php-ews

I tried some examples and I think, this class is good for me. There is only one problem: no example-code in this wiki and unfortunately I do not get it out :-(

Is there someone, who works with this class and can post a example to do uploads to Exchange-Servers using PHP-EWS?

Regards

AHF oliPro
  • 11
  • 1

2 Answers2

2

Based on the answer given by Michael up above, I can't tell you how you'd do it in jamesaires/php-ews, but I can tell you how to do it in my ews library, garethp/php-ews. I'd highly recommend you look in to my library instead, as it's maintained and PSR-4 compatible. Here's how you'd do it using Michael's method

<?php

require_once "vendor/autoload.php";

use jamesiarmes\PEWS\API\Type;
use jamesiarmes\PEWS\Mail\MailAPI;

$api = MailApi::withUsernameAndPassword('server', 'username', 'password');

$message = new Type\MessageType();
$message->setMimeContent(file_get_contents('./file.eml'));

//Set the message as not a draft using extended property types
$extended = new Type\ExtendedPropertyType();
$fieldUri = new Type\ExtendedFieldURI();
$fieldUri->setPropertyTag("0x0E07");
$fieldUri->setPropertyType(\jamesiarmes\PEWS\API\Enumeration\MapiPropertyTypeType::INTEGER);
$extended->setExtendedFieldURI($fieldUri);
$extended->setValue(1);
$message->addExtendedProperty($extended);

//Pass it to the Send Mail function, but to SaveOnly without actually sending the email
$itemId = $api->sendMail($message, array('MessageDisposition' => 'SaveOnly'));

$inbox = $api->getFolderByDistinguishedId('inbox');

//Move the mail message from the sent folder to whatever folder we want to send it to
$api->getClient()->MoveItem(Type::buildFromArray(array(
    'ToFolderId' => array('FolderId' => $inbox->getFolderId()->toArray()),
    'ItemIds' => array(
        'ItemId' => $itemId->toArray()
    )
)));
Gareth Parker
  • 5,012
  • 2
  • 18
  • 42
0

I've never worked with PHP-EWS but I do know that you can put a .eml into Exchange.You won't be using the UploadItem operation. You'll use the CreateItem operation. Here's the workflow:

  1. Create an email object.
  2. Set the MimeContent property with the contents of your .eml file.
  3. Set the PR_MESSAGE_FLAGS_msgflag_read property.
  4. Use the CreateItem operation, or whatever the client-side equivalent is in PHP-EWS.

I'm fairly confident that this will work as I remember writing this in the official documentation.

Michael Mainer
  • 3,387
  • 1
  • 13
  • 32
  • Hi, thanks for posting the method. I've posted some PHP code for using a php-ews library to implement this. I've actually been wanting to get in touch with you to update the documentation of php-ews on the exchange web services website, since the library it's currently linking to is unmaintained and doesn't follow modern practices. Is there any way I can get in touch with you to have a chat about it? – Gareth Parker Apr 28 '16 at 11:04
  • @GarethParker, sure, you can DM me on Twitter at MichaelMainer – Michael Mainer Apr 28 '16 at 16:41