3

我正在做一个项目,我需要向我的客户发送交易电子邮件。

发送请求时出现以下错误。我使用 SendinBlue V3 SDK。

Exception when calling TransactionalEmailsApi->sendTransacEmail: Connection refused for URI
https://api.sendinblue.com/v3/smtp/email

我的方法代码:

$config = Configuration::getDefaultConfiguration()->setApiKey('api-key', 'secret');
$apiInstance = new TransactionalEmailsApi(
    new GuzzleHttp\Client([
        'base_uri' => 'https://uplann.online'
    ]),
    $config
);
$sendSmtpEmail = new SendSmtpEmail();
$sendSmtpEmail['to'] = [
    [
        'email' => $parameters['mail'],
        'name' => $parameters['user'],
    ]
];
$sendSmtpEmail['templateId'] = 3;
$sendSmtpEmail['params'] = [
    'FIRSTNAME' => $parameters['user'],
    'LASTNAME' => $parameters['verification_code'],
];

try {
    $result = $apiInstance->sendTransacEmail($sendSmtpEmail);
    print_r($result);
} catch (ConnectException $e) {
    echo 'Exception when calling TransactionalEmailsApi->sendTransacEmail: ', $e->getMessage(), PHP_EOL;
}
4

2 回答 2

3

您是否使用 Guzzle 正确配置了 https 客户端?如果没有连接,那么您可以检查是否引发了任何其他异常。您在 Guzzle 文档中有大量库存:https ://docs.guzzlephp.org/en/stable/quickstart.html#exceptions

. \RuntimeException
└── TransferException (implements GuzzleException)
    ├── ConnectException (implements NetworkExceptionInterface)
    └── RequestException
        ├── BadResponseException
        │   ├── ServerException
        │   └── ClientException
        └── TooManyRedirectsException

在你的情况下,它是一个ConnectException. 尝试获取有关它的更多信息。

为什么ConnectException通常会被抛出?无法连接时。

什么时候不能连接?当您没有使用正确的端点 url 时,没有将您的 HTTP 客户端配置为使用 HTTPS 协议,没有根据您可以在官方文档中找到的规范使用端点。

您也可以查看官方教程:https ://developers.sendinblue.com/docs/send-a-transactional-email

您也可以将 github.com sendinblue 官方存储库与 php api 一起使用:https ://github.com/sendinblue/APIv3-php-library

于 2022-03-04T18:53:54.883 回答
2

文件说: 中继访问被拒绝 身份验证可能失败:检查您的 API 密钥,

还请确保您的 587 端口已打开。

运行php artisan cache:clearphp artisan config:clear

于 2022-03-04T18:27:13.107 回答