From 83349a6a35560edb1c95e31205e2a848d73196ef Mon Sep 17 00:00:00 2001 From: Eduardo Gusmao Date: Mon, 13 Apr 2020 14:47:22 -0300 Subject: [PATCH] embed from remote --- src/Embedder/AttachmentEmbedder.php | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Embedder/AttachmentEmbedder.php b/src/Embedder/AttachmentEmbedder.php index a1eca28..56e57bd 100644 --- a/src/Embedder/AttachmentEmbedder.php +++ b/src/Embedder/AttachmentEmbedder.php @@ -2,6 +2,7 @@ namespace Eduardokum\LaravelMailAutoEmbed\Embedder; +use Illuminate\Support\Str; use Swift_Image; use Swift_Message; use Swift_EmbeddedFile; @@ -31,6 +32,9 @@ public function fromUrl($url) $filePath = str_replace(url('/'), public_path('/'), $url); if (!file_exists($filePath)) { + if ($embeddedFromRemoteUrl = $this->fromRemoteUrl($filePath)) { + return $embeddedFromRemoteUrl; + } return $url; } @@ -39,6 +43,33 @@ public function fromUrl($url) ); } + /** + * @param string $url + */ + public function fromRemoteUrl($url) + { + if (filter_var($url, FILTER_VALIDATE_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); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); + curl_close($ch); + + if ($httpcode == 200) { + return $this->embed( + new Swift_Image($raw, Str::random(10), $contentType) + ); + } + } + + return null; + } + /** * @param EmbeddableEntity $entity */