TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-14-2013, 01:10 PM   #1 (permalink)
The Visitor
Newcomer 
 
Join Date: Jan 2013
Posts: 1
Thanks: 0
gunjansoni2002 is on a distinguished road
Terminal 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();
                }
gunjansoni2002 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Beginner Guide #1 Hershey Absolute Beginners 1 11-07-2011 10:42 PM
10 PHP Myths Dispelled Wildhoney General 9 06-15-2009 06:55 AM
PHP Script to Extract Email Address from any text sunilbhatia79 Tips & Tricks 5 06-11-2009 02:06 PM
email fill in form help? PHP sacred_tinker Absolute Beginners 19 05-28-2008 05:58 AM
PHP Script to Extract Email Address from any text sunilbhatia79 Show Off 5 11-15-2007 01:53 PM


All times are GMT. The time now is 10:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design