Skip to content

Commit

Permalink
Show SMTP Queue ID in Outgoing Emails for sent emails - closes #3330
Browse files Browse the repository at this point in the history
  • Loading branch information
freescout-help-desk committed Sep 5, 2023
1 parent 900bdfb commit bab3eb1
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 9 deletions.
18 changes: 18 additions & 0 deletions app/FailedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,22 @@ class FailedJob extends Model
* Automatically converted into Carbon dates.
*/
protected $dates = ['failed_at'];

public $payload_decoded = null;

public function getPayloadDecoded()
{
if ($this->payload_decoded !== null) {
return $this->payload_decoded;
}

$this->payload_decoded = json_decode($this->payload, true);

return $this->payload_decoded;
}

public function getCommand()
{
return \App\Job::getPayloadCommand($this->getPayloadDecoded());
}
}
3 changes: 3 additions & 0 deletions app/Http/Controllers/SecureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ function addCol($cols, $col)
$status .= '. Message-ID: '.$record->message_id;
}
}
if ($record->smtp_queue_id) {
$status .= '. SMTP ID: '.$record->smtp_queue_id;
}

$logs[] = [
'date' => $record->created_at,
Expand Down
19 changes: 12 additions & 7 deletions app/Jobs/SendReplyToCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Mail\ReplyToCustomer;
use App\SendLog;
use App\Thread;
use App\Misc\SwiftGetSmtpQueueId;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
Expand Down Expand Up @@ -199,10 +200,10 @@ public function handle()
\MailHelper::setMailDriver($mailbox, $this->last_thread->created_by_user, $this->conversation);

// https://github.com/freescout-helpdesk/freescout/issues/3330
// if (\MailHelper::$smtp_queue_id_plugin_registered) {
// \Mail::getSwiftMailer()->registerPlugin(new \App\Misc\SwiftGetSmtpQueueId());
// \MailHelper::$smtp_queue_id_plugin_registered = true;
// }
if (!\MailHelper::$smtp_queue_id_plugin_registered) {
\Mail::getSwiftMailer()->registerPlugin(new SwiftGetSmtpQueueId());
\MailHelper::$smtp_queue_id_plugin_registered = true;
}

$this->message_id = $this->last_thread->getMessageId($mailbox);
$headers['Message-ID'] = $this->message_id;
Expand Down Expand Up @@ -270,6 +271,8 @@ public function handle()
->cc($cc_array)
->bcc($bcc_array)
->send($reply_mail);

$smtp_queue_id = SwiftGetSmtpQueueId::$last_smtp_queue_id;
} catch (\Exception $e) {
// We come here in case SMTP server unavailable for example
if ($this->attempts() == 1) {
Expand Down Expand Up @@ -324,6 +327,8 @@ public function handle()
}
}

SwiftGetSmtpQueueId::$last_smtp_queue_id = null;

// Clean error message if email finally has been sent.
if ($this->last_thread->send_status == SendLog::STATUS_SEND_ERROR) {
$this->last_thread->send_status = null;
Expand Down Expand Up @@ -452,7 +457,7 @@ public function handle()
$this->failures = Mail::failures();

// Save to send log
$this->saveToSendLog();
$this->saveToSendLog('', $smtp_queue_id);
}

// Save an email to IMAP folder.
Expand Down Expand Up @@ -504,7 +509,7 @@ public function failed(\Exception $e)
/**
* Save emails to send log.
*/
public function saveToSendLog($error_message = '')
public function saveToSendLog($error_message = '', $smtp_queue_id = '')
{
foreach ($this->recipients as $recipient) {
if (in_array($recipient, $this->failures)) {
Expand All @@ -519,7 +524,7 @@ public function saveToSendLog($error_message = '')
} else {
$customer_id = null;
}
SendLog::log($this->last_thread->id, $this->message_id, $recipient, SendLog::MAIL_TYPE_EMAIL_TO_CUSTOMER, $status, $customer_id, null, $status_message);
SendLog::log($this->last_thread->id, $this->message_id, $recipient, SendLog::MAIL_TYPE_EMAIL_TO_CUSTOMER, $status, $customer_id, null, $status_message, $smtp_queue_id);
}
}
}
5 changes: 5 additions & 0 deletions app/Misc/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class Mail
*/
public static $last_mail_config_hash = '';

/**
* Used to get SMTP queue id when sending emails to customers.
*/
public static $smtp_queue_id_plugin_registered = false;

/**
* Configure mail sending parameters.
*
Expand Down
19 changes: 19 additions & 0 deletions app/Misc/SwiftGetSmtpQueueId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Misc;

class SwiftGetSmtpQueueId implements \Swift_Events_ResponseListener
{
public static $last_smtp_queue_id = null;

public function responseReceived(\Swift_Events_ResponseEvent $evt)
{
$response_text = $evt->getResponse();
if (strpos($response_text, 'queued') !== false) {
preg_match("#queued as ([^\$\r\n ]+)[$\r\n]#", $response_text, $m);
if (!empty($m[1]) && trim($m[1])) {
self::$last_smtp_queue_id = trim($m[1]);
}
}
}
}
5 changes: 4 additions & 1 deletion app/SendLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function thread()
/**
* Save log record.
*/
public static function log($thread_id, $message_id, $email, $mail_type, $status, $customer_id = null, $user_id = null, $status_message = null)
public static function log($thread_id, $message_id, $email, $mail_type, $status, $customer_id = null, $user_id = null, $status_message = null, $smtp_queue_id = null)
{
$send_log = new self();
$send_log->thread_id = $thread_id;
Expand All @@ -103,6 +103,9 @@ public static function log($thread_id, $message_id, $email, $mail_type, $status,
$send_log->customer_id = $customer_id;
$send_log->user_id = $user_id;
$send_log->status_message = $status_message;
if ($smtp_queue_id) {
$send_log->smtp_queue_id = $smtp_queue_id;
}
$send_log->save();

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddSmtpQueueIdColumnToSendLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('send_logs', function (Blueprint $table) {
$table->text('smtp_queue_id')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('send_logs', function (Blueprint $table) {
$table->dropColumn('smtp_queue_id');
});
}
}
2 changes: 1 addition & 1 deletion resources/views/conversations/ajax_html/send_log.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<td>@if ($log->mail_type == App\SendLog::MAIL_TYPE_AUTO_REPLY) [{{ __('auto reply') }}] @else &nbsp; @endif</td>
<td>{{ App\User::dateFormat($log->created_at, 'M j, Y H:i:s') }}</td>
<td>
<span class="@if ($log->isErrorStatus())text-danger @elseif ($log->isSuccessStatus()) text-success @endif">{{ $log->getStatusName() }}</span>
<span class="@if ($log->isErrorStatus())text-danger @elseif ($log->isSuccessStatus()) text-success @endif">{{ $log->getStatusName() }}@if ($log->smtp_queue_id) (SMTP ID: {{ $log->smtp_queue_id }})@endif</span>
@if ($log->status_message)
<div class="text-help">{{ $log->status_message }}</div>
@endif
Expand Down

0 comments on commit bab3eb1

Please sign in to comment.