Skip to content

Commit

Permalink
ensure embeds are returning CIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Jun 2, 2022
1 parent 2201661 commit 333db9d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Mail/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public function attachTo($mail)
public function embedIn($mail)
{
return $this->attachWith(
fn ($path) => $mail->embed($path),
fn ($data) => $mail->embedData($data(), $this->as, $this->mime)
fn ($path) => $mail->embed($path, ['as' => $this->as, 'mime' => $this->mime]),
fn ($data) => $mail->embedData($data(), $this->as, ['mime' => $this->mime])
);
}
}
40 changes: 27 additions & 13 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,17 @@ protected function buildAttachments($message)
}

foreach ($this->embeds as $embed) {
$message->embed($embed);
$message->embed(Attachment::fromPath($embed['file'])
->as($embed['options']['as'])
->withMime($embed['options']['mime'] ?? null));
}

foreach ($this->rawEmbeds as $embed) {
$message->embedData($embed['data'], $embed['name'], $embed['contentType']);
$message->embedData(
$embed['data'],
$embed['name'],
$embed['options']['mime'] ?? null
);
}

$this->buildDiskAttachments($message);
Expand Down Expand Up @@ -971,9 +977,10 @@ public function attachData($data, $name, array $options = [])
* Embed a file into the message.
*
* @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file
* @return $this
* @param array $options
* @return string
*/
public function embed($file)
public function embed($file, array $options = [])
{
if ($file instanceof Attachable) {
$file = $file->toMailAttachment();
Expand All @@ -983,34 +990,41 @@ public function embed($file)
return $file->embedIn($this);
}

$options['as'] ??= Str::random();

$this->embeds = collect($this->embeds)
->push($file)
->unique()
->push([
'file' => $file,
'options' => $options,
])
->unique('file')
->all();

return $this;
return "cid:{$options['as']}";
}

/**
* Embed in-memory data in the message.
*
* @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file
* @param string $name
* @param string|null $contentType
* @return $this
* @param string|null $name
* @param array $options
* @return string
*/
public function embedData($data, $name, $contentType = null)
public function embedData($data, $name = null, array $options = [])
{
$name ??= Str::random();

$this->rawEmbeds = collect($this->rawEmbeds)
->push([
'data' => $data,
'name' => $name,
'contentType' => $contentType,
'options' => $options,
])
->unique(fn ($file) => $file['name'].$file['data'])
->all();

return $this;
return "cid:{$name}";
}

/**
Expand Down
80 changes: 70 additions & 10 deletions tests/Mail/MailMailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailer;
use Illuminate\Mail\Transport\ArrayTransport;
use Illuminate\Support\Str;
use Mockery as m;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -507,15 +508,15 @@ public function toMailAttachment()
{
return Attachment::fromPath(__DIR__.'/foo.jpg')
->as('bar')
->withMime('image/jpeg');
->withMime('image/png');
}
});

$this->assertSame([
'file' => __DIR__.'/foo.jpg',
'options' => [
'as' => 'bar',
'mime' => 'image/jpeg',
'mime' => 'image/png',
],
], $mailable->attachments[0]);
}
Expand All @@ -530,55 +531,114 @@ public function toMailAttachment()
{
return Attachment::fromData(fn () => 'expected attachment body', 'foo.jpg')
->as('bar')
->withMime('image/jpeg');
->withMime('image/png');
}
});

$this->assertSame([
'data' => 'expected attachment body',
'name' => 'bar',
'options' => [
'mime' => 'image/jpeg',
'mime' => 'image/png',
],
], $mailable->rawAttachments[0]);
}

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

$cid = $mailable->embedData('expected attachment body', 'foo', ['mime' => 'image/png']);

$this->assertSame([
'data' => 'expected attachment body',
'name' => 'foo',
'options' => [
'mime' => 'image/png',
],
], $mailable->rawEmbeds[0]);
$this->assertSame('cid:foo', $cid);
}

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

$cid = $mailable->embed('logo.png', ['as' => 'logo', 'mime' => 'image/png']);

$this->assertSame([
'file' => 'logo.png',
'options' => [
'as' => 'logo',
'mime' => 'image/png',
],
], $mailable->embeds[0]);
$this->assertSame('cid:logo', $cid);
}

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

$cid = $mailable->embed('logo.png');

$as = Str::after($cid, 'cid:');
$this->assertStringStartsWith('cid:', $cid);
$this->assertSame(16, mb_strlen($as));
$this->assertSame([
'file' => 'logo.png',
'options' => [
'as' => $as,
]
], $mailable->embeds[0]);
}

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

$mailable->embed(new class() implements Attachable
$cid = $mailable->embed(new class() implements Attachable
{
public function toMailAttachment()
{
return Attachment::fromData(fn () => 'expected attachment body', 'foo.jpg')
->as('bar')
->withMime('image/jpeg');
->withMime('image/png');
}
});

$this->assertSame('cid:bar', $cid);
$this->assertSame([
'data' => 'expected attachment body',
'name' => 'bar',
'contentType' => 'image/jpeg',
'options' => [
'mime' => 'image/png',
],
], $mailable->rawEmbeds[0]);
}

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

$mailable->embed(new class() implements Attachable
$cid = $mailable->embed(new class() implements Attachable
{
public function toMailAttachment()
{
return Attachment::fromPath(__DIR__.'/foo.jpg')
->as('bar')
->withMime('image/jpeg');
->withMime('image/png');
}
});

$this->assertSame(__DIR__.'/foo.jpg', $mailable->embeds[0]);
$this->assertSame('cid:bar', $cid);
$this->assertSame([
'file' => __DIR__.'/foo.jpg',
'options' => [
'as' => 'bar',
'mime' => 'image/png',
],
], $mailable->embeds[0]);
}
}

Expand Down

0 comments on commit 333db9d

Please sign in to comment.