forked from spatie/laravel-medialibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Laravel's
Attachable
objects (spatie#2963)
* add attachable support * Fix styling * fix phpstan errors * remove static analysis as it keeps failing only on github Co-authored-by: freekmurze <[email protected]>
- Loading branch information
1 parent
364949e
commit 6f54cf4
Showing
11 changed files
with
242 additions
and
41 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
title: Attaching media in mails | ||
weight: 9 | ||
--- | ||
|
||
Laravel allows [to attach all sorts of classes](https://laravel.com/docs/9.x/mail#attachable-objects) in mails. The `Media` model implements Laravel's `Attachable` interface, so you can attach `Media` models directly in mails. | ||
|
||
|
||
```php | ||
namespace App\Mails; | ||
|
||
use Illuminate\Mail\Mailable; | ||
use Spatie\MediaLibrary\MediaCollections\Models\Media; | ||
use App\Models\Order; | ||
|
||
class OrderConfirmationMail extends Mailable | ||
{ | ||
public function __construct(public Order $order) | ||
{ | ||
|
||
} | ||
|
||
public function build() | ||
{ | ||
/** @var \Spatie\MediaLibrary\MediaCollections\Models\Media $invoice */ | ||
$invoice = $this->order->getFirstMedia('invoice') | ||
|
||
return $this | ||
->view('invoice') | ||
->attach($invoice); | ||
} | ||
} | ||
``` | ||
|
||
## Using conversions as attachments | ||
|
||
You can call `mailAttachment()` on a `Media` model to get back an `Attachment` that you can use in a Mailable. You can pass the name of a conversion to `mailAttachment()` to get an attachable conversion back. | ||
|
||
```php | ||
namespace App\Mails; | ||
|
||
use Illuminate\Mail\Mailable; | ||
use Spatie\MediaLibrary\MediaCollections\Models\Media; | ||
use App\Models\BlogPost; | ||
|
||
class BlogPostThumbnailMail extends Mailable | ||
{ | ||
public function __construct(public BlogPost $blogPost) | ||
{ | ||
|
||
} | ||
|
||
public function build() | ||
{ | ||
/** @var \Spatie\MediaLibrary\MediaCollections\Models\Media $mediaItem */ | ||
$mediaItem = $this->blogPost->getFirstMedia(); | ||
|
||
// pass the conversion name | ||
$thumbnailAttachment = $mediaItem->mailAttachment('thumbnail'); | ||
|
||
return $this | ||
->view('mails/blogpostThumbnail') | ||
->attach($thumbnailAttachment); | ||
} | ||
} | ||
``` | ||
|
||
## Customizing the attachment | ||
|
||
By default, the attachment will use the `file_name` and `mime_type` properties to configure Laravel's `Attachment` class. To override how `Attachments` are made, [use a custom media model](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/using-your-own-model), and override the `toMailAttachment` method. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
use Illuminate\Mail\Attachment; | ||
use Illuminate\Support\Facades\Mail; | ||
use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidConversion; | ||
use Spatie\MediaLibrary\MediaCollections\Models\Media; | ||
use Spatie\MediaLibrary\Tests\TestSupport\Mail\AttachmentMail; | ||
use Spatie\MediaLibrary\Tests\TestSupport\Mail\InvalidMediaConversionAttachmentMail; | ||
use Spatie\MediaLibrary\Tests\TestSupport\Mail\MediaConversionAttachmentMail; | ||
|
||
beforeEach(function () { | ||
$this->testModelWithConversion | ||
->addMedia($this->getTestJpg()) | ||
->toMediaCollection(); | ||
}); | ||
|
||
it('can create a mail attachment from a media', function () { | ||
$mailAttachment = Media::first()->toMailAttachment(); | ||
|
||
expect($mailAttachment)->toBeInstanceOf(Attachment::class); | ||
}); | ||
|
||
it('can send a mail with a media attached', function () { | ||
$mailable = new AttachmentMail(Media::first()); | ||
|
||
Mail::send($mailable); | ||
|
||
// assert no exceptions thrown | ||
expect(true)->toBeTrue(); | ||
}); | ||
|
||
it('can create an attachment to a conversion', function () { | ||
$mailAttachment = $this->testModelWithConversion->getFirstMedia()->mailAttachment('thumb'); | ||
|
||
expect($mailAttachment)->toBeInstanceOf(Attachment::class); | ||
}); | ||
|
||
it('can send a mail with conversion attached', function () { | ||
$mailable = new MediaConversionAttachmentMail(Media::first()); | ||
|
||
Mail::send($mailable); | ||
|
||
// assert no exceptions thrown | ||
expect(true)->toBeTrue(); | ||
}); | ||
|
||
it('will throw an exception when attaching a media specifying a non-existing conversion', function () { | ||
$mailable = new InvalidMediaConversionAttachmentMail(Media::first()); | ||
|
||
Mail::send($mailable); | ||
})->throws(InvalidConversion::class); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Spatie\MediaLibrary\Tests\TestSupport\Mail; | ||
|
||
use Illuminate\Mail\Mailable; | ||
use Spatie\MediaLibrary\MediaCollections\Models\Media; | ||
|
||
class AttachmentMail extends Mailable | ||
{ | ||
public function __construct(public Media $media) | ||
{ | ||
} | ||
|
||
public function build() | ||
{ | ||
return $this | ||
->to('[email protected]') | ||
->view('mailable') | ||
->attach($this->media); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/TestSupport/Mail/InvalidMediaConversionAttachmentMail.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Spatie\MediaLibrary\Tests\TestSupport\Mail; | ||
|
||
use Illuminate\Mail\Mailable; | ||
use Spatie\MediaLibrary\MediaCollections\Models\Media; | ||
|
||
class InvalidMediaConversionAttachmentMail extends Mailable | ||
{ | ||
public function __construct(public Media $media) | ||
{ | ||
} | ||
|
||
public function build() | ||
{ | ||
$thumbnailAttachment = $this->media->mailAttachment('non-existing-conversion'); | ||
|
||
return $this | ||
->to('[email protected]') | ||
->view('mailable') | ||
->attach($thumbnailAttachment); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Spatie\MediaLibrary\Tests\TestSupport\Mail; | ||
|
||
use Illuminate\Mail\Mailable; | ||
use Spatie\MediaLibrary\MediaCollections\Models\Media; | ||
|
||
class MediaConversionAttachmentMail extends Mailable | ||
{ | ||
public function __construct(public Media $media) | ||
{ | ||
} | ||
|
||
public function build() | ||
{ | ||
$thumbnailAttachment = $this->media->mailAttachment('thumb'); | ||
|
||
return $this | ||
->to('[email protected]') | ||
->view('mailable') | ||
->attach($thumbnailAttachment); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Test mailable |