invoiceninja/app/Helpers/IngresMail/Transformer/MailgunInboundWebhookTransformer.php
2024-03-19 07:39:35 +01:00

48 lines
1.8 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Helpers\IngresMail\Transformer;
use App\Services\IngresEmail\IngresEmail;
use App\Utils\TempFile;
use Illuminate\Support\Carbon;
class MailgunInboundWebhookTransformer
{
public function transform($data)
{
$ingresEmail = new IngresEmail();
$ingresEmail->from = $data["sender"]; // TODO: maybe a fallback have to be used to extract email from $data["From"]
$ingresEmail->to = $data["recipient"]; // TODO: maybe a fallback have to be used to extract email from $data["To"]
$ingresEmail->subject = $data["Subject"];
$ingresEmail->body = $data["body-html"];
$ingresEmail->text_body = $data["body-plain"];
$ingresEmail->date = Carbon::createFromTimestamp((int) $data["timestamp"]);
// parse documents as UploadedFile from webhook-data
foreach (json_decode($data["attachments"]) as $attachment) {
// prepare url with credentials before downloading :: https://github.com/mailgun/mailgun.js/issues/24
$url = $attachment->url;
$credentials = config('services.mailgun.domain') . ":" . config('services.mailgun.secret') . "@";
$url = str_replace("http://", "http://" . $credentials, $url);
$url = str_replace("https://", "https://" . $credentials, $url);
// download file and save to tmp dir
$ingresEmail->documents[] = TempFile::UploadedFileFromUrl($url, $attachment->name, $attachment->{"content-type"});
}
return $ingresEmail;
}
}