Skip to content

Commit

Permalink
Do not send multiple emails to customers when saving outgoing reply t…
Browse files Browse the repository at this point in the history
…o IMAP folder fails - closes #984
  • Loading branch information
freescout-help-desk committed Jan 15, 2021
1 parent 6fd2952 commit 408c535
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions app/Jobs/SendReplyToCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.' - ');
}
}

Expand Down

0 comments on commit 408c535

Please sign in to comment.