【发布时间】:2017-10-30 02:18:32
【问题描述】:
我正在使用 sendgrid PHP 发送电子邮件。这是我的代码:
require("sendgrid-php.php");
$from = new SendGrid\Email("Example User", "test@example.com");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "to@gmail.com");
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = getenv('SG.key......');
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo '<pre>';
print_r($response->headers());
echo '</pre>';
echo $response->body();
当我运行上面的代码时,它向我显示以下错误消息:
401 数组 ( [0] => HTTP/1.1 401 未经授权 [1] => 服务器:nginx [2] => 日期:2017 年 5 月 29 日星期一 17:52:44 GMT [3] => 内容类型:应用程序/json [4] => 内容长度:88 [5] => 连接:保持活动状态 [6] => X 帧选项:拒绝 [7] => 访问控制允许来源:https://sendgrid.api-docs.io [8] => 访问控制允许方法:POST [9] => Access-Control-Allow-Headers:授权,内容类型,代表,x-sg-elas-acl [10] => 访问控制最大年龄:600 [11] => X-No-CORS-原因:https://sendgrid.com/docs/Classroom/Basics/API/cors.html [12] => [13] => ) {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}
我不明白为什么会出现与权限相关的错误,因为我刚刚创建了 API 密钥。
【问题讨论】:
-
请阅读 PHP 手册了解
getenv()的作用:php.net/manual/en/function.getenv.php