我正在使用 sendgrid PHP 发送电子邮件。这是我的代码:
require("sendgrid-php.php");
$from = new SendGrid\Email("Example User", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88fcedfbfcc8edf0e9e5f8e4eda6ebe7e5" rel="noreferrer noopener nofollow">[email protected]</a>");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4c0dbf4d3d9d5ddd89ad7dbd9" rel="noreferrer noopener nofollow">[email protected]</a>");
$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 Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Mon, 29 May 2017 17:52:44 GMT [3] => Content-Type: application/json [4] => Content-Length: 88 [5] => Connection: keep-alive [6] => X-Frame-Options: DENY [7] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io [8] => Access-Control-Allow-Methods: POST [9] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl [10] => Access-Control-Max-Age: 600 [11] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html [12] => [13] => ) {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}
我不明白为什么会收到与权限相关的错误,因为我刚刚创建了 API key 。
最佳答案
这很常见。理解和做起来几乎不需要一个小时。您需要设置 env 或使用用户名密码身份验证系统。请阅读official document on Github , documentation here和 documentation here 。
关于php - Sendgrid:权限被拒绝,凭据错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44248138/