diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index b8be1aff4..dce526423 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -74,6 +74,14 @@ class Handler extends ExceptionHandler */ public function report(Throwable $exception) { + if ($exception instanceof MailerHttpTransportException) { + $email = $this->handleMailerExceptions($exception); + + if (! empty($email)) { + return; + } + } + parent::report($exception); } @@ -197,23 +205,20 @@ class Handler extends ExceptionHandler } if ($exception instanceof MailerHttpTransportException) { - /** - * Couldn't access the SentMessage object to get the email address - * https://symfony.com/doc/current/mailer.html#debugging-emails - * - * https://codespeedy.com/extract-email-addresses-from-a-string-in-php - * https://phpliveregex.com/p/IMG - */ - preg_match("/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/", $exception->getMessage(), $matches); + $email = $this->handleMailerExceptions($exception); - if (! empty($matches[0])) { - event(new InvalidEmailDetected($matches[0], $exception->getMessage())); + if (! empty($email)) { + $message = trans('notifications.menu.invalid_email.description', ['email' => $email]); if ($request->ajax()) { return response()->json([ - 'error' => trans('notifications.menu.invalid_email.description', ['email' => $matches[0]]), + 'error' => $message, ], $exception->getCode()); } + + return response()->view('errors.403', [ + 'message' => $message, + ], $exception->getCode()); } } @@ -237,6 +242,28 @@ class Handler extends ExceptionHandler return new Response($response, $this->getStatusCode($exception), $this->getHeaders($exception)); } + protected function handleMailerExceptions(MailerHttpTransportException $exception): string + { + /** + * Couldn't access the SentMessage object to get the email address + * https://symfony.com/doc/current/mailer.html#debugging-emails + * + * https://codespeedy.com/extract-email-addresses-from-a-string-in-php + * https://phpliveregex.com/p/IMG + */ + preg_match("/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/", $exception->getMessage(), $matches); + + if (empty($matches[0])) { + return ''; + } + + $email = $matches[0]; + + event(new InvalidEmailDetected($email, $exception->getMessage())); + + return $email; + } + /** * Prepare the replacements array by gathering the keys and values. *