Skip to content

Commit a0a9a85

Browse files
authored
Merge pull request #17 from ppavlovic/master
Mailgun: added better curl error handling, fixed bug with missing HTML part, added required parameters validation inside Mailgun Transport
2 parents 685d90d + 999157b commit a0a9a85

5 files changed

Lines changed: 46 additions & 22 deletions

File tree

.editorconfig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
# PHP PSR-2 Coding Standards
5+
# http://www.php-fig.org/psr/psr-2/
6+
7+
root = true
8+
9+
[*.php]
10+
charset = utf-8
11+
end_of_line = lf
12+
insert_final_newline = true
13+
trim_trailing_whitespace = true
14+
indent_style = space
15+
indent_size = 4
16+
17+
[{Makefile,**.mk}]
18+
# Use tabs for indentation (Makefiles require tabs)
19+
indent_style = tab
20+
21+
22+
[composer.json]
23+
indent_size = 2

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"phpunit/phpunit": "3.7.*"
2828
},
2929
"require": {
30-
"php": ">=5.6",
30+
"php": "^7.3",
31+
"ext-json": "*",
32+
"ext-curl": "*",
3133
"zendframework/zend-mail": "2.10.*",
3234
"zendframework/zend-servicemanager": "2.7.*"
3335
}

src/Client/MailgunCurlHttpClient.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class MailgunCurlHttpClient
1515
* Send POST curl http request to provided url with provided params and headers
1616
*
1717
* @param array $params
18-
* @param array $headers
1918
* @param string $url
2019
* @param string $token
2120
*
@@ -41,17 +40,15 @@ public function post(array $params, $url, $token)
4140
$error = $this->error();
4241
$this->close();
4342

44-
if ($error) {
45-
throw new \RuntimeException(sprintf("cURL Error #: %s\n", $error));
43+
if (!empty($error) || !strpos($result, 'Queued.')) {
44+
throw new \RuntimeException(sprintf("cURL Error #: %s\n", $result));
4645
}
4746

48-
$response = json_decode($result, true);
49-
50-
if (!$response) {
47+
try {
48+
return json_decode($result, true, 512, JSON_THROW_ON_ERROR);
49+
} catch (\Exception $e) {
5150
throw new \RuntimeException(sprintf('Empty response from %s', $url));
5251
}
53-
54-
return $response;
5552
}
5653

5754
/**
@@ -87,4 +84,4 @@ private function error()
8784
{
8885
return curl_error($this->curl);
8986
}
90-
}
87+
}

src/Message/Mailgun/Rest/MessageFacade.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ public static function convert(\G4\Mailer\Message $message, array $options)
1515
'from' => $message->getFrom(),
1616
'to' => is_array($message->getTo()) ? $message->getTo()[0] : $message->getTo(),
1717
'subject' => $message->getSubject(),
18-
'text' => $message->getTextBody()
18+
'text' => $message->getTextBody(),
19+
'html' => $message->getHtmlBody(),
1920
];
2021

21-
$url = $options['url'];
22-
$token = $options['token'];
22+
$url = sprintf($options['params']['url'], $options['params']['domain']);
23+
$token = $options['params']['token'];
2324

2425
return new Message($body, $url, $token);
2526
}
26-
}
27+
}

src/Transport/Mailgun/Rest/Mailgun.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,23 @@ public function send(\G4\Mailer\Message $message)
2727
$mailgunMessage->getToken()
2828
);
2929
} catch (\Exception $exception) {
30-
if ($exception->getMessage() !== sprintf('Empty response from %s', $this->options['url'])) {
30+
if ($exception->getMessage() !== sprintf('Empty response from %s', $mailgunMessage->getUrl())) {
3131
throw new MailgunMailNotSentException(sprintf('Email not sent. Reason: %s', $exception->getMessage()), $exception->getCode());
3232
}
3333
}
3434
}
3535

3636
private function setOptions($options)
3737
{
38-
if (!isset($options['url'])) {
39-
throw new \InvalidArgumentException('url not defined');
38+
if (!isset($options['params']['url'])) {
39+
throw new \InvalidArgumentException('service entpoint url not defined');
4040
}
41-
42-
if (!isset($options['token'])) {
43-
throw new \InvalidArgumentException('token not defined');
41+
if (!isset($options['params']['domain'])) {
42+
throw new \InvalidArgumentException('sending domain not defined');
43+
}
44+
if (!isset($options['params']['token'])) {
45+
throw new \InvalidArgumentException('token for a domain not defined');
4446
}
45-
4647
$this->options = $options;
4748
}
48-
}
49+
}

0 commit comments

Comments
 (0)