Skip to content

NOW MANAGED ON CODEBERG

Mail: Set domain for EHLO based upon the APP_URL

Dan Brownec3dd856db6e
diff --git a/app/App/Providers/AppServiceProvider.php b/app/App/Providers/AppServiceProvider.php
index debba7944..5264c0dcc 100644
--- a/app/App/Providers/AppServiceProvider.php
+++ b/app/App/Providers/AppServiceProvider.php
@@ -65,6 +65,13 @@ class AppServiceProvider extends ServiceProvider
URL::forceScheme($isHttps ? 'https' : 'http');
}
+ // Set SMTP mail driver to use a local domain matching the app domain,
+ // which helps avoid defaulting to a 127.0.0.1 domain
+ if ($appUrl) {
+ $hostName = parse_url($appUrl, PHP_URL_HOST) ?: null;
+ config()->set('mail.mailers.smtp.local_domain', $hostName);
+ }
+
// Allow longer string lengths after upgrade to utf8mb4
Schema::defaultStringLength(191);
diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php
index 9ed68c8bd..2e9190dfd 100644
--- a/tests/Unit/ConfigTest.php
+++ b/tests/Unit/ConfigTest.php
@@ -122,6 +122,27 @@ class ConfigTest extends TestCase
});
}
+ public function test_app_url_changes_smtp_ehlo_host_on_mailer()
+ {
+ $getLocalDomain = function (): string {
+ /** @var EsmtpTransport $transport */
+ $transport = Mail::mailer('smtp')->getSymfonyTransport();
+ return $transport->getLocalDomain();
+ };
+
+ $this->runWithEnv(['APP_URL' => ''], function () use ($getLocalDomain) {
+ $this->assertEquals('[127.0.0.1]', $getLocalDomain());
+ });
+
+ $this->runWithEnv(['APP_URL' => 'https://example.com/cats/dogs'], function () use ($getLocalDomain) {
+ $this->assertEquals('example.com', $getLocalDomain());
+ });
+
+ $this->runWithEnv(['APP_URL' => 'http://beans.cat.example.com'], function () use ($getLocalDomain) {
+ $this->assertEquals('beans.cat.example.com', $getLocalDomain());
+ });
+ }
+
public function test_non_null_mail_encryption_options_enforce_smtp_scheme()
{
$this->checkEnvConfigResult('MAIL_ENCRYPTION', 'tls', 'mail.mailers.smtp.require_tls', true);