Skip to content

Commit

Permalink
Merge pull request #42 from MotivoZwolle/feature/custom-embedders
Browse files Browse the repository at this point in the history
The answer to life, the universe and custom embedders
  • Loading branch information
eduardokum authored Feb 23, 2022
2 parents 06518c1 + d9a978e commit ce9e67a
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 8 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/format_php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,46 @@ Then, you can use the `embed:ClassName:id` syntax in your e-mail template:
</p>
```

## 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.<embedder name>` 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
Expand Down
13 changes: 6 additions & 7 deletions src/Listeners/SwiftEmbedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
16 changes: 16 additions & 0 deletions tests/fixtures/CustomEmbedder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Eduardokum\LaravelMailAutoEmbed\Tests\fixtures;

use Eduardokum\LaravelMailAutoEmbed\Embedder\AttachmentEmbedder;

class CustomEmbedder extends AttachmentEmbedder
{
/**
* @param string $url
*/
public function fromUrl($url)
{
return "custom:{$url}";
}
}
62 changes: 62 additions & 0 deletions tests/lib/formats/html5-custom-embeds.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
</head>

<body>
<main>
<article>
<p>
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.
</p>

<p>
<figure>
<!-- default --><img src="http://localhost/test.png" />
<caption>Lorem ipsum</caption>
</figure>
</p>
</article>

<aside>
<header>
<h1>Lorem Ipsum</h1>
</header>

<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore cum, blanditiis a minima aspernatur
corporis pariatur, perferendis explicabo consectetur tenetur iste, fuga possimus corrupti dolorem
laudantium sapiente sunt error autem.
</p>

<figure>
<!-- custom embedder --><img data-auto-embed="custom" src="http://localhost/test.png" />
<caption>Lorem Ipsum</caption>
</figure>
</aside>


<aside>
<header>
<h1>Lorem Ipsum Two</h1>
</header>

<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore cum, blanditiis a minima aspernatur
corporis pariatur, perferendis explicabo consectetur tenetur iste, fuga possimus corrupti dolorem
laudantium sapiente sunt error autem.
</p>

<figure>
<!-- invalid embedder --><img data-auto-embed="unknown" src="http://localhost/test.png" />
<caption>Lorem Ipsum</caption>
</figure>
</aside>
</main>
</body>

</html>

0 comments on commit ce9e67a

Please sign in to comment.