如何修复PHP / Sendgrid / Heroku中的“ 401Array {“ errors”:[{“ message”:“权限被拒绝,错误的凭据”,“ field”:null,“ help”:null}]}“”错误?

时间:2019-01-13 12:07:40

标签: php heroku sendgrid

浏览器错误为:401Array {“错误”:[{“消息”:“权限被拒绝,凭据错误”,“字段”:空,“帮助”:空}]}}

我已经编写了这段代码,以便使用heroku的sendgrid插件发送电子邮件。我不是自己写api_key的。 出什么问题了?为什么会出现这种错误?

require("sendgrid-php/sendgrid-php.php");

$from = new SendGrid\Email(null, "kesursweta@yahoo.com");
$subject = "Hello World from the SendGrid PHP Library!";
$to = new SendGrid\Email(null, "kesursweta@yahoo.com");
$content = new SendGrid\Content("text/plain", "Hello, Email!");
$mail = new SendGrid\Mail($from, $subject, $to, $content);

$apiKey = getenv('my_api_key');
$sg = new \SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo $response->headers();
echo $response->body();

1 个答案:

答案 0 :(得分:1)

首先尝试使用composer运行此代码,然后创建并设置SENDGRID_API_KEY。完成这两项任务后,您的代码将不会显示任何错误。有关参考,请查看此链接:

从此处下载作曲家:https://gist.github.com/tomysmile/3b37ab4a1ddd604093fe724d0a882166 如何创建和设置API_KEY:https://github.com/sendgrid/sendgrid-php