微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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密钥,以便不将其硬编码到脚本中.

原文地址:https://www.jb51.cc/nginx/434411.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