2013-01-14 131 views
0

我使用php-ews訪問Exchange電子郵件服務器。回覆使用php exchange web服務的電子郵件

我可以閱讀用戶收件箱中的所有電子郵件,但是我無法回覆特定電子郵件。我嘗試了谷歌尋求幫助,但無法得到一個。

這裏是我的代碼:

$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(); 
} 
+2

更詳細的:你想解決哪些錯誤?錯誤消息在哪裏?你有什麼嘗試? –

+0

感謝您的回覆,我不知道回覆電子郵件的代碼。我剛剛發佈了用於閱讀電子郵件的代碼。我希望提前使用此代碼並將其擴展爲回覆電子郵件。 – Guns

+0

對不起,但EWS讓我困惑不已...... :( – Guns

回答

0

終於找到了如何使用REPLYTO 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

相關問題