使用php-ews回复电子邮件

时间:2013-01-21 13:36:53

标签: php email exchangewebservices email-integration php-ews

我使用php-ews阅读交换邮箱。但是,我想从收件箱中选择一封电子邮件并回复该电子邮件,并附上电子邮件历史记录并发送回复。以下是我用于从收件箱中获取特定电子邮件的代码。

$ews = new ExchangeWebServices($account_array['server'], $account_array['username'], $account_array['password'], ExchangeWebServices::VERSION_2010_SP1);
$message_id = 'AAMkADU3ZDdmZmY3LWI3OGMtNDRmMy1hYTdlLTBlZjkwOGE3NTU5MwBGAAAAAADkJRKCdlaES7sRqf3veO/UBwCgoREk6zyqQqi6KC/gJMy0AAAAvGoCAACgoREk6zyqQqi6KC/gJMy0AAAAvGpQAAA=';
$change_id = 'CQAAABYAAACgoREk6zyqQqi6KC/gJMy0AAAAvMEZ';

$request = new EWSType_GetItemType();
$request -> ItemShape = new EWSType_ItemResponseShapeType();
$request -> ItemShape -> BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;

$request -> ItemShape -> BodyType = EWSType_BodyTypeResponseType::HTML;

$body_property = new EWSType_PathToUnindexedFieldType();
$body_property -> FieldURI = 'item:Body';
$request -> ItemShape -> AdditionalProperties = new EWSType_NonEmptyArrayOfPathsToElementType();
$request -> ItemShape -> AdditionalProperties -> FieldURI = array($body_property);

$request -> ItemIds = new EWSType_NonEmptyArrayOfBaseItemIdsType();
$request -> ItemIds -> ItemId = array();

$message_item = new EWSType_ItemIdType();
$message_item -> Id = trim($message_id);
$request -> ItemIds -> ItemId[] = $message_item;

$response = $ews -> GetItem($request);
//print '<pre>' . print_r($response, true) . '</pre><hr/>';
$message = $response -> ResponseMessages -> GetItemResponseMessage -> Items -> Message;

print '<pre>' . print_r($message, true) . '</pre><hr/>';

现在我已收到要回复的消息,如何进一步处理并起草回复消息并为此电子邮件创建回复项目。

我已经搜索了Google,但没有运气。

钻进php-ews类几个小时,我看了EWSType_ReplyAllToItemType,EWSType_PostReplyItemType,EWSType_PostReplyItemBaseType等,但无法理解如何使用这些代码。

请帮帮我们!任何帮助将不胜感激。

我确信任何对这篇文章的回复都会有所帮助,因为没有论坛可以讨论 对这个。 :)

谢谢。

2 个答案:

答案 0 :(得分:3)

  

最后找到了关于如何使用PHP-EWS回复电子邮件的答案。

首先,我们需要修改EWSType / MessageType.php并在关闭类之前在类的末尾添加以下行:

public $NewBodyContent;

,回复功能如下:

Public function replyToMessage($id,$changeKey)
    {
        $ews = new ExchangeWebServices($this->server_url, $this->username, $this->password, ExchangeWebServices::VERSION_2010_SP1);

        //$msg = new EWSType_ReplyAllToItemType();
        $msg = new EWSType_MessageType();

//In Case you need to add anyone in CC
        $cc = new EWSType_ArrayOfRecipientsType();
        $cc->Mailbox = new EWSType_EmailAddressType();
        $cc->Mailbox->EmailAddress = 'emailaddresshere';
        $cc->Mailbox->Name = 'displaynamehere';
        $msg->CcRecipients = $cc;

        $msg->ReferenceItemId = new EWSType_ItemIdType();
        $msg->ReferenceItemId->Id = $id;
        $msg->ReferenceItemId->ChangeKey = $changeKey;

        $msg->NewBodyContent = new EWSType_BodyType();
        $msg->NewBodyContent->BodyType = 'HTML';
        $msg->NewBodyContent->_ = 'HTML Content Goes Here';

        $msgRequest = new EWSType_CreateItemType();
        $msgRequest->Items = new EWSType_NonEmptyArrayOfAllItemsType();
        $msgRequest->Items->ReplyAllToItem = $msg;
        $msgRequest->MessageDisposition = 'SendAndSaveCopy';
        $msgRequest->MessageDispositionSpecified = TRUE;

        $response = $ews->CreateItem($msgRequest);

        return $response->ResponseMessages->CreateItemResponseMessage->ResponseCode;

    }

这将发送对指定ID和ChangeKey的回复

答案 1 :(得分:1)

您是否可以简单地将新电子邮件发送到您要回复的电子邮件的回复地址?

https://github.com/jamesiarmes/php-ews/wiki/Email:-Send-Email

将主题设置为Re: <subject of previous message>

然后简单地将原始电子邮件的正文包含在回复正文下方?