diff --git a/app/Http/Controllers/PostMarkController.php b/app/Http/Controllers/PostMarkController.php index 1f59240a2d73..a6877b1e250c 100644 --- a/app/Http/Controllers/PostMarkController.php +++ b/app/Http/Controllers/PostMarkController.php @@ -12,6 +12,7 @@ namespace App\Http\Controllers; use App\Jobs\PostMark\ProcessPostmarkWebhook; +use App\Libraries\MultiDB; use App\Services\InboundMail\InboundMail; use App\Services\InboundMail\InboundMailEngine; use App\Utils\TempFile; @@ -287,10 +288,16 @@ class PostMarkController extends BaseController if ($inboundEngine->isInvalidOrBlocked($input["From"], $input["To"])) { Log::info('Failed: Sender is blocked: ' . $input["From"] . " Recipient: " . $input["To"]); - $inboundEngine->saveMeta($input["From"], $input["To"]); return response()->json(['message' => 'Blocked.'], 403); } + $company = MultiDB::findAndSetDbByExpenseMailbox($input["To"]); + if (!$company) { + Log::info('[PostmarkInboundWebhook] unknown Expense Mailbox occured while handling an inbound email from mailgun: ' . $input["To"]); + $inboundEngine->saveMeta($input["From"], $input["To"], true); // important to save this, to protect from spam + return; + } + try { // important to save meta if something fails here to prevent spam // prepare data for ingresEngine @@ -316,7 +323,7 @@ class PostMarkController extends BaseController } // perform - $inboundEngine->handle($inboundMail); + $inboundEngine->handleExpenseMailbox($inboundMail); return response()->json(['message' => 'Success'], 200); } diff --git a/app/Jobs/Brevo/ProcessBrevoInboundWebhook.php b/app/Jobs/Brevo/ProcessBrevoInboundWebhook.php index d6fc40e6f4a0..be11b4905f0f 100644 --- a/app/Jobs/Brevo/ProcessBrevoInboundWebhook.php +++ b/app/Jobs/Brevo/ProcessBrevoInboundWebhook.php @@ -31,6 +31,8 @@ class ProcessBrevoInboundWebhook implements ShouldQueue public $tries = 1; + private InboundMailEngine $engine = new InboundMailEngine(); + /** * Create a new job instance. * @@ -123,7 +125,7 @@ class ProcessBrevoInboundWebhook implements ShouldQueue foreach ($this->input["Recipients"] as $recipient) { // Spam protection - if ((new InboundMailEngine())->isInvalidOrBlocked($this->input["From"]["Address"], $recipient)) { + if ($this->engine->isInvalidOrBlocked($this->input["From"]["Address"], $recipient)) { Log::info('Failed: Sender is blocked: ' . $this->input["From"]["Address"] . " Recipient: " . $recipient); throw new \Error('Sender is blocked'); } @@ -132,7 +134,7 @@ class ProcessBrevoInboundWebhook implements ShouldQueue $company = MultiDB::findAndSetDbByExpenseMailbox($recipient); if (!$company) { Log::info('[ProcessBrevoInboundWebhook] unknown Expense Mailbox occured while handling an inbound email from brevo: ' . $recipient); - (new InboundMailEngine())->saveMeta($this->input["From"]["Address"], $recipient); // important to save this, to protect from spam + // $this->engine->saveMeta($this->input["From"]["Address"], $recipient, true); // @turbo124 disabled, because recipents contains all recipients, and will likly result in false bans?! => normally important to save this, to protect from spam continue; } @@ -186,11 +188,11 @@ class ProcessBrevoInboundWebhook implements ShouldQueue } } catch (\Exception $e) { - (new InboundMailEngine())->saveMeta($this->input["From"]["Address"], $recipient); // important to save this, to protect from spam + $this->engine->saveMeta($this->input["From"]["Address"], $recipient); // important to save this, to protect from spam throw $e; } - (new InboundMailEngine())->handle($inboundMail); + $this->engine->handleExpenseMailbox($inboundMail); } } diff --git a/app/Jobs/Mailgun/ProcessMailgunInboundWebhook.php b/app/Jobs/Mailgun/ProcessMailgunInboundWebhook.php index 1e786f80eeac..3ac892d8d081 100644 --- a/app/Jobs/Mailgun/ProcessMailgunInboundWebhook.php +++ b/app/Jobs/Mailgun/ProcessMailgunInboundWebhook.php @@ -29,6 +29,8 @@ class ProcessMailgunInboundWebhook implements ShouldQueue public $tries = 1; + private InboundMailEngine $engine = new InboundMailEngine(); + /** * Create a new job instance. * $input consists of 3 informations: sender/from|recipient/to|messageUrl @@ -170,7 +172,7 @@ class ProcessMailgunInboundWebhook implements ShouldQueue // $messageId = explode("|", $this->input)[2]; // used as base in download function // Spam protection - if ((new InboundMailEngine())->isInvalidOrBlocked($from, $to)) { + if ($this->engine->isInvalidOrBlocked($from, $to)) { Log::info('Failed: Sender is blocked: ' . $from . " Recipient: " . $to); throw new \Error('Sender is blocked'); } @@ -179,7 +181,7 @@ class ProcessMailgunInboundWebhook implements ShouldQueue $company = MultiDB::findAndSetDbByExpenseMailbox($to); if (!$company) { Log::info('[ProcessMailgunInboundWebhook] unknown Expense Mailbox occured while handling an inbound email from mailgun: ' . $to); - (new InboundMailEngine())->saveMeta($from, $to); // important to save this, to protect from spam + $this->engine->saveMeta($from, $to, true); // important to save this, to protect from spam return; } @@ -276,11 +278,11 @@ class ProcessMailgunInboundWebhook implements ShouldQueue } } catch (\Exception $e) { - (new InboundMailEngine())->saveMeta($from, $to); // important to save this, to protect from spam + $this->engine->saveMeta($from, $to); // important to save this, to protect from spam throw $e; } // perform - (new InboundMailEngine())->handle($inboundMail); + $this->engine->handleExpenseMailbox($inboundMail); } } diff --git a/app/Services/InboundMail/InboundMailEngine.php b/app/Services/InboundMail/InboundMailEngine.php index cdf1fe177fb7..f5deef5a874b 100644 --- a/app/Services/InboundMail/InboundMailEngine.php +++ b/app/Services/InboundMail/InboundMailEngine.php @@ -46,21 +46,20 @@ class InboundMailEngine * if there is not a company with an matching mailbox, we only do monitoring * reuse this method to add more mail-parsing behaviors */ - public function handle(InboundMail $email) + public function handleExpenseMailbox(InboundMail $email) { if ($this->isInvalidOrBlocked($email->from, $email->to)) return; - $isUnknownRecipent = true; - // Expense Mailbox => will create an expense $company = MultiDB::findAndSetDbByExpenseMailbox($email->to); - if ($company) { - $isUnknownRecipent = false; - $this->createExpense($company, $email); + if (!$company) { + $this->saveMeta($email->from, $email->to, true); + return; } - $this->saveMeta($email->from, $email->to, $isUnknownRecipent); + $this->createExpense($company, $email); + $this->saveMeta($email->from, $email->to); } // SPAM Protection