PHP-EWS将电子邮件移动到文件夹

时间:2018-01-18 16:46:00

标签: php exchangewebservices php-ews

我正在使用php-ews获取附件并将​​其保存到特定目录,现在我需要将邮件移动到另一个文件夹中。我卡在这里:

$client = new Client($host, $username, $password, $version);

$request = new FindItemType();
$request->ParentFolderIds = new NonEmptyArrayOfBaseFolderIdsType();

// Return all message properties.
$request->ItemShape = new ItemResponseShapeType();
$request->ItemShape->BaseShape = DefaultShapeNamesType::ALL_PROPERTIES;
// Search in the user's inbox.
$folder_id = new DistinguishedFolderIdType();
$folder_id->Id = DistinguishedFolderIdNameType::INBOX;
$request->ParentFolderIds->DistinguishedFolderId[] = $folder_id;
$response = $client->FindItem($request);
// Iterate over the results, printing any error messages or message subjects.
$response_messages = $response->ResponseMessages->FindItemResponseMessage;



foreach ($response_messages as $response_message) {
     // Make sure the request succeeded.
     if ($response_message->ResponseClass != ResponseClassType::SUCCESS) {
         $code = $response_message->ResponseCode;
         $message = $response_message->MessageText;
         continue;
     } 

     $items = $response_message->RootFolder->Items->Message;
     foreach ($items as $item) {
         $subject = $item->Subject;
         $sender = $item->From->Mailbox->EmailAddress;
         //move mail item from folder "INBOX" to folder "DONE"
     }
}

2 个答案:

答案 0 :(得分:1)

您需要使用MoveItem操作。

创建一个类文件。将其保存为EWSType / MoveItemType.php

<?php
/**
 * Definition of the MoveItemType type
 *
 * @package php-ews
 * @subpackage Types
 */

/**
* Definition of the MoveItemType type
*/
class EWSType_MoveItemType extends EWSType
{
    /**
    * ToFolderId property
    *
    * @var EWSType_TargetFolderIdType
    */
   public $ToFolderId;

   /**
   * DistinguishedFolderId property
   * 
   * @var EWSType_DistinguishedFolderIdType
   */
   public $DistinguishedFolderId;

   /**
   * ItemIds property
   *
   * @var EWSType_NonEmptyArrayOfBaseItemIdsType
   */
   public $ItemIds;
}

然后在您的应用程序代码中执行以下操作:

// Generic connection sample code
$ews = new ExchangeWebServices($host, $username, $password);

$request = new EWSType_MoveItemType();

$request->ToFolderId->FolderId->Id = 'FOLDER_ITEM_ID_VALUE';
$request->ToFolderId->FolderId->ChangeKey = 'FOLDER_ITEM_CHANGE_KEY_VALUE';

$request->ItemIds->ItemId->Id = 'ITEM_ID_VALUE';
$request->ItemIds->ItemId->ChangeKey = 'ITEM_CHANGE_KEY_VALUE';

// Generic execution sample code
$response = $ews->MoveItem($request);
echo '<pre>'.print_r($response, true).'</pre>';

答案 1 :(得分:0)

这是使用php-ews 1.0的有效解决方案

您可以使用现有的类MoveItemType

    $request = new MoveItemType();

    $request->ItemIds = new NonEmptyArrayOfBaseItemIdsType();
    $item_id = new ItemIdType();
    $item_id->Id = 'foobarfoobar';
    $request->ItemIds->ItemId[] = $item_id;

    $request->ToFolderId = new TargetFolderIdType();
    $folder_id = new FolderIdType();
    $folder_id->Id = $this->treatedFolderId;
    $request->ToFolderId->FolderId = 'barbar';

    $response = $this->client->MoveItem($request);
    $response_messages = $response->ResponseMessages->MoveItemResponseMessage;
    foreach ($response_messages as $response_message) {
        // Make sure the request succeeded.
        if ($response_message->ResponseClass != ResponseClassType::SUCCESS) {
            // Error treatment .....
            continue;
        }
        $items = $response_message->Items->Message;
        foreach ($items as $item) {
            // ...
        }
    }