Respons :
聽 | 401
-- | --
聽 | Array
聽 | (
聽 | [0] => HTTP/1.1 401 Unauthorized
聽 | [1] => Server: nginx
聽 | [2] => Date: Wed, 03 Apr 2019 20:36:08 GMT
聽 | [3] => Content-Type: application/json
聽 | [4] => Content-Length: 88
聽 | [5] => Connection: keep-alive
聽 | [6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io
聽 | [7] => Access-Control-Allow-Methods: POST
聽 | [8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
聽 | [9] => Access-Control-Max-Age: 600
聽 | [10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html
聽 | [11] =>
聽 | [12] =>
聽 | )
聽 | {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}
Code :
````
// require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
require("./../plugins/sendgrid-php/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases
$email = new \SendGrid\Mail\Mail();
$email->setFrom("[email protected]", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("[email protected]", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "and easy to do anywhere, even with PHP"
);
$sendgrid = new \SendGrid(getenv('my_key'));
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage() ."\n";
}
```
Hello @Theolodewijk,
Is my_key in your code the actual SendGrid API Key? If so, it should not be, instead it should be the name of the environment variable where your API key is stored, such as SENDGRID_API_KEY.
With Best Regards,
Elmer
Hi,
Although I followed the instructions appear on SendGrid official web https://app.sendgrid.com/guide/integrate/langs/php, I have the same problem:
401 Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Sat, 04 May 2019 17:22:44 GMT [3] => Content-Type: application/json [4] => Content-Length: 88 [5] => Connection: keep-alive [6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io [7] => Access-Control-Allow-Methods: POST [8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl [9] => Access-Control-Max-Age: 600 [10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html [11] => [12] => ) {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}
Where should I locate the sendgrid.env file?
Which text should include in it exactly?
Hi,
Although I followed the instructions appear on SendGrid official web https://app.sendgrid.com/guide/integrate/langs/php, I have the same problem:
401 Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Sat, 04 May 2019 17:22:44 GMT [3] => Content-Type: application/json [4] => Content-Length: 88 [5] => Connection: keep-alive [6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io [7] => Access-Control-Allow-Methods: POST [8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl [9] => Access-Control-Max-Age: 600 [10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html [11] => [12] => ) {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}Where should I locate the sendgrid.env file?
Which text should include in it exactly?
The 401 problem is about wrong credentials.
And you can look at the JSON response that you paste.
To debug the problem conveniently, I suggest that you can set the SenfGrid key directly instead of using environment variables at this moment on the following code snippets:
```php
.....
$sendgrid = new \SendGrid('your_sendgrid_key');
.....
Hello @Theolodewijk, @orhagashi,
Another thing you may try to rule out the API Key as the cause, if you don't want to include the API Key directly as @peter279k suggested, is to make a call using our interactive docs. If your call is successful there, it means that the issues is the API Key is not getting set properly in your code.
With Best Regards,
Elmer
Most helpful comment
The
401problem is about wrong credentials.And you can look at the JSON response that you paste.
To debug the problem conveniently, I suggest that you can set the SenfGrid key directly instead of using environment variables at this moment on the following code snippets:
```php
.....
$sendgrid = new \SendGrid('your_sendgrid_key');
.....