Finished the test e-mail settings functionality and introduced TLS encryption. Tested with Office 365.

This commit is contained in:
Andy Heathershaw 2016-09-06 13:26:02 +01:00
parent c7a56d1753
commit 2a1311b2ba
11 changed files with 178 additions and 10 deletions

View File

@ -9,9 +9,13 @@ use App\Facade\UserConfig;
use App\Helpers\ConfigHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\SaveSettingsRequest;
use App\Mail\TestMailConfig;
use App\Photo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
class DefaultController extends Controller
{
@ -30,15 +34,23 @@ class DefaultController extends Controller
public function saveSettings(SaveSettingsRequest $request)
{
$passwordKeys = [
'smtp_password'
];
$checkboxKeys = [
'allow_self_registration',
'require_email_verification',
'smtp_encryption'
];
$updateKeys = [
'app_name',
'date_format',
'sender_address',
'sender_name',
'smtp_server',
'smtp_port',
'smtp_username',
'smtp_password',
'theme'
];
@ -47,7 +59,24 @@ class DefaultController extends Controller
foreach ($updateKeys as $key)
{
$config = UserConfig::getOrCreateModel($key);
// Bit of a hack when the browser returns an empty password field - meaning the user didn't change it
// - don't touch it!
if (
$key == 'smtp_password' &&
strlen($config->value) > 0 &&
strlen($request->request->get($key)) == 0 &&
strlen($request->request->get('smtp_username')) > 0
)
{
continue;
}
$config->value = $request->request->get($key);
if (in_array($key, $passwordKeys))
{
$config->value = encrypt($config->value);
}
if (isset($defaults[$key]) && $defaults[$key] == $config->value)
{
@ -102,4 +131,48 @@ class DefaultController extends Controller
'theme_names' => $themeNamesLookup
]);
}
public function testMailSettings(SaveSettingsRequest $request)
{
try
{
$validKeys = [
'sender_address',
'sender_name',
'smtp_server',
'smtp_port',
'smtp_username',
'smtp_password',
'smtp_encryption'
];
$config = $request->only($validKeys);
/** @var \Swift_Mailer $swiftMailer */
$swiftMailer = resolve('mailer')->getSwiftMailer();
/** @var \Swift_SmtpTransport $transport */
$transport = $swiftMailer->getTransport();
$transport->setHost($config['smtp_server']);
$transport->setPort(intval($config['smtp_port']));
$transport->setUsername($config['smtp_username']);
$transport->setPassword($config['smtp_password']);
if (isset($config['smtp_encryption']) && strtolower($config['smtp_encryption']) == 'on')
{
$transport->setEncryption('tls');
}
else
{
$transport->setEncryption('');
}
Mail::to(Auth::user())->send(new TestMailConfig($config['sender_name'], $config['sender_address']));
return response()->json(array('is_successful' => true, 'message' => 'sent_successfully'));
}
catch (\Exception $ex)
{
return response()->json(array('is_successful' => false, 'message' => $ex->getMessage()));
}
}
}

View File

@ -26,6 +26,8 @@ class SaveSettingsRequest extends FormRequest
return [
'app_name' => 'required|max:255',
'date_format' => 'required',
'smtp_server' => 'required',
'smtp_port' => 'required:integer'
];
}
}

View File

@ -2,23 +2,28 @@
namespace App\Mail;
use App\Facade\Theme;
use App\Facade\UserConfig;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestMailConfig extends Mailable
{
use Queueable, SerializesModels;
private $senderAddress;
private $senderName;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
public function __construct($senderName, $senderAddress)
{
//
$this->senderName = $senderName;
$this->senderAddress = $senderAddress;
}
/**
@ -28,8 +33,11 @@ class TestMailConfig extends Mailable
*/
public function build()
{
return $this->from(UserConfig::get('sender_address'), UserConfig::get('sender_name'))
->subject(trans('email.test_email_subject', ['app_name' => UserConfig::get('app_name')]))
->view(Theme::viewName('email.test_email'));
$subject = trans('email.test_email_subject', ['app_name' => UserConfig::get('app_name')]);
return $this->from($this->senderAddress, $this->senderName)
->subject($subject)
->view(Theme::viewName('email.test_email'))
->with(['subject' => $subject]);
}
}

View File

@ -86,6 +86,11 @@ class AppServiceProvider extends ServiceProvider
$transport->setHost(UserConfig::get('smtp_server'));
$transport->setPort(intval(UserConfig::get('smtp_port')));
$transport->setUsername(UserConfig::get('smtp_username'));
$transport->setPassword(UserConfig::get('smtp_password'));
$transport->setPassword(decrypt(UserConfig::get('smtp_password')));
if (UserConfig::get('smtp_encryption'))
{
$transport->setEncryption('tls');
}
}
}

1
public/ripple.svg Normal file
View File

