Skip to content

Commit

Permalink
embed from remote
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardokum committed Apr 13, 2020
1 parent fff797e commit 83349a6
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Embedder/AttachmentEmbedder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Eduardokum\LaravelMailAutoEmbed\Embedder;

use Illuminate\Support\Str;
use Swift_Image;
use Swift_Message;
use Swift_EmbeddedFile;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
*/
Expand Down

0 comments on commit 83349a6

Please sign in to comment.