Skip to content

Commit

Permalink
Merge pull request #32392 from mnabialek/6.x-mailable-raw-attachments
Browse files Browse the repository at this point in the history
[6.x] Duplicated mailable attachments with different names
  • Loading branch information
taylorotwell authored Apr 15, 2020
2 parents e78d24f + 3203c81 commit 7c07cc5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,9 @@ public function attachData($data, $name, array $options = [])
{
$this->rawAttachments = collect($this->rawAttachments)
->push(compact('data', 'name', 'options'))
->unique('data')
->all();
->unique(function ($file) {
return $file['name'].$file['data'];
})->all();

return $this;
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Mail/MailMailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,48 @@ public function testMailableSetsReplyToCorrectly()
$this->assertTrue($mailable->hasReplyTo('[email protected]'));
}

public function testItIgnoresDuplicatedRawAttachments()
{
$mailable = new WelcomeMailableStub;

$mailable->attachData('content1', 'report-1.txt');
$this->assertCount(1, $mailable->rawAttachments);

$mailable->attachData('content2', 'report-2.txt');
$this->assertCount(2, $mailable->rawAttachments);

$mailable->attachData('content1', 'report-1.txt');
$mailable->attachData('content2', 'report-2.txt');
$this->assertCount(2, $mailable->rawAttachments);

$mailable->attachData('content1', 'report-3.txt');
$mailable->attachData('content2', 'report-4.txt');
$this->assertCount(4, $mailable->rawAttachments);

$this->assertSame([
[
'data' => 'content1',
'name' => 'report-1.txt',
'options' => [],
],
[
'data' => 'content2',
'name' => 'report-2.txt',
'options' => [],
],
[
'data' => 'content1',
'name' => 'report-3.txt',
'options' => [],
],
[
'data' => 'content2',
'name' => 'report-4.txt',
'options' => [],
],
], $mailable->rawAttachments);
}

public function testMailableBuildsViewData()
{
$mailable = new WelcomeMailableStub;
Expand Down

0 comments on commit 7c07cc5

Please sign in to comment.