01-14-2013, 01:10 PM
|
#1 (permalink)
|
|
The Visitor
Join Date: Jan 2013
Posts: 1
Thanks: 0
|
Replying to an email using php exchange web services
i am using php-ews for accessing the Exchange e-mail server ( php-ews).
I am able to read all the e-mails from a user's inbox, however, could not reply to a particular e-mail. I tried googling for help but could not get one.
Please help me replying to the e-mail. My code for reading the e-mail is
Code:
$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();
}
|
|
|
|