From 999157be306c2e1645cfc2c8f7d6359bd943a195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petar=20Pavlovi=C4=87?= Date: Tue, 14 Jun 2022 13:16:04 +0200 Subject: [PATCH] Mailgun: added better curl error handling, fixed bug with missing HTML part, added required parameters validation inside Mailgun Transport --- .editorconfig | 23 ++++++++++++++++++++++ composer.json | 4 +++- src/Client/MailgunCurlHttpClient.php | 15 ++++++-------- src/Message/Mailgun/Rest/MessageFacade.php | 9 +++++---- src/Transport/Mailgun/Rest/Mailgun.php | 17 ++++++++-------- 5 files changed, 46 insertions(+), 22 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..95f5984 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,23 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +# PHP PSR-2 Coding Standards +# http://www.php-fig.org/psr/psr-2/ + +root = true + +[*.php] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = space +indent_size = 4 + +[{Makefile,**.mk}] +# Use tabs for indentation (Makefiles require tabs) +indent_style = tab + + +[composer.json] +indent_size = 2 \ No newline at end of file diff --git a/composer.json b/composer.json index a3fa8fe..549cc6e 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,9 @@ "phpunit/phpunit": "3.7.*" }, "require": { - "php": ">=5.6", + "php": "^7.3", + "ext-json": "*", + "ext-curl": "*", "zendframework/zend-mail": "2.10.*", "zendframework/zend-servicemanager": "2.7.*" } diff --git a/src/Client/MailgunCurlHttpClient.php b/src/Client/MailgunCurlHttpClient.php index 0a642f2..c44c2bc 100644 --- a/src/Client/MailgunCurlHttpClient.php +++ b/src/Client/MailgunCurlHttpClient.php @@ -15,7 +15,6 @@ class MailgunCurlHttpClient * Send POST curl http request to provided url with provided params and headers * * @param array $params - * @param array $headers * @param string $url * @param string $token * @@ -41,17 +40,15 @@ public function post(array $params, $url, $token) $error = $this->error(); $this->close(); - if ($error) { - throw new \RuntimeException(sprintf("cURL Error #: %s\n", $error)); + if (!empty($error) || !strpos($result, 'Queued.')) { + throw new \RuntimeException(sprintf("cURL Error #: %s\n", $result)); } - $response = json_decode($result, true); - - if (!$response) { + try { + return json_decode($result, true, 512, JSON_THROW_ON_ERROR); + } catch (\Exception $e) { throw new \RuntimeException(sprintf('Empty response from %s', $url)); } - - return $response; } /** @@ -87,4 +84,4 @@ private function error() { return curl_error($this->curl); } -} \ No newline at end of file +} diff --git a/src/Message/Mailgun/Rest/MessageFacade.php b/src/Message/Mailgun/Rest/MessageFacade.php index f972edd..f2cc71f 100644 --- a/src/Message/Mailgun/Rest/MessageFacade.php +++ b/src/Message/Mailgun/Rest/MessageFacade.php @@ -15,12 +15,13 @@ public static function convert(\G4\Mailer\Message $message, array $options) 'from' => $message->getFrom(), 'to' => is_array($message->getTo()) ? $message->getTo()[0] : $message->getTo(), 'subject' => $message->getSubject(), - 'text' => $message->getTextBody() + 'text' => $message->getTextBody(), + 'html' => $message->getHtmlBody(), ]; - $url = $options['url']; - $token = $options['token']; + $url = sprintf($options['params']['url'], $options['params']['domain']); + $token = $options['params']['token']; return new Message($body, $url, $token); } -} \ No newline at end of file +} diff --git a/src/Transport/Mailgun/Rest/Mailgun.php b/src/Transport/Mailgun/Rest/Mailgun.php index f120915..008bfa5 100644 --- a/src/Transport/Mailgun/Rest/Mailgun.php +++ b/src/Transport/Mailgun/Rest/Mailgun.php @@ -27,7 +27,7 @@ public function send(\G4\Mailer\Message $message) $mailgunMessage->getToken() ); } catch (\Exception $exception) { - if ($exception->getMessage() !== sprintf('Empty response from %s', $this->options['url'])) { + if ($exception->getMessage() !== sprintf('Empty response from %s', $mailgunMessage->getUrl())) { throw new MailgunMailNotSentException(sprintf('Email not sent. Reason: %s', $exception->getMessage()), $exception->getCode()); } } @@ -35,14 +35,15 @@ public function send(\G4\Mailer\Message $message) private function setOptions($options) { - if (!isset($options['url'])) { - throw new \InvalidArgumentException('url not defined'); + if (!isset($options['params']['url'])) { + throw new \InvalidArgumentException('service entpoint url not defined'); } - - if (!isset($options['token'])) { - throw new \InvalidArgumentException('token not defined'); + if (!isset($options['params']['domain'])) { + throw new \InvalidArgumentException('sending domain not defined'); + } + if (!isset($options['params']['token'])) { + throw new \InvalidArgumentException('token for a domain not defined'); } - $this->options = $options; } -} \ No newline at end of file +}