【发布时间】:2016-11-17 01:29:05
【问题描述】:
这是使用 sendgrid 发送电子邮件的代码,我有正确的 api 密钥,但浏览器仍显示错误
HTTP/1.1 401 未经授权的服务器:nginx 日期:2016 年 7 月 14 日星期四 08:14:32 GMT 内容类型:application/json 内容长度:88 连接:keep-alive {"errors":[ {"message":"权限被拒绝,凭据错误","field":null,"help":null}]}
<?php
require '/sendgrid-php/vendor/autoload.php';
if(require("sendgrid-php/vendor/autoload.php"))
{echo "path found";}
sendemail('kanwararyan2@gmail.com','SEndgrid','kanwararyan1@gmail.com','HI');
function sendemail($f,$s,$t,$m){
$from = new SendGrid\Email(null, $f);
$subject = $s;
$to = new SendGrid\Email(null, $t);
$content = new SendGrid\Content("text/plain", $m);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = getenv('Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo $response->headers();
echo $response->body();
}
?>
【问题讨论】:
-
你设置了api键值的环境变量吗?
-
不需要@dlondero 吗?
-
是的,否则 getenv() 不会返回值,并且 $apiKey 不会包含您需要的内容。见下文。