From 408c5351bff988c7b5e8b5db9e3e7447c05aa0ce Mon Sep 17 00:00:00 2001 From: FreeScout Date: Thu, 14 Jan 2021 23:58:14 -0800 Subject: [PATCH] Do not send multiple emails to customers when saving outgoing reply to IMAP folder fails - closes #984 --- app/Jobs/SendReplyToCustomer.php | 51 ++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/app/Jobs/SendReplyToCustomer.php b/app/Jobs/SendReplyToCustomer.php index 8d7ad0f1c..d988a2098 100644 --- a/app/Jobs/SendReplyToCustomer.php +++ b/app/Jobs/SendReplyToCustomer.php @@ -253,31 +253,38 @@ public function handle() $imap_sent_folder = $mailbox->imap_sent_folder; if ($imap_sent_folder) { - $client = \MailHelper::getMailboxClient($mailbox); - $client->connect(); - - $envelope['from'] = $mailbox->getMailFrom()['address']; - $envelope['to'] = $this->customer_email; - $envelope['subject'] = 'Re: ' . $this->conversation->subject; - - // Get penultimate email Message-Id if reply - if (!$new && !empty($last_customer_thread) && $last_customer_thread->message_id) { - $envelope['custom_headers'] = [ - 'In-Reply-To: <'.$last_customer_thread->message_id.'>', - 'References: <'.$last_customer_thread->message_id.'>' - ]; - } - - $part1['type'] = TYPETEXT; - $part1['subtype'] = 'html'; - $part1['contents.data'] = $reply_mail->render(); - try { - $folder = $client->getFolder($imap_sent_folder); - $folder->appendMessage(imap_mail_compose($envelope, [$part1]), '\Seen', now()->format('d-M-Y H:i:s O')); + $client = \MailHelper::getMailboxClient($mailbox); + $client->connect(); + + $envelope['from'] = $mailbox->getMailFrom()['address']; + $envelope['to'] = $this->customer_email; + $envelope['subject'] = 'Re: ' . $this->conversation->subject; + + // Get penultimate email Message-Id if reply + if (!$new && !empty($last_customer_thread) && $last_customer_thread->message_id) { + $envelope['custom_headers'] = [ + 'In-Reply-To: <'.$last_customer_thread->message_id.'>', + 'References: <'.$last_customer_thread->message_id.'>' + ]; + } + + $part1['type'] = TYPETEXT; + $part1['subtype'] = 'html'; + $part1['contents.data'] = $reply_mail->render(); + + try { + $folder = $client->getFolder($imap_sent_folder); + $folder->appendMessage(imap_mail_compose($envelope, [$part1]), '\Seen', now()->format('d-M-Y H:i:s O')); + } catch (\Exception $e) { + // Just log error and continue. + \Helper::logException($e, 'Could not save outgoing reply to the IMAP folder, IMAP folder not found: '.$imap_sent_folder.' - '); + //$this->saveToSendLog('['.date('Y-m-d H:i:s').'] Could not save outgoing reply to the IMAP folder: '.$imap_sent_folder); + } } catch (\Exception $e) { // Just log error and continue. - $this->saveToSendLog('['.date('Y-m-d H:i:s').'] Could not get mailbox IMAP folder: '.$imap_sent_folder); + //$this->saveToSendLog('['.date('Y-m-d H:i:s').'] Could not get mailbox IMAP folder: '.$imap_sent_folder); + \Helper::logException($e, 'Could not save outgoing reply to the IMAP folder: '.$imap_sent_folder.' - '); } }