Skip to content

Commit dbb6c87

Browse files
committed
Mail Config: Updated how TLS is configured
After full review of current MAIL_ENCRYPTION usage in laravel and smyfony mailer, this updates the options in BookStack to be simplified and specific in usage: - Removed mail.mailers.smtp.encryption option since it did not actually affect anything in the current state of dependancies. - Updated MAIL_ENCRYPTION so values of tls OR ssl will force-enable tls via 'scheme' option with laravel passes to the SMTP transfport, which Smyfony uses as an indicator to force TLS. When MAIL_ENCRYPTION is not used, STARTTLS will still be attempted by symfony mailer. Updated .env files to refer to BookStack docs (which was updated for this) and to reflect correct default port. Related to #4342
1 parent 9ae17ef commit dbb6c87

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

.env.example

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ [email protected]
3737
# SMTP mail options
3838
# These settings can be checked using the "Send a Test Email"
3939
# feature found in the "Settings > Maintenance" area of the system.
40+
# For more detailed documentation on mail options, refer to:
41+
# https://www.bookstackapp.com/docs/admin/email-webhooks/#email-configuration
4042
MAIL_HOST=localhost
41-
MAIL_PORT=1025
43+
MAIL_PORT=587
4244
MAIL_USERNAME=null
4345
MAIL_PASSWORD=null
4446
MAIL_ENCRYPTION=null

.env.example.complete

+3-7
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,19 @@ DB_PASSWORD=database_user_password
6969
# certificate itself (Common Name or Subject Alternative Name).
7070
MYSQL_ATTR_SSL_CA="/path/to/ca.pem"
7171

72-
# Mail system to use
73-
# Can be 'smtp' or 'sendmail'
72+
# Mail configuration
73+
# Refer to https://www.bookstackapp.com/docs/admin/email-webhooks/#email-configuration
7474
MAIL_DRIVER=smtp
75-
76-
# Mail sending options
7775
7876
MAIL_FROM_NAME=BookStack
7977

80-
# SMTP mail options
8178
MAIL_HOST=localhost
82-
MAIL_PORT=1025
79+
MAIL_PORT=587
8380
MAIL_USERNAME=null
8481
MAIL_PASSWORD=null
8582
MAIL_ENCRYPTION=null
8683
MAIL_VERIFY_SSL=true
8784

88-
# Command to use when email is sent via sendmail
8985
MAIL_SENDMAIL_COMMAND="/usr/sbin/sendmail -bs"
9086

9187
# Cache & Session driver to use

app/Config/mail.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
* Do not edit this file unless you're happy to maintain any changes yourself.
99
*/
1010

11+
// Configured mail encryption method.
12+
// STARTTLS should still be attempted, but tls/ssl forces TLS usage.
13+
$mailEncryption = env('MAIL_ENCRYPTION', null);
14+
1115
return [
1216

1317
// Mail driver to use.
@@ -27,9 +31,9 @@
2731
'mailers' => [
2832
'smtp' => [
2933
'transport' => 'smtp',
34+
'scheme' => ($mailEncryption === 'tls' || $mailEncryption === 'ssl') ? 'smtps' : null,
3035
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
3136
'port' => env('MAIL_PORT', 587),
32-
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
3337
'username' => env('MAIL_USERNAME'),
3438
'password' => env('MAIL_PASSWORD'),
3539
'verify_peer' => env('MAIL_VERIFY_SSL', true),

tests/Unit/ConfigTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\Log;
66
use Illuminate\Support\Facades\Mail;
77
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
8+
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
89
use Tests\TestCase;
910

1011
/**
@@ -122,6 +123,45 @@ public function test_mail_disable_ssl_verification_alters_mailer()
122123
});
123124
}
124125

126+
public function test_non_null_mail_encryption_options_enforce_smtp_scheme()
127+
{
128+
$this->checkEnvConfigResult('MAIL_ENCRYPTION', 'tls', 'mail.mailers.smtp.scheme', 'smtps');
129+
$this->checkEnvConfigResult('MAIL_ENCRYPTION', 'ssl', 'mail.mailers.smtp.scheme', 'smtps');
130+
$this->checkEnvConfigResult('MAIL_ENCRYPTION', 'null', 'mail.mailers.smtp.scheme', null);
131+
}
132+
133+
public function test_smtp_scheme_and_certain_port_forces_tls_usage()
134+
{
135+
$isMailTlsForcedEnabled = function () {
136+
$transport = Mail::mailer('smtp')->getSymfonyTransport();
137+
/** @var SocketStream $stream */
138+
$stream = $transport->getStream();
139+
Mail::purge('smtp');
140+
return $stream->isTLS();
141+
};
142+
143+
config()->set([
144+
'mail.mailers.smtp.scheme' => null,
145+
'mail.mailers.smtp.port' => 587,
146+
]);
147+
148+
$this->assertFalse($isMailTlsForcedEnabled());
149+
150+
config()->set([
151+
'mail.mailers.smtp.scheme' => 'smtps',
152+
'mail.mailers.smtp.port' => 587,
153+
]);
154+
155+
$this->assertTrue($isMailTlsForcedEnabled());
156+
157+
config()->set([
158+
'mail.mailers.smtp.scheme' => '',
159+
'mail.mailers.smtp.port' => 465,
160+
]);
161+
162+
$this->assertTrue($isMailTlsForcedEnabled());
163+
}
164+
125165
/**
126166
* Set an environment variable of the given name and value
127167
* then check the given config key to see if it matches the given result.

0 commit comments

Comments
 (0)