Mailgun sendMessage参数异常

Lan*_*man 3 php codeigniter mailgun

为什么函数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>'
));
Run Code Online (Sandbox Code Playgroud)

......而我得到的例外是......

致命错误:未捕获异常'Mailgun\Connection\Exceptions\MissingRequiredParameters'及消息'传递给API的参数无效.检查你的输入!' 在第127行的C:\ wamp\www\sektor\admin\app\application\third_party\MailGun\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php

显然我发送给API的参数是错误的,但这是遵循MailGun API文档,但这显然不起作用.

我根本没有修改过Mailer类的代码.

Iva*_*vić 6

要获得有关该错误的更多详细信息,请使用此修补程序中的代码:

将实际响应消息添加到400,401和404响应代码上引发的错误.这提供了比当前消息更多有用的信息.这条消息并没有给你太多的帮助.我花了几个小时试图找到我做错了什么,仔细检查我的API密钥并在谷歌上查找错误.

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;
+       }
+   }   
Run Code Online (Sandbox Code Playgroud)