jamesiarmes / php-ews

PHP Exchange Web Services
http://jamesarmes.com/php-ews/
MIT License
567 stars 302 forks source link

Unable to get InternetMessageId from GetItem #554

Open saranvarma opened 5 years ago

saranvarma commented 5 years ago

HI,

I am using this library in our product to send multiple emails to users. While sending the emails with cron, I am getting itemid and changekey from createitem method. By using that itemid, changekey we are sending the emails with senditem method.

After sending the email successfully, we are trying to get the Internetmessageid with getitem method by passing that mails itemid.But some items i am getting the Internetmessageid as empty.

we are trying to fetch the replies for particular email with Internetmessageid.so we need that Id.

Please refer the below code and let me know the workaround for this

try {

       $request = new SendItemType();
       $request->SaveItemToFolder = true;
       $request->ItemIds = new NonEmptyArrayOfBaseItemIdsType();

        $item = new ItemIdType();
        $item->Id = $api_response_mail_id;
         $item->ChangeKey = $change_key;
        $request->ItemIds->ItemId[] = $item;

          $send_folder = new TargetFolderIdType();
          $send_folder->DistinguishedFolderId = new DistinguishedFolderIdType();
           $send_folder->DistinguishedFolderId->Id = DistinguishedFolderIdNameType::SENT;
            $request->SavedItemFolderId = $send_folder;

            $response = $client->SendItem($request);

            $response_messages = $response->ResponseMessages->SendItemResponseMessage;
                            foreach ($response_messages as $response_message) {
                                    if ($response_message->ResponseClass != ResponseClassType::SUCCESS) {
                                        echo $code = $response_message->ResponseCode;
                                        echo $message = $response_message->MessageText;
                                        $success_message = "Message failed to send";
                                        continue;
                                    }
                                    $success_message = "Message sent successfully.";
                                    $exchanger_send_mail_error_status = false;
                                }

                                if (!$exchanger_send_mail_error_status) {

                                    $request = new \jamesiarmes\PhpEws\Request\GetItemType();
                                    $request->ItemShape = new ItemResponseShapeType();
                                    $request->ItemShape->BaseShape = DefaultShapeNamesType::ALL_PROPERTIES;
                                    $request->ItemIds = new NonEmptyArrayOfBaseItemIdsType();

                                    $item = new ItemIdType();
                                    $item->Id = $api_response_mail_id;
                                    $request->ItemIds->ItemId[] = $item;

                                    $response = $client->GetItem($request);

                                    $message_details =  $response->ResponseMessages->GetItemResponseMessage[0]->Items->Message;

                                $message_id = ArrayHelper::getValue($message_details, '0.InternetMessageId', '');

                               $thread_references = ArrayHelper::getValue($message_details, '0.References', '');

                               $threadid = '';

                                    if ($thread_references) {

                                        $thread_all_references = explode(' ', $thread_references);

                                        if (!empty($thread_all_references)) {
                                            $threadid = current($thread_all_references);
                                        }
                                    }

                                    if (empty($threadid)) {

                                        if (!empty($thread_id)) {
                                            $threadid = $thread_id;
                                        } else {
                                            $threadid = $message_id;
                                        }
                                    }

                                    $message_id = str_replace(['<', '>'], '', $message_id);

                                    $thread_id = str_replace(['<', '>'], '', $threadid);
                                }

                                $response_code = '200';
                           $success_message = 'Email sent successfully';
                            } catch (\Exception $e) {
                                $success_message = $e->getMessage();
                                $response_code = '504';
                            }

Sometimes we are not getting $message_id. will use this id in future to fetch replies to this particular email

winds1983 commented 3 years ago

I have same questions, how to get this? could you share you workaround? @saranvarma