Skip to content

Commit ac93576

Browse files
committed
Mailer configuration for Laravel 9
1 parent e736117 commit ac93576

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ build
22
composer.lock
33
docs
44
vendor
5-
coverage
5+
coverage
6+
.phpunit.result.cache
7+
.idea

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ language: php
33
php:
44
- 7.3
55
- 7.4
6+
- 8.0
7+
- 8.1
68

79
env:
810
matrix:

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"config": {
4343
"sort-packages": true
4444
},
45+
"minimum-stability": "dev",
4546
"extra": {
4647
"laravel": {
4748
"providers": [

src/HeloLaravelServiceProvider.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\View;
99
use Illuminate\Support\ServiceProvider;
1010
use Illuminate\Support\Str;
11+
use Symfony\Component\Mailer\Mailer as SymfonyMailer;
1112
use Swift_Mailer;
1213

1314
class HeloLaravelServiceProvider extends ServiceProvider
@@ -26,7 +27,7 @@ public function boot()
2627
Mail::swap($instance);
2728

2829
app()->instance(MailerContract::class, $instance);
29-
30+
3031
if ($this->app->runningInConsole()) {
3132
View::addNamespace('helo', __DIR__ . '/../resources/views');
3233
}
@@ -41,7 +42,7 @@ public function register()
4142
$this->commands([
4243
TestMailCommand::class,
4344
]);
44-
45+
4546
$this->publishes([
4647
__DIR__.'/../config/helo.php' => base_path('config/helo.php'),
4748
], 'config');
@@ -50,11 +51,15 @@ public function register()
5051
$this->mergeConfigFrom(__DIR__.'/../config/helo.php', 'helo');
5152

5253
$this->app->singleton(Mailer::class, function ($app) {
53-
if (version_compare($app->version(), '7.0.0', '<')) {
54+
$version = (int) Str::of($app->version())->explode('.')->first();
55+
56+
if ($version < 7) {
5457
return $this->createLaravel6Mailer($app);
58+
} elseif ($version < 9) {
59+
return $this->createLaravel7Mailer($app);
5560
}
5661

57-
return $this->createLaravel7Mailer($app);
62+
return $this->createLaravel9Mailer($app);
5863
});
5964
}
6065

@@ -112,6 +117,35 @@ protected function createLaravel7Mailer($app)
112117
return $mailer;
113118
}
114119

120+
protected function createLaravel9Mailer($app)
121+
{
122+
$defaultDriver = $app['mail.manager']->getDefaultDriver();
123+
$config = $this->getConfig($defaultDriver);
124+
125+
// We get Symfony Transport from Laravel 9 mailer
126+
$symfonyTransport = $app['mail.manager']->getSymfonyTransport();
127+
128+
// Once we have create the mailer instance, we will set a container instance
129+
// on the mailer. This allows us to resolve mailer classes via containers
130+
// for maximum testability on said classes instead of passing Closures.
131+
$mailer = new Laravel7Mailer(
132+
'smtp', $app['view'], $symfonyTransport, $app['events']
133+
);
134+
135+
if ($app->bound('queue')) {
136+
$mailer->setQueue($app['queue']);
137+
}
138+
139+
// Next we will set all of the global addresses on this mailer, which allows
140+
// for easy unification of all "from" addresses as well as easy debugging
141+
// of sent messages since they get be sent into a single email address.
142+
foreach (['from', 'reply_to', 'to', 'return_path'] as $type) {
143+
$this->setGlobalAddress($mailer, $config, $type);
144+
}
145+
146+
return $mailer;
147+
}
148+
115149
protected function getConfig($name = 'smtp')
116150
{
117151
return $this->app['config']['mail.driver']

0 commit comments

Comments
 (0)