对于PHP SDK,Mailgun会抛出致命错误而不是异常

时间:2017-11-30 10:26:15

标签: php mailgun

我正在尝试使用Mailgun API服务器来处理我的电子邮件,这是我的代码根据文档:

的index.php

require 'vendor/autoload.php';
use Mailgun\Mailgun;

$mgClient = Mailgun::create("key-xxxxxxxxxxxxxxxxxxxxxxxxxxx"); 
$domain = "xxxxxxx.com"; 


$mgClient->sendMessage($domain, 
                       array('from' => 'Dwight Schrute<dwight@xxxxxxx.com>', 
                             'to' => 'Michael Scott <xxxx@xxxxx.com>', 
                             'subject' => 'The Printer Caught Fire', 
                             'text' => 'We have a problem.'));

var_dump($result);

composer.json

 "require": {
        "mailgun/mailgun-php": "^2.3",
        "php-http/curl-client": "^1.7",
        "guzzlehttp/psr7": "^1.4"
    }

一切正常但$ result包含大量数据,而不是简单的返回数组,其中包含id和消息“消息已排队谢谢”。另一个问题是当我模拟错误以查看发生了什么时,不幸的是Mailgun会发出致命错误而不是我可以处理的连接异常:

[30-Nov-2017 10:05:44 UTC] PHP Fatal error:  Uncaught exception 'Mailgun\Connection\Exceptions\MissingRequiredParameters' with message 'The parameters passed to the API were invalid. Check your inputs! Free accounts are for test purposes only. Please upgrade or add the address to authorized recipients in Account Settings.' in /home/xxx/xxx/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php:254
Stack trace:
#0 /home/xxx/xxx/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(110): Mailgun\Connection\RestClient->responseHandler(Object(GuzzleHttp\Psr7\Response))
#1 /home/xxx/xxx/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(179): Mailgun\Connection\RestClient->send('POST', 'xxxxx.com...', Array, Array)
#2 /home/xxx/xxx/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php(204): Mailgun\Connection\RestClient->post('xxxxx.com...', Array, Array)
#3 /home/xxx/xxx/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php(135): Mailgun\Mailgun->post(' in /home/xxxxxx/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php on line 254

我使用PHP 7 os linux没有框架。我做错了什么?谢谢你的任何建议。

1 个答案:

答案 0 :(得分:1)

只要使用沙箱域或免费帐户而不邀请名为“授权收件人”的用户,就会发生此错误。免费计划旨在用于测试,所有自定义域都需要授权收件人。通过升级计划,自定义域不再需要授权收件人。

有关授权收件人流程的详细信息,请查看以下帮助中心文章:

https://help.mailgun.com/hc/en-us/articles/217531258-Authorized-Recipients

有关升级的更多信息,请查看:

https://help.mailgun.com/hc/en-us/articles/203599840-How-do-I-upgrade-my-account-

免责声明我在Mailgun工作