Skip to content

Commit

Permalink
Prevent duplicate attachments
Browse files Browse the repository at this point in the history
This will prevent users from adding duplicate attachments. This bug surfaced when rendering a mailable for logging or storage before sending.
  • Loading branch information
taylorotwell committed Dec 17, 2019
1 parent 2cd9417 commit 3c8ccc2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,10 @@ public function with($key, $value = null)
*/
public function attach($file, array $options = [])
{
$this->attachments[] = compact('file', 'options');
$this->attachments = collect($this->attachments)
->push(compact('file', 'options'))
->unique('file')
->all();

return $this;
}
Expand Down Expand Up @@ -776,12 +779,14 @@ public function attachFromStorage($path, $name = null, array $options = [])
*/
public function attachFromStorageDisk($disk, $path, $name = null, array $options = [])
{
$this->diskAttachments[] = [
$this->diskAttachments = collect($this->diskAttachments)->push([
'disk' => $disk,
'path' => $path,
'name' => $name ?? basename($path),
'options' => $options,
];
])->unique(function ($file) {
return $file['disk'].$file['path'];
})->all();

return $this;
}
Expand All @@ -796,7 +801,10 @@ public function attachFromStorageDisk($disk, $path, $name = null, array $options
*/
public function attachData($data, $name, array $options = [])
{
$this->rawAttachments[] = compact('data', 'name', 'options');
$this->rawAttachments = collect($this->rawAttachments)
->push(compact('data', 'name', 'options'))
->unique('data')
->all();

return $this;
}
Expand Down

1 comment on commit 3c8ccc2

@wrenminix
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to comment out line 806 because i wasn't able to send more than 1 Excel file using maatwebsite/excel.
2021_01_30_10_37_57_minix_us_prod_ProductionPlansToSendEmail php

Please sign in to comment.