I'm trying to implement a contact form, however upon submission, I get a 401 from sendgrid
echo $response->statusCode(); == 401
<?php
require 'vendor/autoload.php';
$apiKey = getenv('SENDGRID_API_KEY');
$sendgrid = new \SendGrid($apiKey);
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
if (!$errName && !$errEmail && !$errMessage) {
$from = new SendGrid\Email(null, "$name");
$subject = "Hello World from the SendGrid PHP Library!";
$to = new SendGrid\Email(null, "[email protected]");
$content = new SendGrid\Content("text/plain", "$message");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo $response->headers();
echo $response->body();
}
}
?>
Any ideas?
@xorb0ss,
This means your API Key either is not valid or set properly.
First, I would suggest that you print the value of $apiKey and ensure it is set properly.
If your API Key is set properly, please reach out to https://support.sendgrid.com to check on the status of your API Key's permissions.
Thank you!
@thinkingserious
Yup, I've printed $apiKey and it's correctly set to what I have in
sendgrid.env
@thinkingserious
I've actually tried putting my API key in directly to test, instead of
getenv('SENDGRID_API_KEY')
And that gives me error 400
@thinkingserious
Problem fixed - created a new API key and it seems to work
Awesome, thanks for following up!
hey @xorb0ss ,
can you please describe what permissions should we give to a general key while creating one on SendGrid portal? which is used for purpose of sending emails from Azure webapp.
Hello @pganeshGapps,
Please reach out to our support team for help on our permission options. They can be reached at https://support.sendgrid.com
Thanks!
i have some problem, so i create new code curl ( manual ) see tutorial sendgrid.
Test and work.
So i delete this lib php. not working for me.
`$data_post = json_encode( $data_post );
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendgrid.com/v3/mail/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data_post,
CURLOPT_HTTPHEADER => array(
"authorization: Bearer API_KEY",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
//echo "cURL Error #:" . $err;
} else {
//echo $response;
$response = json_decode ( $response );
//print_r ( $response );
}`
Hi @dudenk,
I'm glad you got it working. If you ever decide to use this lib for future work, please create a new issue and I'll help work through it with you.
With Best Regards,
Elmer
hello, everyone i am having this problem as shown below.
401 Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Sat, 13 Jul 2019 15:21:04 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}]}
please change your $apiKey
from
$apiKey = getenv('SENDGRID_API_KEY')
to
$apiKey = 'SENDGRID_API_KEY';
please change your $apiKey
from
$apiKey = getenv('SENDGRID_API_KEY')
to
$apiKey = 'SENDGRID_API_KEY';
Thank you! This solves my issue.
Most helpful comment
@thinkingserious
Problem fixed - created a new API key and it seems to work