Mailgun sendMessage参数异常
Mailgun sendMessage parameter exception
为什么函数 sendMessage() 在这里抛出异常?
$mg = new MailGun('my_actual_api_key');
$response = $mg->sendMessage('my-domain.com', array(
'from' => 'real@email.com',
'to' => 'real@email.com',
'subject' => 'Test',
'html' => '<h1>Test body</h2>'
));
...我得到的异常是...
Fatal error: Uncaught exception'Mailgun\Connection\Exceptions\MissingRequiredParameters' with message 'The parameters passed to the API were invalid. Check your inputs!' in C:\wamp\www\sektor\admin\app\application\third_party\MailGun\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php on line 127
显然我发送给 API 的参数是错误的,但这是遵循 MailGun API 文档,但这显然不起作用。
我根本没有修改Mailer的代码class。
要获得有关该错误的更多详细信息,请使用 this patch 中的代码:
Adds the actual response message to the errors thrown on 400, 401 and
404 response codes. This provides a lot more useful info than the
current messages. The message doesn’t really give you much to go on. I
spent hours trying to find what I did wrong, double checking my API
keys and looking up the error on google.
像这样更改源文件 src/Mailgun/Connection/RestClient.php
(完整补丁位于 https://github.com/mailgun/mailgun-php/pull/72/files):
抛出异常 EXCEPTION_MISSING_REQUIRED_PARAMETERS
时,使用方法 getResponseExceptionMessage()
获取更多信息(注意添加和删除行前面的 + 和 - 符号):
elseif($httpResponseCode == 400){
- throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS);
+ throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj));
}
/**
+ * @param \Guzzle\Http\Message\Response $responseObj
+ * @return string
+ */
+ protected function getResponseExceptionMessage(\Guzzle\Http\Message\Response $responseObj){
+ $body = (string)$responseObj->getBody();
+ $response = json_decode($body);
+ if (json_last_error() == JSON_ERROR_NONE && isset($response->message)) {
+ return " " . $response->message;
+ }
+ }
为什么函数 sendMessage() 在这里抛出异常?
$mg = new MailGun('my_actual_api_key');
$response = $mg->sendMessage('my-domain.com', array(
'from' => 'real@email.com',
'to' => 'real@email.com',
'subject' => 'Test',
'html' => '<h1>Test body</h2>'
));
...我得到的异常是...
Fatal error: Uncaught exception'Mailgun\Connection\Exceptions\MissingRequiredParameters' with message 'The parameters passed to the API were invalid. Check your inputs!' in C:\wamp\www\sektor\admin\app\application\third_party\MailGun\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php on line 127
显然我发送给 API 的参数是错误的,但这是遵循 MailGun API 文档,但这显然不起作用。
我根本没有修改Mailer的代码class。
要获得有关该错误的更多详细信息,请使用 this patch 中的代码:
Adds the actual response message to the errors thrown on 400, 401 and 404 response codes. This provides a lot more useful info than the current messages. The message doesn’t really give you much to go on. I spent hours trying to find what I did wrong, double checking my API keys and looking up the error on google.
像这样更改源文件 src/Mailgun/Connection/RestClient.php
(完整补丁位于 https://github.com/mailgun/mailgun-php/pull/72/files):
抛出异常 EXCEPTION_MISSING_REQUIRED_PARAMETERS
时,使用方法 getResponseExceptionMessage()
获取更多信息(注意添加和删除行前面的 + 和 - 符号):
elseif($httpResponseCode == 400){
- throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS);
+ throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj));
}
/**
+ * @param \Guzzle\Http\Message\Response $responseObj
+ * @return string
+ */
+ protected function getResponseExceptionMessage(\Guzzle\Http\Message\Response $responseObj){
+ $body = (string)$responseObj->getBody();
+ $response = json_decode($body);
+ if (json_last_error() == JSON_ERROR_NONE && isset($response->message)) {
+ return " " . $response->message;
+ }
+ }