Skip to content

Commit

Permalink
Merge pull request #35 from mailjet/fix-mail-driver-for-laravel-7-8
Browse files Browse the repository at this point in the history
support for mail-driver for latest laravel versions
  • Loading branch information
uavn authored Nov 11, 2020
2 parents e50ef76 + 1b1435d commit f7df2d6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
37 changes: 28 additions & 9 deletions src/MailjetMailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit f7df2d6

Please sign in to comment.