-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from joelbutcher/fix-mail-facade-instance-swap
Fix the `Mail::swap()` facade override used in Laravel 10
- Loading branch information
Showing
4 changed files
with
156 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace BeyondCode\HeloLaravel; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
|
||
trait CreatesMailers | ||
{ | ||
protected function createLaravel6Mailer($app) | ||
{ | ||
$config = $this->getConfig(); | ||
|
||
// Once we have create the mailer instance, we will set a container instance | ||
// on the mailer. This allows us to resolve mailer classes via containers | ||
// for maximum testability on said classes instead of passing Closures. | ||
$mailer = new Mailer( | ||
$app['view'], $app['swift.mailer'], $app['events'] | ||
); | ||
|
||
if ($app->bound('queue')) { | ||
$mailer->setQueue($app['queue']); | ||
} | ||
|
||
// Next we will set all of the global addresses on this mailer, which allows | ||
// for easy unification of all "from" addresses as well as easy debugging | ||
// of sent messages since they get be sent into a single email address. | ||
foreach (['from', 'reply_to', 'to'] as $type) { | ||
$this->setGlobalAddress($mailer, $config, $type); | ||
} | ||
|
||
return $mailer; | ||
} | ||
|
||
protected function createLaravel7Mailer($app) | ||
{ | ||
$defaultDriver = $app['mail.manager']->getDefaultDriver(); | ||
$config = $this->getConfig($defaultDriver); | ||
|
||
// Laravel 7 no longer bindes the swift.mailer: | ||
$swiftMailer = new Swift_Mailer($app['mail.manager']->createTransport($config)); | ||
|
||
// Once we have create the mailer instance, we will set a container instance | ||
// on the mailer. This allows us to resolve mailer classes via containers | ||
// for maximum testability on said classes instead of passing Closures. | ||
$mailer = new Laravel7Mailer( | ||
'smtp', $app['view'], $swiftMailer, $app['events'] | ||
); | ||
|
||
if ($app->bound('queue')) { | ||
$mailer->setQueue($app['queue']); | ||
} | ||
|
||
// Next we will set all of the global addresses on this mailer, which allows | ||
// for easy unification of all "from" addresses as well as easy debugging | ||
// of sent messages since they get be sent into a single email address. | ||
foreach (['from', 'reply_to', 'to', 'return_path'] as $type) { | ||
$this->setGlobalAddress($mailer, $config, $type); | ||
} | ||
|
||
return $mailer; | ||
} | ||
|
||
protected function createLaravel9Mailer($app) | ||
{ | ||
$defaultDriver = $app['mail.manager']->getDefaultDriver(); | ||
$config = $this->getConfig($defaultDriver); | ||
|
||
// We get Symfony Transport from Laravel 9 mailer | ||
$symfonyTransport = $app['mail.manager']->getSymfonyTransport(); | ||
|
||
// Once we have create the mailer instance, we will set a container instance | ||
// on the mailer. This allows us to resolve mailer classes via containers | ||
// for maximum testability on said classes instead of passing Closures. | ||
$mailer = new Laravel7Mailer( | ||
'smtp', $app['view'], $symfonyTransport, $app['events'] | ||
); | ||
|
||
if ($app->bound('queue')) { | ||
$mailer->setQueue($app['queue']); | ||
} | ||
|
||
// Next we will set all of the global addresses on this mailer, which allows | ||
// for easy unification of all "from" addresses as well as easy debugging | ||
// of sent messages since they get be sent into a single email address. | ||
foreach (['from', 'reply_to', 'to', 'return_path'] as $type) { | ||
$this->setGlobalAddress($mailer, $config, $type); | ||
} | ||
|
||
return $mailer; | ||
} | ||
|
||
protected function getConfig($name = 'smtp') | ||
{ | ||
return $this->app['config']['mail.driver'] | ||
? $this->app['config']['mail'] | ||
: $this->app['config']["mail.mailers.{$name}"]; | ||
} | ||
|
||
/** | ||
* Set a global address on the mailer by type. | ||
* | ||
* @param \Illuminate\Mail\Mailer $mailer | ||
* @param array $config | ||
* @param string $type | ||
* @return void | ||
*/ | ||
protected function setGlobalAddress($mailer, array $config, $type) | ||
{ | ||
if (version_compare(app()->version(), '7.0.0', '<')) { | ||
$address = Arr::get($config, $type); | ||
} else { | ||
$address = Arr::get($config, $type, $this->app['config']['mail.'.$type]); | ||
} | ||
|
||
if (is_array($address) && isset($address['address'])) { | ||
$mailer->{'always' . Str::studly($type)}($address['address'], $address['name']); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace BeyondCode\HeloLaravel; | ||
|
||
use Illuminate\Contracts\Mail\Factory as FactoryContract; | ||
use Illuminate\Mail\MailManager as LaravelMailManager; | ||
|
||
class MailManager extends LaravelMailManager implements FactoryContract | ||
{ | ||
use CreatesMailers; | ||
|
||
public function mailer($name = null) | ||
{ | ||
if (! $name) { | ||
return $this->createLaravel9Mailer($this->app); | ||
} | ||
|
||
return $this->mailers[$name] = $this->get($name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters