Skip to content

Commit f7df2d6

Browse files
authored
Merge pull request #35 from mailjet/fix-mail-driver-for-laravel-7-8
support for mail-driver for latest laravel versions
2 parents e50ef76 + 1b1435d commit f7df2d6

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Then, you need to add some informations in your configuration files. You can fin
4242
* In the services.php file:
4343

4444
```php
45-
mailjet' => [
45+
'mailjet' => [
4646
'key' => env('MAILJET_APIKEY'),
4747
'secret' => env('MAILJET_APISECRET'),
4848
]
@@ -88,8 +88,18 @@ You can pass settings to [MailjetClient](https://github.com/mailjet/mailjet-apiv
8888

8989
## Mail driver configuration
9090

91-
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)
91+
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)
92+
93+
For Laravel 7+ you also need to specify new available mail driver in config/mail.php:
94+
```
95+
'mailers' => [
96+
...
9297
98+
'mailjet' => [
99+
'transport' => 'mailjet',
100+
],
101+
],
102+
```
93103
For usage, please check the [Laravel mail documentation](https://laravel.com/docs/master/mail)
94104

95105
## Usage

docs/configuration.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ You can pass settings to [MailjetClient](https://github.com/mailjet/mailjet-apiv
3636

3737
## Mail driver configuration
3838

39-
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)
39+
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)
4040

41+
For Laravel 7+ you also need to specify new available mail driver in config/mail.php:
42+
```
43+
'mailers' => [
44+
...
45+
46+
'mailjet' => [
47+
'transport' => 'mailjet',
48+
],
49+
],
50+
```
4151
For usage, check the [Laravel mail documentation](https://laravel.com/docs/master/mail)

src/MailjetMailServiceProvider.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,44 @@
22

33
namespace Mailjet\LaravelMailjet;
44

5+
use Exception;
56
use Illuminate\Mail\MailServiceProvider;
67
use Mailjet\LaravelMailjet\Transport\MailjetTransport;
8+
use Swift_Events_SimpleEventDispatcher as EventDispatcher;
79

810
class MailjetMailServiceProvider extends MailServiceProvider
911
{
1012
/**
11-
* Extended register the Swift Transport instance.
13+
* Extended register the Illuminate Mailer instance.
1214
*
1315
* @return void
1416
*/
15-
protected function registerSwiftTransport()
17+
protected function registerIlluminateMailer(): void
1618
{
17-
parent::registerSwiftTransport();
18-
app('swift.transport')->extend('mailjet', function ($app) {
19-
$config = $this->app['config']->get('services.mailjet', array());
20-
$call = $this->app['config']->get('services.mailjet.transactional.call', true);
21-
$options = $this->app['config']->get('services.mailjet.transactional.options', array());
19+
parent::registerIlluminateMailer();
2220

23-
return new MailjetTransport(new \Swift_Events_SimpleEventDispatcher(), $config['key'], $config['secret'], $call, $options);
24-
});
21+
try {
22+
app('mail.manager')->extend('mailjet', function () {
23+
return $this->mailjetTransport();
24+
});
25+
} catch (Exception $e) {
26+
app('swift.transport')->extend('mailjet', function () {
27+
return $this->mailjetTransport();
28+
});
29+
}
30+
}
31+
32+
/**
33+
* Return configured MailjetTransport.
34+
*
35+
* @return MailjetTransport
36+
*/
37+
protected function mailjetTransport(): MailjetTransport
38+
{
39+
$config = $this->app['config']->get('services.mailjet', []);
40+
$call = $this->app['config']->get('services.mailjet.transactional.call', true);
41+
$options = $this->app['config']->get('services.mailjet.transactional.options', []);
42+
43+
return new MailjetTransport(new EventDispatcher(), $config['key'], $config['secret'], $call, $options);
2544
}
2645
}

0 commit comments

Comments
 (0)