Skip to content

Commit

Permalink
Merge pull request #17 from ppavlovic/master
Browse files Browse the repository at this point in the history
Mailgun: added better curl error handling, fixed bug with missing HTML part, added required parameters validation inside Mailgun Transport
  • Loading branch information
ppavlovic committed Jun 14, 2022
2 parents 685d90d + 999157b commit a0a9a85
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
23 changes: 23 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
}
Expand Down
15 changes: 6 additions & 9 deletions src/Client/MailgunCurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -87,4 +84,4 @@ private function error()
{
return curl_error($this->curl);
}
}
}
9 changes: 5 additions & 4 deletions src/Message/Mailgun/Rest/MessageFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
17 changes: 9 additions & 8 deletions src/Transport/Mailgun/Rest/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@ 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());
}
}
}

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;
}
}
}

0 comments on commit a0a9a85

Please sign in to comment.