【问题标题】:Mailgun sendMessage parameter exceptionMailgun sendMessage 参数异常
【发布时间】:2015-02-12 16:50:48
【问题描述】:

为什么函数 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>'
));

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

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

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

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

【问题讨论】:

    标签: php codeigniter mailgun


    【解决方案1】:

    要了解有关该错误的更多详细信息,请使用来自this patch 的代码:

    将实际响应消息添加到 400、401 和 404 响应代码。这提供了比 当前消息。这条信息并没有真正给你太多继续下去的机会。一世 花了几个小时试图找出我做错了什么,仔细检查我的 API 键并在 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;
    +       }
    +   }   
    

    【讨论】:

    • 非常感谢您的回复。您添加到异常中的函数给了我一个很好的错误消息,我能够解决问题。
    • @Lancashireman 很高兴听到这个消息。请将答案标记为已接受(在投票箭头下方打勾)。更多关于接受答案的信息可以在这里找到:meta.stackexchange.com/questions/5234/…
    • 新的。照顾好!
    • 我刚刚将 Mailgun 更新到 1.8,我真的希望这能解决问题。你能确认@IvanJovovic吗?这个异常正在破坏我们生产服务器上的代码,必须尽快解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2013-12-16
    相关资源
    最近更新 更多