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: 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 diff --git a/src/Listeners/SwiftEmbedImages.php b/src/Listeners/SwiftEmbedImages.php index 432f85f..ae551be 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); } /** 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 +
+

+
+ + + + + +
+ + +