php – 使用sendgrid发送邮件时出错的凭据

这是使用sendgrid发送电子邮件代码我有正确的api密钥仍然浏览器显示错误

HTTP / 1.1 401未经授权的服务器:Nginx日期:星期四,2016年7月14日08:14:32 GMT内容类型:application / json内容长度:88连接:keep-alive {“errors”:[{“message”:“权限被拒绝,凭据错误“,”字段“: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密钥设置环境变量:

$apiKey = getenv(...);

请在此处查看documentation,因为您看起来正在使用示例代码.

只是为了测试你可以使用:

$apiKey = 'add here your api key';

取代getenv的用法.它应该工作.然后,您可以在配置文件或env变量(取决于您的应用程序)中设置api密钥,以便不将其硬编码到脚本中.

相关文章

Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一...
本地项目配置 1 复制 luffy/settings/dev.py为prop.py 修改l...
nginx不仅可以隐藏版本信息,还支持自定义web服务器信息 先看...
一 、此次漏洞分析 1 nginx HTTP/2漏洞 [nginx-announce] ng...
###进入nginx 目录cd /usr/local/nginx###递归显示 2 级目录...
在cmd命令窗口输入下面命令进行查看 tasklist /fi "ima...