diff --git a/README.md b/README.md index 2f4c578..dfbc5b4 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Then, you need to add some informations in your configuration files. You can fin * In the services.php file: ```php -mailjet' => [ +'mailjet' => [ 'key' => env('MAILJET_APIKEY'), 'secret' => env('MAILJET_APISECRET'), ] @@ -88,8 +88,18 @@ You can pass settings to [MailjetClient](https://github.com/mailjet/mailjet-apiv ## Mail driver configuration -In order to use Mailjet as your Mail driver, you need to update the mail driver in your `config/mail.php` or your `.env` file to `MAIL_DRIVER=mailjet`, and make sure you are using a valid and authorised from email address configured on your Mailjet account. The sending email addresses and domain can be managed [here](https://app.mailjet.com/account/sender) +In order to use Mailjet as your Mail driver, you need to update the mail driver in your `config/mail.php` or your `.env` file to `MAIL_MAILER=mailjet` (for Laravel 6 and older use MAIL_DRIVER constant instead), and make sure you are using a valid and authorised from email address configured on your Mailjet account. The sending email addresses and domain can be managed [here](https://app.mailjet.com/account/sender) + +For Laravel 7+ you also need to specify new available mail driver in config/mail.php: +``` +'mailers' => [ + ... + 'mailjet' => [ + 'transport' => 'mailjet', + ], +], +``` For usage, please check the [Laravel mail documentation](https://laravel.com/docs/master/mail) ## Usage diff --git a/docs/configuration.md b/docs/configuration.md index d213edc..e3b9ddf 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -36,6 +36,16 @@ You can pass settings to [MailjetClient](https://github.com/mailjet/mailjet-apiv ## Mail driver configuration -In order to use Mailjet as your Mail driver, you need to update the mail driver in your `config/mail.php` or your `.env` file to `MAIL_DRIVER=mailjet`, and make sure you are using a valid and authorised from email address configured on your Mailjet account. The sending email addresses and domain can be managed [here](https://app.mailjet.com/account/sender) +In order to use Mailjet as your Mail driver, you need to update the mail driver in your `config/mail.php` or your `.env` file to `MAIL_MAILER=mailjet` (for Laravel 6 and older use MAIL_DRIVER constant instead), and make sure you are using a valid and authorised from email address configured on your Mailjet account. The sending email addresses and domain can be managed [here](https://app.mailjet.com/account/sender) +For Laravel 7+ you also need to specify new available mail driver in config/mail.php: +``` +'mailers' => [ + ... + + 'mailjet' => [ + 'transport' => 'mailjet', + ], +], +``` For usage, check the [Laravel mail documentation](https://laravel.com/docs/master/mail) \ No newline at end of file diff --git a/src/MailjetMailServiceProvider.php b/src/MailjetMailServiceProvider.php index d04cc1e..b8a3a30 100644 --- a/src/MailjetMailServiceProvider.php +++ b/src/MailjetMailServiceProvider.php @@ -2,25 +2,44 @@ namespace Mailjet\LaravelMailjet; +use Exception; use Illuminate\Mail\MailServiceProvider; use Mailjet\LaravelMailjet\Transport\MailjetTransport; +use Swift_Events_SimpleEventDispatcher as EventDispatcher; class MailjetMailServiceProvider extends MailServiceProvider { /** - * Extended register the Swift Transport instance. + * Extended register the Illuminate Mailer instance. * * @return void */ - protected function registerSwiftTransport() + protected function registerIlluminateMailer(): void { - parent::registerSwiftTransport(); - app('swift.transport')->extend('mailjet', function ($app) { - $config = $this->app['config']->get('services.mailjet', array()); - $call = $this->app['config']->get('services.mailjet.transactional.call', true); - $options = $this->app['config']->get('services.mailjet.transactional.options', array()); + parent::registerIlluminateMailer(); - return new MailjetTransport(new \Swift_Events_SimpleEventDispatcher(), $config['key'], $config['secret'], $call, $options); - }); + try { + app('mail.manager')->extend('mailjet', function () { + return $this->mailjetTransport(); + }); + } catch (Exception $e) { + app('swift.transport')->extend('mailjet', function () { + return $this->mailjetTransport(); + }); + } + } + + /** + * Return configured MailjetTransport. + * + * @return MailjetTransport + */ + protected function mailjetTransport(): MailjetTransport + { + $config = $this->app['config']->get('services.mailjet', []); + $call = $this->app['config']->get('services.mailjet.transactional.call', true); + $options = $this->app['config']->get('services.mailjet.transactional.options', []); + + return new MailjetTransport(new EventDispatcher(), $config['key'], $config['secret'], $call, $options); } }