我使用sendgrid PHP发送电子邮件。这是我的代码:Sendgrid:权限被拒绝,错误凭据
require("sendgrid-php.php");
$from = new SendGrid\Email("Example User", "[email protected]");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "[email protected]");
$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 =>日期:2017年5月29日星期一17:52:44 GMT [3] => Content-Type:application/json [4] =>内容长度:88 [5] =>连接:保持活动 [6] => X-Frame-Options:DENY [7] => Access-Control-Allow-来源:https://sendgrid.api-docs.io =>访问控制 - 允许 - 方法:POST [9] =>访问控制允许标题:授权,内容类型,代表,x-sg-elas- acl => 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密钥。
[使用sendgrid发送邮件中的凭据错误]的完全重复(https://stackoverflow.com/questions/38368968/wrong-credentials-in-sending-mail-using-sendgrid) – Veve
请阅读PHP手册'getenv()':http://php.net/manual/en/function.getenv.php –