使用sendgrid发送邮件时凭据错误 [英] Wrong credentials in sending mail using sendgrid
问题描述
这是使用sendgrid发送电子邮件的代码,我具有正确的api密钥,但浏览器仍然显示错误
HTTP/1.1 401未经授权的服务器:nginx日期:2016年7月14日,星期四08:14:32 GMT内容类型:application/json内容长度:88连接:keep-alive {错误":[ {"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密钥设置环境变量:
$apiKey = getenv(...);
请在此处检查文档,因为您似乎正在使用示例代码. /p>
只需进行测试,您就可以使用
$apiKey = 'add here your api key';
替换getenv的用法.它应该工作.然后,您可以在配置文件中设置api密钥,也可以将其设置为env变量(取决于您的应用程序),以便不将其硬编码到脚本中.
This is the code for sending email using sendgrid i have correct api key still the browser displays error as
HTTP/1.1 401 Unauthorized Server: nginx Date: Thu, 14 Jul 2016 08:14:32 GMT Content-Type: application/json Content-Length: 88 Connection: keep-alive {"errors":[{"message":"Permission denied, wrong credentials","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();
}
?>
Looks like you didn't set the environment variable with the api key that you're trying to use with:
$apiKey = getenv(...);
Please check documentation here as it looks like you're using the example code.
Just for a test you can use:
$apiKey = 'add here your api key';
replacing the usage of getenv. It should work. Then you can set the api key in a config file or as env variable (depending on your application) in order to not hardcode it into the script.
这篇关于使用sendgrid发送邮件时凭据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!