0

我正在使用php-ews访问 Exchange 电子邮件服务器。

我能够阅读用户收件箱中的所有电子邮件,但是,我无法回复特定电子邮件。我尝试用谷歌搜索寻求帮助,但找不到。

这是我的代码:

$ews        = new ExchangeWebServices('serveraddress', 'username', 'password', ExchangeWebServices::VERSION_2010_SP1);
$message_id = $conversationid;
$change_id  = $changekey;
// Build the request for the parts.
$request = new EWSType_GetItemType();
$request->ItemShape = new EWSType_ItemResponseShapeType();
$request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES;
// You can get the body as HTML, text or "best".
$request->ItemShape->BodyType = EWSType_BodyTypeResponseType::HTML;

// Add the body property.
$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();

// Add the message to the request.
$message_item = new EWSType_ItemIdType();
$message_item->Id = trim($message_id);
$request->ItemIds->ItemId[] = $message_item;
try
{
    $response = $ews->GetItem($request);
    //print '<pre>' . print_r($response, true) . '</pre><hr/>';
    $message = $response->ResponseMessages->GetItemResponseMessage->Items->Message;

    $data['conversationid'] = $message_id;
    $data['changekey'] = $change_id;
    $data['displayname'] = $message->Sender->Mailbox->Name;
    $data['mailfrom'] = $message->Sender->Mailbox->EmailAddress;

    $data['mailto'] = '';
    if (isset($message->ToRecipients))
    {
        $tempto = $message->ToRecipients->Mailbox;
        if (is_array($tempto))
        {
            foreach ($tempto as $key => $value)
            {
                $data['mailto'] .= $value->EmailAddress . ';';
            }
        }
        else
        {
            $data['mailto'] .= $message->ToRecipients->Mailbox->EmailAddress . ';';
        }
    }

    $data['mailcc'] = '';
    if (isset($message->CcRecipients))
    {
        $tempcc = $message->CcRecipients->Mailbox;
        if (is_array($tempcc))
        {
            foreach ($tempcc as $key => $value)
            {
                $data['mailcc'] .= $value->EmailAddress . ';';
            }
        }
        else
        {
            $data['mailcc'] .= $message->CcRecipients->Mailbox->EmailAddress . ';';
        }
    }

    $this->load->model('update_mail_model');
    $id = $this->update_mail_model->getnextid();

    $save_dir = 'emailattachments' . DIRECTORY_SEPARATOR . $id;

    if (!is_dir($save_dir))
    {
        mkdir($save_dir);
    }

    $attcount = 0;

    if ($response->ResponseMessages->GetItemResponseMessage->ResponseCode == 'NoError' && $response->ResponseMessages->GetItemResponseMessage->ResponseClass == 'Success')
    {
        $message = $response->ResponseMessages->GetItemResponseMessage->Items->Message;

        if (!empty($message->Attachments->FileAttachment))
        {
            // FileAttachment attribute can either be an array or
            // instance of stdClass...
            $attachments = array();
            if (is_array($message->Attachments->FileAttachment) === FALSE)
            {
                $attachments[] = $message->Attachments->FileAttachment;
            }
            else
            {
                $attachments = $message->Attachments->FileAttachment;
            }

            $attid = '';

            foreach ($attachments as $attachment)
            {
                $request = new EWSType_GetAttachmentType();
                $request->AttachmentIds->AttachmentId = $attachment->AttachmentId;
                $response = $ews->GetAttachment($request);

                // Assuming response was successful ...
                $attachments = $response->ResponseMessages->GetAttachmentResponseMessage->Attachments;
                $content = $attachments->FileAttachment->Content;
                $att['attachmentid'] = $attachments->FileAttachment->AttachmentId->Id;
                $att['attachment_name'] = $attachments->FileAttachment->Name;
                $att['contenttype'] = $attachments->FileAttachment->ContentType;
                $att['contentid'] = $attachments->FileAttachment->ContentId;
                $att['contenturl'] = $save_dir . DIRECTORY_SEPARATOR . $attachment->Name;
                $att['mailid'] = $id;
                //print '<pre>'.print_r($attachments,TRUE).'</pre>';

                //print $save_dir . DIRECTORY_SEPARATOR . $attachment
                //->Name;

                file_put_contents($save_dir . DIRECTORY_SEPARATOR . $attachment->Name, $content);
                $attid .= $this->update_mail_model->updateattachment($att) . ',';
                $attcount = $attcount + 1;
            }
        }
        else
        {
            //echo "No attachments found\n";
        }
    }

    $messageType = new EWSType_MessageType();
    $messageType->IsRead = true;

    $path = new EWSType_PathToUnindexedFieldType();
    $path->FieldURI = 'message:IsRead';

    $setField = new EWSType_SetItemFieldType();
    $setField->Message = $messageType;
    $setField->FieldURI = $path;

    $u = new EWSType_ItemChangeType();
    $u->Updates = new EWSType_NonEmptyArrayOfItemChangeDescriptionsType();
    $u->Updates->SetItemField[] = $setField;
    $u->ItemId = new EWSType_ItemIdType();
    $u->ItemId->Id = $message_id;
    $u->ItemId->ChangeKey = $change_id;

    $updatedItems = new EWSType_NonEmptyArrayOfItemChangesType();
    $updatedItems->ItemChange = $u;

    $updateMessenger = new EWSType_UpdateItemType();
    $updateMessenger->ItemChanges = $updatedItems;
    $updateMessenger->MessageDisposition = 'SaveOnly';
    $updateMessenger->ConflictResolution = 'AutoResolve';
    //print 'Trying Now...';
    try
    {
        $update_response = $ews->UpdateItem($updateMessenger);
    }
    catch (Exception $e)
    {
        print $e->getMessage();
    }

    $data['subject'] = $message->Subject;
    $data['mailbody'] = $message->Body->_;
    $data['mailtime'] = $message->DateTimeReceived;
    $data['mailtime'] = str_replace("T", " ", $data['mailtime']);
    $data['mailtime'] = str_replace("Z", "", $data['mailtime']);
    $data['account'] = $account_array['username'];
    $data['accountid'] = $account_array['accountid'];
    $data['assignedto'] = 'false';
    $data['attachments'] = $attcount;
    $data['attachment_ids'] = $attid;
    $this->load->model('update_mail_model');
    $mailid = $this->update_mail_model->update_default($data);
    print '<code>EMail with ID <strong>' . $mailid . '</strong> updated successfully. E-Mail from <strong>' . $data['mailfrom'] . '</strong></code><hr/>';
}
catch (Exception $e)
{
    print 'Error for Conv ID ' . $message_id . ':<br/>' . $e->getMessage();
}
4

1 回答 1

1

终于找到了关于如何使用 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 发送回复

于 2013-08-27T12:46:11.257 回答