Mailgun sendMessage parameter exception

这一生的挚爱 提交于 2019-12-10 10:25:17

问题


Why is the function sendMessage() throwing an exception here?

$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>'
));

...and the exception I am getting is...

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

Apparently the parameters I am sending to the API are wrong but this is following the MailGun API documentation but this clearly doesn't work.

I haven't modified the code of the Mailer class at all.


回答1:


To get a bit more details about that error, use the code from 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.

Change source file src/Mailgun/Connection/RestClient.php like this (full patch is at https://github.com/mailgun/mailgun-php/pull/72/files):

When throwing exception EXCEPTION_MISSING_REQUIRED_PARAMETERS, get more info with the method getResponseExceptionMessage() (note + and - signs in front of added and removed lines):

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


来源:https://stackoverflow.com/questions/28483155/mailgun-sendmessage-parameter-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!