From b534da3d52df2bbd45c3693e9d2a663fe3706f3d Mon Sep 17 00:00:00 2001 From: Roelof Roos Date: Tue, 22 Jun 2021 14:13:16 +0200 Subject: [PATCH 1/5] Custom Embedder - Custom embedders --- src/Listeners/SwiftEmbedImages.php | 13 ++++++------- src/ServiceProvider.php | 6 ++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Listeners/SwiftEmbedImages.php b/src/Listeners/SwiftEmbedImages.php index 432f85f..2d07204 100755 --- a/src/Listeners/SwiftEmbedImages.php +++ b/src/Listeners/SwiftEmbedImages.php @@ -8,6 +8,7 @@ use Eduardokum\LaravelMailAutoEmbed\Embedder\Base64Embedder; use Eduardokum\LaravelMailAutoEmbed\Embedder\Embedder; use Eduardokum\LaravelMailAutoEmbed\Models\EmbeddableEntity; +use Illuminate\Support\Facades\App; use Masterminds\HTML5; use ReflectionClass; use Swift_Events_SendEvent; @@ -155,14 +156,12 @@ private function getEmbedder($imageTag) $method = $this->config['method']; } - switch ($method) { - case 'attachment': - default: - return new AttachmentEmbedder($this->message); - - case 'base64': - return new Base64Embedder(); + $embedderName = "mail-auto-embed.{$method}"; + if (!App::bound($embedderName)) { + $embedderName = 'mail-auto-embed.attachment'; } + + return App::make($embedderName, [$this->message]); } /** diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 400867b..3c8123c 100755 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,6 +2,8 @@ namespace Eduardokum\LaravelMailAutoEmbed; +use Eduardokum\LaravelMailAutoEmbed\Embedder\AttachmentEmbedder; +use Eduardokum\LaravelMailAutoEmbed\Embedder\Base64Embedder; use Eduardokum\LaravelMailAutoEmbed\Listeners\SwiftEmbedImages; use Illuminate\Support\Facades\Mail; use Illuminate\Support\ServiceProvider as BaseServiceProvider; @@ -28,6 +30,10 @@ public function boot() public function register() { $this->mergeConfigFrom($this->getConfigPath(), 'mail-auto-embed'); + + // Register default embedder + $this->app->bind('mail-auto-embed.attachment', AttachmentEmbedder::class); + $this->app->bind('mail-auto-embed.base64', Base64Embedder::class); } /** From 0f2ab23d06ad7daf6aa9d23ecc3c82e37d706063 Mon Sep 17 00:00:00 2001 From: Roelof Roos Date: Tue, 22 Jun 2021 14:13:19 +0200 Subject: [PATCH 2/5] Custom Embedder - Readme update --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index 192cc8f..16ce339 100755 --- a/README.md +++ b/README.md @@ -239,6 +239,46 @@ Then, you can use the `embed:ClassName:id` syntax in your e-mail template:

``` +## Custom embedders +There might be cases where you want to use a custom embedder, which can be +registered via the App container. + +Your embedder should extend the +`Eduardokum\LaravelMailAutoEmbed\Embedder\Embedder` class. + +```php +namespace App\MailEmbedders; + +class CustomEmbedder extends AttachmentInterface +{ + // your embedder code, which attaches other stuff +} +``` + +To bind the new embedder with the extension, bind it as +`mail-auto-embed.` in your `AppServiceProvider`: + +```php +use App\MailEmbedders\CustomEmbedder; + +class AppServiceProvider extends ServiceProvider +{ + /** + * Register the application services. + * + * @return void + */ + public function register() + { + // … + $this->app->bind('mail-auto-embed.custom', CustomEmbedder::class); + } +} +``` + +Lastly, either change your published `mail-auto-embed.method` to `custom`, or +use `data-auto-embed="custom"` on the applicable images you want to embed using +your embedder. ## Contributing Please feel free to submit pull requests if you can improve or add any From c3e0bfe91437de27d4117e0185ac48c84e4e1d92 Mon Sep 17 00:00:00 2001 From: Roelof Roos Date: Tue, 22 Jun 2021 14:13:21 +0200 Subject: [PATCH 3/5] Custom Embedder - Tests --- tests/FormatTest.php | 18 +++++++ tests/fixtures/CustomEmbedder.php | 16 ++++++ tests/lib/formats/html5-custom-embeds.html | 62 ++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 tests/fixtures/CustomEmbedder.php create mode 100755 tests/lib/formats/html5-custom-embeds.html diff --git a/tests/FormatTest.php b/tests/FormatTest.php index e71ad47..2b46aa6 100755 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -3,7 +3,9 @@ namespace Eduardokum\LaravelMailAutoEmbed\Tests; use Eduardokum\LaravelMailAutoEmbed\Listeners\SwiftEmbedImages; +use Eduardokum\LaravelMailAutoEmbed\Tests\fixtures\CustomEmbedder; use Eduardokum\LaravelMailAutoEmbed\Tests\Traits\InteractsWithSwift; +use Illuminate\Support\Facades\App; /** * Tests some scenarios, like HTML5 mails and mail with "invalid" HTML that mail clients @@ -42,4 +44,20 @@ public function testUserGeneratedHtml5Message() 'entity' => 'cid:', ], $message->getBody()); } + + /** + * @test + */ + public function testMixedAndCustomEmbedders() + { + App::bind('mail-auto-embed.custom', CustomEmbedder::class); + + $message = $this->handleBeforeSendPerformedEvent('formats/html5-custom-embeds.html', self::HANDLE_CONFIG); + + $this->assertEmailImageTags([ + 'default' => 'cid:', + 'custom embedder' => 'custom:', + 'invalid embedder' => 'cid:', + ], $message->getBody()); + } } diff --git a/tests/fixtures/CustomEmbedder.php b/tests/fixtures/CustomEmbedder.php new file mode 100644 index 0000000..3ce8f62 --- /dev/null +++ b/tests/fixtures/CustomEmbedder.php @@ -0,0 +1,16 @@ + + + + + + + + +
+
+

+ Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla, dolorum assumenda aliquam blanditiis, + necessitatibus mollitia delectus sapiente amet earum minima qui non deserunt quidem, doloremque + architecto voluptatem eveniet illo aperiam. +

+ +

+

+ + Lorem ipsum +
+

+
+ + + + + +
+ + + From 30fc1cbe7a077a2b705916e7503a5c9dd869adbf Mon Sep 17 00:00:00 2001 From: Roelof Roos Date: Tue, 22 Jun 2021 14:13:25 +0200 Subject: [PATCH 4/5] Custom Embedder - php-cs-fixer --- src/Listeners/SwiftEmbedImages.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Listeners/SwiftEmbedImages.php b/src/Listeners/SwiftEmbedImages.php index 2d07204..ae551be 100755 --- a/src/Listeners/SwiftEmbedImages.php +++ b/src/Listeners/SwiftEmbedImages.php @@ -157,7 +157,7 @@ private function getEmbedder($imageTag) } $embedderName = "mail-auto-embed.{$method}"; - if (!App::bound($embedderName)) { + if (! App::bound($embedderName)) { $embedderName = 'mail-auto-embed.attachment'; } From d9a978ed48b6e076bf992445717e5ad896e9e376 Mon Sep 17 00:00:00 2001 From: Roelof Roos Date: Tue, 22 Jun 2021 14:13:23 +0200 Subject: [PATCH 5/5] GitHub Actions - Pin php-cs-fixer version via shivammathur/setup-php --- .github/workflows/format_php.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/format_php.yml b/.github/workflows/format_php.yml index f676fe4..436c456 100644 --- a/.github/workflows/format_php.yml +++ b/.github/workflows/format_php.yml @@ -12,8 +12,14 @@ jobs: - name: Checkout repository uses: actions/checkout@v2 + - name: Setup PHP with php-cs-fixer + uses: shivammathur/setup-php@master + with: + php-version: '8.0' + tools: friendsofphp/php-cs-fixer:^2.19 + - name: Run php-cs-fixer - uses: docker://oskarstark/php-cs-fixer-ga + run: php-cs-fixer fix - uses: stefanzweifel/git-auto-commit-action@v4 with: