0

Exchange 電子メール サーバーへのアクセスにphp-ewsを使用しています。

ユーザーの受信トレイからすべてのメールを読むことができますが、特定のメールに返信できませんでした。グーグルで助けを求めてみましたが、見つけることができませんでした。

これが私のコードです:

$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 に答える