Skip to content

Commit 7aecc32

Browse files
author
Karel FAILLE
committed
feat: support multiple to configuration formats
1 parent 149f4b9 commit 7aecc32

File tree

2 files changed

+76
-22
lines changed

2 files changed

+76
-22
lines changed

README.md

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ A service provider to add support for logging via email using Laravels built-in
99

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

12-
**This fork introduces some breaking changes. If you're upgrading from it, please follow the [configuration](#configuration) bellow.**
13-
1412
![image](https://user-images.githubusercontent.com/12199424/45576336-a93c1300-b86e-11e8-9575-d1e4c5ed5dec.png)
1513

1614

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

3432
Laravel | Package |
3533
:---------|:--------|
36-
7.x | 2.0.x |
37-
6.x | 2.0.x |
34+
7.x | 1.1.x |
35+
6.x | 1.1.x |
3836
5.6.x | 1.0.x |
3937

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

80-
// Optionally overwrite the sender.
81-
// Default is config('mail.from.address') and config('mail.from.name')
82-
// 'from' => [
83-
// 'address' => env('LOG_MAIL_ADDRESS'),
84-
// 'name' => 'Errors'
85-
// ],
78+
'from' => [
79+
'address' => env('LOG_MAIL_ADDRESS'),
80+
'name' => 'Errors'
81+
],
8682

8783
// Optionally overwrite the subject format pattern
88-
// 'subject_format' => env('LOG_MAIL_SUBJECT_FORMAT', '[%datetime%] %level_name%: %message%'),
84+
'subject_format' => env('LOG_MAIL_SUBJECT_FORMAT', '[%datetime%] %level_name%: %message%'),
8985

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

93+
The following `to` config formats are supported:
94+
95+
* single email address:
96+
97+
```php
98+
'to' => env('LOG_MAIL_ADDRESS', ''),
99+
```
100+
101+
* array of email addresses:
102+
103+
```php
104+
'to' => explode(',', env('LOG_MAIL_ADDRESS', '')),
105+
```
106+
107+
* associative array of email/name addresses:
108+
109+
```php
110+
'to' => [env('LOG_MAIL_ADDRESS', '') => 'Error'],`
111+
```
112+
113+
* an array of email and name:
114+
115+
```php
116+
'to' => [
117+
[
118+
'address' => env('LOG_MAIL_ADDRESS', ''),
119+
'name' => 'Error',
120+
],
121+
],
122+
```
123+
97124
You can specify multiple channels and change the recipients and customize the email template per channel.

src/MailLogger.php

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,17 @@ public function __invoke(array $config)
5252
*/
5353
protected function buildMailable(): Mailable
5454
{
55-
$mailable = $this->config('mailable') ?? MailableLog::class;
56-
$mailable = new $mailable();
55+
$mailableClass = $this->config('mailable') ?? MailableLog::class;
56+
/** @var \Illuminate\Contracts\Mail\Mailable $mailable */
57+
$mailable = new $mailableClass();
5758

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

62-
foreach ($recipients as $recipient) {
63-
$mailable->to(
64-
$recipient['address'],
65-
$recipient['name']
66-
);
67-
}
63+
$mailable->to($recipients);
6864

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

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

77+
protected function buildRecipients(): array
78+
{
79+
if (! ($to = $this->config('to'))) {
80+
return [];
81+
}
82+
83+
$recipients = [];
84+
foreach ((array)$to as $emailOrIndex => $nameOrEmail) {
85+
if (is_array($nameOrEmail)) {
86+
$email = $nameOrEmail['email'] ?? $nameOrEmail['address'] ?? null;
87+
if ($email) {
88+
$recipients[] = [
89+
'email' => $email,
90+
'name' => $nameOrEmail['name'] ?? null,
91+
];
92+
}
93+
} elseif (is_string($emailOrIndex)) {
94+
$recipients[] = [
95+
'email' => $emailOrIndex,
96+
'name' => $nameOrEmail,
97+
];
98+
} elseif (is_string($nameOrEmail)) {
99+
$recipients[] = [
100+
'email' => $nameOrEmail,
101+
'name' => null,
102+
];
103+
}
104+
}
105+
106+
return $recipients;
107+
}
81108

82109
/**
83110
* Get the default from address.

0 commit comments

Comments
 (0)