8
8
use Illuminate \Support \Facades \View ;
9
9
use Illuminate \Support \ServiceProvider ;
10
10
use Illuminate \Support \Str ;
11
+ use Symfony \Component \Mailer \Mailer as SymfonyMailer ;
11
12
use Swift_Mailer ;
12
13
13
14
class HeloLaravelServiceProvider extends ServiceProvider
@@ -26,7 +27,7 @@ public function boot()
26
27
Mail::swap ($ instance );
27
28
28
29
app ()->instance (MailerContract::class, $ instance );
29
-
30
+
30
31
if ($ this ->app ->runningInConsole ()) {
31
32
View::addNamespace ('helo ' , __DIR__ . '/../resources/views ' );
32
33
}
@@ -41,7 +42,7 @@ public function register()
41
42
$ this ->commands ([
42
43
TestMailCommand::class,
43
44
]);
44
-
45
+
45
46
$ this ->publishes ([
46
47
__DIR__ .'/../config/helo.php ' => base_path ('config/helo.php ' ),
47
48
], 'config ' );
@@ -50,11 +51,15 @@ public function register()
50
51
$ this ->mergeConfigFrom (__DIR__ .'/../config/helo.php ' , 'helo ' );
51
52
52
53
$ 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 ) {
54
57
return $ this ->createLaravel6Mailer ($ app );
58
+ } elseif ($ version < 9 ) {
59
+ return $ this ->createLaravel7Mailer ($ app );
55
60
}
56
61
57
- return $ this ->createLaravel7Mailer ($ app );
62
+ return $ this ->createLaravel9Mailer ($ app );
58
63
});
59
64
}
60
65
@@ -112,6 +117,35 @@ protected function createLaravel7Mailer($app)
112
117
return $ mailer ;
113
118
}
114
119
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
+
115
149
protected function getConfig ($ name = 'smtp ' )
116
150
{
117
151
return $ this ->app ['config ' ]['mail.driver ' ]
0 commit comments