Last active
August 18, 2022 16:31
-
-
Save rvalenzuelaferrada/400b358f40a347b47ed2158ddb713144 to your computer and use it in GitHub Desktop.
Elasticemail Webhook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Models\Post; | |
use DOMDocument; | |
use Illuminate\Support\Str; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Log; | |
use Illuminate\Support\Facades\Storage; | |
class WebhookController extends Controller | |
{ | |
public function __invoke(Request $request) | |
{ | |
//guardamos todos los archivos adjuntos. | |
$this->saveFiles($request->get('Attachments')); | |
$doc = new DOMDocument(); | |
$doc->loadHTML(htmlentities($request->HtmlBody)); | |
//$this->replaceImg($doc); | |
$html = $doc->saveHTML(); | |
$html = htmlspecialchars_decode($html); | |
$post = new Post(); | |
$post->html = $html; | |
$post->save(); | |
} | |
public function saveFiles($attach) | |
{ | |
$this->attach = collect($attach); | |
} | |
public function replaceImg($doc) | |
{ | |
$images = $doc->getElementsByTagName('img'); | |
foreach ($images as $image) | |
{ | |
//guardamos el nombre de la imagen | |
$src = $image->attributes->getNamedItem('src')->nodeValue; | |
//no es una url | |
if (!filter_var($src, FILTER_VALIDATE_URL)) | |
{ | |
//es cid? de gmail | |
if (Str::startsWith($src, 'cid:')) { | |
//obtenemos la imagen del attachment que comienza con el cid | |
$file = $this->attach->firstWhere('ContentID', Str::replaceFirst('cid:', '', $src)); | |
Storage::disk('public')->put($file['Name'], base64_decode($file['Content'])); | |
$url = Storage::url($file['Name']); | |
} | |
} | |
//Guardamos el enlace al archivo que hemos creado. | |
$image->attributes->getNamedItem('src')->nodeValue = env('APP_URL') . $url; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::post('/postmarkapp/webhook', WebhookController::class); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment