Skip to content

Commit

Permalink
feat: support multiple to configuration formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Karel FAILLE committed Feb 27, 2020
1 parent 149f4b9 commit 7aecc32
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 22 deletions.
49 changes: 38 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ A service provider to add support for logging via email using Laravels built-in

This package is a fork of [laravel-log-mailer](https://packagist.org/packages/designmynight/laravel-log-mailer) by Steve Porter.

**This fork introduces some breaking changes. If you're upgrading from it, please follow the [configuration](#configuration) bellow.**

![image](https://user-images.githubusercontent.com/12199424/45576336-a93c1300-b86e-11e8-9575-d1e4c5ed5dec.png)


Expand All @@ -33,8 +31,8 @@ composer require shaffe/laravel-mail-log-channel

Laravel | Package |
:---------|:--------|
7.x | 2.0.x |
6.x | 2.0.x |
7.x | 1.1.x |
6.x | 1.1.x |
5.6.x | 1.0.x |

The package will automatically register itself if you use Laravel.
Expand Down Expand Up @@ -77,15 +75,13 @@ To ensure all unhandled exceptions are mailed:
]
],

// Optionally overwrite the sender.
// Default is config('mail.from.address') and config('mail.from.name')
// 'from' => [
// 'address' => env('LOG_MAIL_ADDRESS'),
// 'name' => 'Errors'
// ],
'from' => [
'address' => env('LOG_MAIL_ADDRESS'),
'name' => 'Errors'
],

// Optionally overwrite the subject format pattern
// 'subject_format' => env('LOG_MAIL_SUBJECT_FORMAT', '[%datetime%] %level_name%: %message%'),
'subject_format' => env('LOG_MAIL_SUBJECT_FORMAT', '[%datetime%] %level_name%: %message%'),

// Optionally overwrite the mailable template
// Two variables are sent to the view: `string $content` and `array $records`
Expand All @@ -94,4 +90,35 @@ To ensure all unhandled exceptions are mailed:
],
```

The following `to` config formats are supported:

* single email address:

```php
'to' => env('LOG_MAIL_ADDRESS', ''),
```

* array of email addresses:

```php
'to' => explode(',', env('LOG_MAIL_ADDRESS', '')),
```

* associative array of email/name addresses:

```php
'to' => [env('LOG_MAIL_ADDRESS', '') => 'Error'],`
```

* an array of email and name:

```php
'to' => [
[
'address' => env('LOG_MAIL_ADDRESS', ''),
'name' => 'Error',
],
],
```

You can specify multiple channels and change the recipients and customize the email template per channel.
49 changes: 38 additions & 11 deletions src/MailLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,17 @@ public function __invoke(array $config)
*/
protected function buildMailable(): Mailable
{
$mailable = $this->config('mailable') ?? MailableLog::class;
$mailable = new $mailable();
$mailableClass = $this->config('mailable') ?? MailableLog::class;
/** @var \Illuminate\Contracts\Mail\Mailable $mailable */
$mailable = new $mailableClass();

if (! ($recipients = $this->config('to'))) {
throw new InvalidArgumentException('"To" address is required.');
if (! ($recipients = $this->buildRecipients())) {
throw new InvalidArgumentException('"To" address is required. Please check the `to` driver\'s logging config.');
}

foreach ($recipients as $recipient) {
$mailable->to(
$recipient['address'],
$recipient['name']
);
}
$mailable->to($recipients);

if (! $this->defaultFromAddress()) {
if (! $this->defaultFromAddress() && ! isset($this->config('from')['address'])) {
throw new InvalidArgumentException('"From" address is required. Please check the `from.address` driver\'s config and the `mail.from.address` config.');
}

Expand All @@ -78,6 +74,37 @@ protected function buildMailable(): Mailable
return $mailable;
}

protected function buildRecipients(): array
{
if (! ($to = $this->config('to'))) {
return [];
}

$recipients = [];
foreach ((array)$to as $emailOrIndex => $nameOrEmail) {
if (is_array($nameOrEmail)) {
$email = $nameOrEmail['email'] ?? $nameOrEmail['address'] ?? null;
if ($email) {
$recipients[] = [
'email' => $email,
'name' => $nameOrEmail['name'] ?? null,
];
}
} elseif (is_string($emailOrIndex)) {
$recipients[] = [
'email' => $emailOrIndex,
'name' => $nameOrEmail,
];
} elseif (is_string($nameOrEmail)) {
$recipients[] = [
'email' => $nameOrEmail,
'name' => null,
];
}
}

return $recipients;
}

/**
* Get the default from address.
Expand Down

0 comments on commit 7aecc32

Please sign in to comment.