33namespace BeyondCode \HeloLaravel ;
44
55use Illuminate \Contracts \Mail \Mailer as MailerContract ;
6- use Illuminate \Support \Arr ;
76use Illuminate \Support \Facades \Mail ;
87use Illuminate \Support \Facades \View ;
98use Illuminate \Support \ServiceProvider ;
10- use Illuminate \Support \Str ;
119use Swift_Mailer ;
1210
1311class HeloLaravelServiceProvider extends ServiceProvider
1412{
13+ use CreatesMailers;
14+
1515 /**
1616 * Bootstrap the application services.
1717 */
@@ -21,11 +21,8 @@ public function boot()
2121 return ;
2222 }
2323
24- $ instance = app ()->make (Mailer::class);
25-
26- Mail::swap ($ instance );
2724
28- app ()-> instance (MailerContract::class, $ instance );
25+ $ this -> bootMailable ( );
2926
3027 if ($ this ->app ->runningInConsole ()) {
3128 View::addNamespace ('helo ' , __DIR__ . '/../resources/views ' );
@@ -50,7 +47,7 @@ public function register()
5047 $ this ->mergeConfigFrom (__DIR__ .'/../config/helo.php ' , 'helo ' );
5148
5249 $ this ->app ->singleton (Mailer::class, function ($ app ) {
53- $ version = ( int ) Str:: of ( $ app ->version ())-> explode ( ' . ' )-> first ( );
50+ $ version = $ this ->version ($ app );
5451
5552 if ($ version < 7 ) {
5653 return $ this ->createLaravel6Mailer ($ app );
@@ -63,114 +60,25 @@ public function register()
6360 });
6461 }
6562
66- protected function createLaravel6Mailer ( $ app )
63+ protected function bootMailable ( )
6764 {
68- $ config = $ this ->getConfig ();
65+ if ($ this ->version () < 10 ) {
66+ $ instance = app ()->make (Mailer::class);
6967
70- // Once we have create the mailer instance, we will set a container instance
71- // on the mailer. This allows us to resolve mailer classes via containers
72- // for maximum testability on said classes instead of passing Closures.
73- $ mailer = new Mailer (
74- $ app ['view ' ], $ app ['swift.mailer ' ], $ app ['events ' ]
75- );
68+ Mail::swap ($ instance );
7669
77- if ($ app ->bound ('queue ' )) {
78- $ mailer ->setQueue ($ app ['queue ' ]);
79- }
80-
81- // Next we will set all of the global addresses on this mailer, which allows
82- // for easy unification of all "from" addresses as well as easy debugging
83- // of sent messages since they get be sent into a single email address.
84- foreach (['from ' , 'reply_to ' , 'to ' ] as $ type ) {
85- $ this ->setGlobalAddress ($ mailer , $ config , $ type );
86- }
87-
88- return $ mailer ;
89- }
90-
91- protected function createLaravel7Mailer ($ app )
92- {
93- $ defaultDriver = $ app ['mail.manager ' ]->getDefaultDriver ();
94- $ config = $ this ->getConfig ($ defaultDriver );
95-
96- // Laravel 7 no longer bindes the swift.mailer:
97- $ swiftMailer = new Swift_Mailer ($ app ['mail.manager ' ]->createTransport ($ config ));
98-
99- // Once we have create the mailer instance, we will set a container instance
100- // on the mailer. This allows us to resolve mailer classes via containers
101- // for maximum testability on said classes instead of passing Closures.
102- $ mailer = new Laravel7Mailer (
103- 'smtp ' , $ app ['view ' ], $ swiftMailer , $ app ['events ' ]
104- );
105-
106- if ($ app ->bound ('queue ' )) {
107- $ mailer ->setQueue ($ app ['queue ' ]);
108- }
109-
110- // Next we will set all of the global addresses on this mailer, which allows
111- // for easy unification of all "from" addresses as well as easy debugging
112- // of sent messages since they get be sent into a single email address.
113- foreach (['from ' , 'reply_to ' , 'to ' , 'return_path ' ] as $ type ) {
114- $ this ->setGlobalAddress ($ mailer , $ config , $ type );
115- }
116-
117- return $ mailer ;
118- }
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 );
70+ $ this ->app ->instance (MailerContract::class, $ instance );
71+ } else {
72+ Mail::swap (new MailManager ($ this ->app ));
14473 }
145-
146- return $ mailer ;
147- }
148-
149- protected function getConfig ($ name = 'smtp ' )
150- {
151- return $ this ->app ['config ' ]['mail.driver ' ]
152- ? $ this ->app ['config ' ]['mail ' ]
153- : $ this ->app ['config ' ]["mail.mailers. {$ name }" ];
15474 }
15575
156- /**
157- * Set a global address on the mailer by type.
158- *
159- * @param \Illuminate\Mail\Mailer $mailer
160- * @param array $config
161- * @param string $type
162- * @return void
163- */
164- protected function setGlobalAddress ($ mailer , array $ config , $ type )
76+ private function version ($ app = null ): int
16577 {
166- if (version_compare (app ()->version (), '7.0.0 ' , '< ' )) {
167- $ address = Arr::get ($ config , $ type );
168- } else {
169- $ address = Arr::get ($ config , $ type , $ this ->app ['config ' ]['mail. ' .$ type ]);
78+ if (! $ app ) {
79+ $ app = $ this ->app ;
17080 }
17181
172- if (is_array ($ address ) && isset ($ address ['address ' ])) {
173- $ mailer ->{'always ' . Str::studly ($ type )}($ address ['address ' ], $ address ['name ' ]);
174- }
82+ return (int ) collect (explode ('. ' , $ app ->version ()))->first ();
17583 }
17684}
0 commit comments