diff --git a/composer.json b/composer.json index 26c6006..146dff8 100755 --- a/composer.json +++ b/composer.json @@ -14,16 +14,17 @@ } ], "require": { - "php": "^7.2|^8.0", - "ext-dom": "*", - "illuminate/contracts": "^6.0|^7.0|^8.0", - "illuminate/support": "^6.0|^7.0|^8.0", - "illuminate/mail": "^6.0|^7.0|^8.0", + "php": "^7.2|^8.0", + "ext-dom": "*", + "illuminate/contracts": "^6.0|^7.0|^8.0", + "illuminate/support": "^6.0|^7.0|^8.0", + "illuminate/mail": "^6.0|^7.0|^8.0", "squizlabs/php_codesniffer": "^3.5", - "masterminds/html5": "^2.0" + "masterminds/html5": "^2.0", + "ext-curl": "*", + "ext-fileinfo": "*" }, "require-dev": { - "php": ">=7.0|^8.0", "orchestra/testbench": "^4.0|^5.0|^6.0", "phpunit/phpunit": "^8.0|^9.0" }, diff --git a/src/Embedder/AttachmentEmbedder.php b/src/Embedder/AttachmentEmbedder.php index e1f50cc..e08b8a5 100755 --- a/src/Embedder/AttachmentEmbedder.php +++ b/src/Embedder/AttachmentEmbedder.php @@ -3,7 +3,6 @@ namespace Eduardokum\LaravelMailAutoEmbed\Embedder; use Eduardokum\LaravelMailAutoEmbed\Models\EmbeddableEntity; -use Illuminate\Support\Str; use Swift_EmbeddedFile; use Swift_Image; use Swift_Message; @@ -41,6 +40,11 @@ public function fromUrl($url) return $url; } + /** + * @param $path + * + * @return string + */ public function fromPath($path) { if (file_exists($path)) { @@ -66,15 +70,6 @@ public function fromEntity(EmbeddableEntity $entity) ); } - /** - * @param Swift_EmbeddedFile $attachment - * @return string - */ - protected function embed(Swift_EmbeddedFile $attachment) - { - return $this->message->embed($attachment); - } - /** * @param string $url */ @@ -84,7 +79,6 @@ public function fromRemoteUrl($url) $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $raw = curl_exec($ch); @@ -104,5 +98,15 @@ public function fromRemoteUrl($url) ); } } + return $url; + } + + /** + * @param Swift_EmbeddedFile $attachment + * @return string + */ + protected function embed(Swift_EmbeddedFile $attachment) + { + return $this->message->embed($attachment); } } diff --git a/src/Embedder/Base64Embedder.php b/src/Embedder/Base64Embedder.php index 22b2fd1..77721f9 100755 --- a/src/Embedder/Base64Embedder.php +++ b/src/Embedder/Base64Embedder.php @@ -3,9 +3,6 @@ namespace Eduardokum\LaravelMailAutoEmbed\Embedder; use Eduardokum\LaravelMailAutoEmbed\Models\EmbeddableEntity; -use Illuminate\Support\Str; -use Swift_EmbeddedFile; -use Swift_Image; class Base64Embedder extends Embedder { @@ -27,6 +24,11 @@ public function fromUrl($url) return $url; } + /** + * @param $path + * + * @return string + */ public function fromPath($path) { if (file_exists($path)) { @@ -45,15 +47,6 @@ public function fromEntity(EmbeddableEntity $entity) return $this->base64String($entity->getMimeType(), $entity->getRawContent()); } - /** - * @param string $mimeType - * @param mixed $content - */ - private function base64String($mimeType, $content) - { - return 'data:'.$mimeType.';base64,'.base64_encode($content); - } - /** * @param string $url */ @@ -63,7 +56,6 @@ public function fromRemoteUrl($url) $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $raw = curl_exec($ch); @@ -75,5 +67,16 @@ public function fromRemoteUrl($url) return $this->base64String($contentType, $raw); } } + + return $url; + } + + /** + * @param string $mimeType + * @param mixed $content + */ + private function base64String($mimeType, $content) + { + return 'data:'.$mimeType.';base64,'.base64_encode($content); } } diff --git a/src/Listeners/SwiftEmbedImages.php b/src/Listeners/SwiftEmbedImages.php index ae551be..c4ed612 100755 --- a/src/Listeners/SwiftEmbedImages.php +++ b/src/Listeners/SwiftEmbedImages.php @@ -8,7 +8,6 @@ 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; @@ -55,7 +54,7 @@ public function sendPerformed(Swift_Events_SendEvent $evt) } /** - * Attaches images by parsing the HTML document. + * */ private function attachImages() { @@ -80,13 +79,19 @@ private function attachImages() // Replace body $this->message->setBody($parser->saveHTML($document)); + +// $html_body = $this->message->getBody(); +// + /* $html_body = preg_replace_callback('//', [$this, 'replaceCallback'], $html_body);*/ +// +// $this->message->setBody($html_body); } /** * @param DOMDocument $document * @return bool */ - private function shouldSkipDocument($document) + private function shouldSkipDocument(DOMDocument $document) { if ($document->childNodes->count() != 1) { return false; @@ -101,9 +106,8 @@ private function shouldSkipDocument($document) /** * @param DOMDocument $document - * @return string */ - private function attachImagesToDom(&$document) + private function attachImagesToDom(DOMDocument &$document) { foreach ($document->getElementsByTagName('img') as $image) { \assert($image instanceof DOMElement); @@ -130,7 +134,7 @@ private function attachImagesToDom(&$document) * @param DOMElement $imageTag * @return bool */ - private function needsEmbed($imageTag) + private function needsEmbed(DOMElement $imageTag) { // Don't embed if 'data-skip-embed' is present if ($imageTag->hasAttribute('data-skip-embed')) { @@ -146,22 +150,23 @@ private function needsEmbed($imageTag) } /** - * @param DOMElement $imageTag + * @param DOMElement $imageTag * @return Embedder */ - private function getEmbedder($imageTag) + private function getEmbedder(DOMElement $imageTag) { $method = $imageTag->getAttribute('data-auto-embed'); if (empty($method)) { $method = $this->config['method']; } - $embedderName = "mail-auto-embed.{$method}"; - if (! App::bound($embedderName)) { - $embedderName = 'mail-auto-embed.attachment'; + switch ($method) { + case 'attachment': + default: + return new AttachmentEmbedder($this->message); + case 'base64': + return new Base64Embedder(); } - - return App::make($embedderName, [$this->message]); } /** @@ -173,6 +178,7 @@ private function embed(Embedder $embedder, $src) { // Entity embedding if (strpos($src, 'embed:') === 0) { + $embedParams = explode(':', $src); if (count($embedParams) < 3) { return $src; @@ -181,12 +187,12 @@ private function embed(Embedder $embedder, $src) $className = urldecode($embedParams[1]); $id = $embedParams[2]; - if (! class_exists($className)) { + if (!class_exists($className)) { return $src; } $class = new ReflectionClass($className); - if (! $class->implementsInterface(EmbeddableEntity::class)) { + if (! $class->implementsInterface(EmbeddableEntity::class) ) { return $src; } @@ -203,20 +209,6 @@ private function embed(Embedder $embedder, $src) return $embedder->fromUrl($src); } - // Path embedding - $publicPath = public_path($src); - $appPath = app_path($src); - $storagePath = storage_path($src); - if (file_exists($src)) { - return $embedder->fromPath($src); - } elseif (file_exists($publicPath)) { // Try to guess where the file is at that priority level - return $embedder->fromPath($publicPath); - } elseif (file_exists($appPath)) { - return $embedder->fromPath($appPath); - } elseif (file_exists($storagePath)) { - return $embedder->fromPath($storagePath); - } - return $src; } } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 3c8123c..ca1a956 100755 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -30,10 +30,6 @@ 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); } /**