@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><svg width='32px' height='32px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" class="uil-ripple"><rect x="0" y="0" width="100" height="100" fill="none" class="bk"></rect><g> <animate attributeName="opacity" dur="2s" repeatCount="indefinite" begin="0s" keyTimes="0;0.33;1" values="1;1;0"></animate><circle cx="50" cy="50" r="40" stroke="#afafb7" fill="none" stroke-width="6" stroke-linecap="round"><animate attributeName="r" dur="2s" repeatCount="indefinite" begin="0s" keyTimes="0;0.33;1" values="0;22;44"></animate></circle></g><g><animate attributeName="opacity" dur="2s" repeatCount="indefinite" begin="1s" keyTimes="0;0.33;1" values="1;1;0"></animate><circle cx="50" cy="50" r="40" stroke="#e07678" fill="none" stroke-width="6" stroke-linecap="round"><animate attributeName="r" dur="2s" repeatCount="indefinite" begin="1s" keyTimes="0;0.33;1" values="0;22;44"></animate></circle></g></svg>

After

Width:  |  Height:  |  Size: 973 B

View File

@ -4,6 +4,10 @@
font-family: Raleway, sans-serif;
}
body {
padding-bottom: 40px;
}
.album-index img {
max-width: 100%;
}

View File

@ -14,6 +14,7 @@ return [
'settings_link' => 'Settings',
'settings_save_action' => 'Update Settings',
'settings_saved_message' => 'The settings were updated successfully.',
'settings_test_email_action' => 'Send a test e-mail',
'settings_title' => 'Settings',
'stats_albums' => 'album|albums',
'stats_panel' => 'Statistics',

View File

@ -20,6 +20,7 @@
{{-- Tab panes --}}
<div class="tab-content">
{{-- General --}}
<div role="tabpanel" class="tab-pane active" id="general-tab">
<div class="form-group">
{!! Form::label('app_name', 'Gallery name:', ['class' => 'control-label']) !!}
@ -45,6 +46,7 @@
</div>
</div>
{{-- E-mail --}}
<div role="tabpanel" class="tab-pane" id="email-tab">
<div class="form-group">
{!! Form::label('sender_name', 'Sender name:', ['class' => 'control-label']) !!}
@ -58,7 +60,7 @@
<hr/>
<p style="margin-bottom: 15px;">Configure your SMTP server using the settings below.</p>
<p style="margin-bottom: 15px;">Configure your SMTP server using the settings below. If your server does not require authentication, leave the Username and Password fields empty.</p>
<div class="form-group">
{!! Form::label('smtp_server', 'Hostname:', ['class' => 'control-label']) !!}
@ -77,10 +79,32 @@
<div class="form-group">
{!! Form::label('smtp_password', 'Password:', ['class' => 'control-label']) !!}
{!! Form::text('smtp_password', old('smtp_password'), ['class' => 'form-control']) !!}
{!! Form::password('smtp_password', ['class' => 'form-control']) !!}
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="smtp_encryption" @if (UserConfig::get('smtp_encryption') == 1)checked="checked"@endif>
<strong>Requires encrypted connection</strong>
</label>
</div>
<div>
<button id="test-email-button" type="button" class="btn btn-primary">@lang('admin.settings_test_email_action')</button>
<div style="margin-top: 10px;">
<div class="alert alert-info" id="test-email-status" style="display: none;">
<img src="{{ asset('ripple.svg') }}"/> Testing e-mail settings...
</div>
<div class="alert" id="test-email-result" style="display: none;">
<p><strong id="test-email-result-summary"></strong> <span id="test-email-result-message"></span></p>
</div>
</div>
</div>
</div>
{{-- Security --}}
<div role="tabpanel" class="tab-pane" id="security-tab">
<div class="checkbox">
<label>
@ -110,4 +134,36 @@
</div>
</div>
</div>
@endsection
@endsection
@push('scripts')
<script type="text/javascript">
$(document).ready(function() {
$('#test-email-button').click(function() {
var data = $('form').serialize();
$('#test-email-status').show();
$.post('{{ route('admin.testMailSettings') }}', data, function(data)
{
$('#test-email-result').removeClass('alert-danger');
$('#test-email-result').removeClass('alert-success');
if (data.is_successful)
{
$('#test-email-result').addClass('alert-success');
$('#test-email-result-summary').html('Successful');
}
else
{
$('#test-email-result').addClass('alert-danger');
$('#test-email-result-summary').html('Failed');
}
$('#test-email-result-message').html(data.message);
$('#test-email-result').show();
$('#test-email-status').hide();
});
return false;
});
});
</script>
@endpush

View File

@ -0,0 +1,9 @@
@extends('themes.base.email_layout')
@section('title', $subject)
@section('content')
<p>Hi {{ Auth::user()->name }},</p>
<p>This e-mail confirms your e-mail settings are correct.</p>
<p>You can now press &quot;Save Settings&quot; on the Settings screen to save these settings.</p>
<p>Regards,<br/>{{ UserConfig::get('app_name') }}</p>
@endsection

View File

@ -0,0 +1,8 @@
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>

View File

@ -17,6 +17,7 @@ Auth::routes();
Route::group(['prefix' => 'admin'], function () {
Route::get('/', 'Admin\DefaultController@index')->name('admin');
Route::post('settings/save', 'Admin\DefaultController@saveSettings')->name('admin.saveSettings');
Route::post('settings/test-email', 'Admin\DefaultController@testMailSettings')->name('admin.testMailSettings');
Route::get('settings', 'Admin\DefaultController@settings')->name('admin.settings');
// Album management