Skip to content

Commit

Permalink
Show SMTP communication details when testing mail sending - closes #3626
Browse files Browse the repository at this point in the history
  • Loading branch information
freescout-help-desk committed Dec 27, 2023
1 parent ed6911a commit 6d3f1bb
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
15 changes: 10 additions & 5 deletions app/Http/Controllers/MailboxesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,17 +622,22 @@ public function ajax(Request $request)
}

if (!$response['msg']) {
$test_result = false;
$test_result = [
'status' => 'error'
];

try {
$test_result = \App\Misc\Mail::sendTestMail($request->to, $mailbox);
$test_result = \MailHelper::sendTestMail($request->to, $mailbox);
} catch (\Exception $e) {
$response['msg'] = $e->getMessage();
$test_result['msg'] = $e->getMessage();
}

if (!$test_result && !$response['msg']) {
$response['msg'] = __('Error occurred sending email. Please check your mail server logs for more details.');
if ($test_result['status'] == 'error') {
$response['msg'] = $test_result['msg']
?: __('Error occurred sending email. Please check your mail server logs for more details.');
}

$response['log'] = $test_result['log'] ?? '';
}

if (!$response['msg']) {
Expand Down
13 changes: 9 additions & 4 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,22 @@ public function ajax(Request $request)
}

if (!$response['msg']) {
$test_result = false;
$test_result = [
'status' => 'error'
];

try {
$test_result = \MailHelper::sendTestMail($request->to);
} catch (\Exception $e) {
$response['msg'] = $e->getMessage();
$test_result['msg'] = $e->getMessage();
}

if (!$test_result && !$response['msg']) {
$response['msg'] = __('Error occurred sending email. Please check your mail server logs for more details.');
if ($test_result['status'] == 'error') {
$response['msg'] = $test_result['msg']
?: __('Error occurred sending email. Please check your mail server logs for more details.');
}

$response['log'] = $test_result['log'] ?? '';
}

if (!$response['msg']) {
Expand Down
26 changes: 22 additions & 4 deletions app/Misc/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,29 @@ public static function getSystemMailDriver()
return Option::get('mail_driver', 'mail');
}

public static function registerSmtpLogger()
{
$logger = new \Swift_Plugins_Loggers_ArrayLogger();
\Mail::getSwiftMailer()->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));

return $logger;
}

/**
* Send test email from mailbox.
*/
public static function sendTestMail($to, $mailbox = null)
{
$result = [
'status' => 'success',
'msg' => '',
'log' => '',
];

if ($mailbox) {
// Configure mail driver according to Mailbox settings
\MailHelper::setMailDriver($mailbox);
$smtp_logger = self::registerSmtpLogger();

$status_message = '';

Expand All @@ -302,6 +317,7 @@ public static function sendTestMail($to, $mailbox = null)
} else {
// System email
\MailHelper::setSystemMailDriver();
$smtp_logger = self::registerSmtpLogger();

$status_message = '';

Expand All @@ -317,15 +333,17 @@ public static function sendTestMail($to, $mailbox = null)
if (\Mail::failures() || $status_message) {
SendLog::log(null, null, $to, SendLog::MAIL_TYPE_TEST, SendLog::STATUS_SEND_ERROR, null, null, $status_message);
if ($status_message) {
throw new \Exception($status_message, 1);
} else {
return false;
$result['msg'] = $status_message;
}
$result['status'] = 'error';
$result['log'] = $smtp_logger->dump();
} else {
SendLog::log(null, null, $to, SendLog::MAIL_TYPE_TEST, SendLog::STATUS_ACCEPTED);

return true;
$result['status'] = 'success';
}

return $result;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4387,6 +4387,9 @@ a.disabled:focus {
padding: 4px 6px 4px 6px;
vertical-align: inherit;
}
#send_test_log {
max-height: 200px;
}
.glyphicon-spin {
-webkit-animation: spin 1000ms infinite linear;
animation: spin 1000ms infinite linear;
Expand Down
8 changes: 8 additions & 0 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ function mailboxConnectionInit(out_method_smtp)
$('#send-test-trigger').click(function(event) {
var button = $(this);
button.button('loading');
$('#send_test_log').addClass('hidden');
fsAjax(
{
action: 'send_test',
Expand All @@ -668,6 +669,9 @@ function mailboxConnectionInit(out_method_smtp)
showFloatingAlert('success', Lang.get("messages.email_sent"));
} else {
showAjaxError(response, true);
if (typeof(response.log) != "undefined" && response.log) {
$('#send_test_log').removeClass('hidden').text(response.log);
}
}
button.button('reset');
},
Expand Down Expand Up @@ -786,6 +790,7 @@ function mailSettingsInit()
$('#send-test-trigger').click(function(event) {
var button = $(this);
button.button('loading');
$('#send_test_log').addClass('hidden');
fsAjax(
{
action: 'send_test',
Expand All @@ -797,6 +802,9 @@ function mailSettingsInit()
showFloatingAlert('success', Lang.get("messages.email_sent"));
} else {
showAjaxError(response);
if (typeof(response.log) != "undefined" && response.log) {
$('#send_test_log').removeClass('hidden').text(response.log);
}
}
button.button('reset');
},
Expand Down
1 change: 1 addition & 0 deletions resources/views/mailboxes/connection.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
</span>
</div>
<div class="form-help">{!! __("Make sure to save settings before testing.") !!}</div>
<pre class="alert alert-danger hidden" id="send_test_log"></pre>
</div>
</div>
<hr/>
Expand Down
1 change: 1 addition & 0 deletions resources/views/settings/emails.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
</span>
</div>
<div class="form-help">{!! __("Make sure to save settings before testing.") !!}</div>
<pre class="alert alert-danger hidden" id="send_test_log"></pre>
</div>
</div>

Expand Down

0 comments on commit 6d3f1bb

Please sign in to comment.