Skip to content

Commit a64567f

Browse files
committed
Add support for Address object in ReplyTo
1 parent 75be256 commit a64567f

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/Preparer.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Stackkit\LaravelDatabaseEmails;
66

77
use Carbon\Carbon;
8+
use Illuminate\Mail\Mailables\Address;
89

910
class Preparer
1011
{
@@ -127,8 +128,23 @@ private function prepareBcc(EmailComposer $composer): void
127128
*/
128129
private function prepareReplyTo(EmailComposer $composer): void
129130
{
131+
$value = $composer->getData('reply_to', []);
132+
133+
if (! is_array($value)) {
134+
$value = [$value];
135+
}
136+
137+
foreach ($value as $i => $v) {
138+
if ($v instanceof Address) {
139+
$value[$i] = [
140+
'address' => $v->address,
141+
'name' => $v->name,
142+
];
143+
}
144+
}
145+
130146
$composer->getEmail()->fill([
131-
'reply_to' => json_encode($composer->getData('reply_to', [])),
147+
'reply_to' => json_encode($value),
132148
]);
133149
}
134150

src/Validator.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Exception;
88
use Carbon\Carbon;
9+
use Illuminate\Mail\Mailables\Address;
10+
use Illuminate\Support\Arr;
911
use InvalidArgumentException;
1012
use const FILTER_VALIDATE_EMAIL;
1113

@@ -133,7 +135,11 @@ private function validateReplyTo(EmailComposer $composer): void
133135
return;
134136
}
135137

136-
foreach ((array) $composer->getData('reply_to') as $replyTo) {
138+
foreach (Arr::wrap($composer->getData('reply_to')) as $replyTo) {
139+
if ($replyTo instanceof Address) {
140+
$replyTo = $replyTo->address;
141+
}
142+
137143
if (! filter_var($replyTo, FILTER_VALIDATE_EMAIL)) {
138144
throw new InvalidargumentException('E-mail address [' . $replyTo . '] is invalid');
139145
}

tests/SenderTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests;
44

5+
use Illuminate\Mail\Mailables\Address;
56
use Illuminate\Support\Facades\Event;
67
use Stackkit\LaravelDatabaseEmails\MessageSent;
78
use Stackkit\LaravelDatabaseEmails\SentMessage;
@@ -242,5 +243,36 @@ public function it_adds_the_reply_to_addresses()
242243
$this->assertCount(2, $replyTo);
243244
$this->assertArrayHasKey('[email protected]', $replyTo);
244245
$this->assertArrayHasKey('[email protected]', $replyTo);
246+
247+
if (! class_exists(Address::class)) {
248+
return;
249+
}
250+
251+
$this->sent = [];
252+
$this->sendEmail([
253+
'reply_to' => new Address('[email protected]', 'NoReplyTest'),
254+
]);
255+
$this->artisan('email:send');
256+
$replyTo = reset($this->sent)->replyTo;
257+
$this->assertCount(1, $replyTo);
258+
$this->assertSame(['[email protected]' => 'NoReplyTest'], $replyTo);
259+
260+
$this->sent = [];
261+
$this->sendEmail([
262+
'reply_to' => [
263+
new Address('[email protected]', 'NoReplyTest'),
264+
new Address('[email protected]', 'NoReplyTest2'),
265+
],
266+
]);
267+
$this->artisan('email:send');
268+
$replyTo = reset($this->sent)->replyTo;
269+
$this->assertCount(2, $replyTo);
270+
$this->assertSame(
271+
[
272+
'[email protected]' => 'NoReplyTest',
273+
'[email protected]' => 'NoReplyTest2',
274+
],
275+
$replyTo
276+
);
245277
}
246278
}

0 commit comments

Comments
 (0)