A summary of the issue and the environment in which it occurs. If suitable, include the steps required to reproduce the bug. Please feel free to include screenshots, screencasts, code examples.
Any other information you want to share that is relevant to the issue being reported. Especially, why do you consider this to be a bug? What do you expect to happen instead?
i am getting this error --
HTTP/1.1 401 UNAUTHORIZED [1] => Server: nginx [2] => Date: Thu, 12 Jul 2018 12:22:31 GMT [3] => Content-Type: application/json [4] => Content-Length: 62 [5] => Connection: keep-alive [6] => Access-Control-Allow-Methods: HEAD, GET, OPTIONS, DELETE [7] => Access-Control-Max-Age: 21600 [8] => Access-Control-Expose-Headers: Link, Location [9] => Access-Control-Allow-Origin: * [10] => Access-Control-Allow-Headers: AUTHORIZATION, Content-Type, On-behalf-of, x-sg-elas-acl, X-Recaptcha [11] => Content-Security-Policy: default-src https://api.sendgrid.com; frame-src 'none'; object-src 'none' [12] => X-Content-Type-Options: nosniff [13] => Strict-Transport-Security: max-age=31536000 [14] => [15] => ) {"errors":[{"field":null,"message":"authorization required"}]}
here is my code-
if($_SERVER["REQUEST_METHOD"] == "POST"){
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("./sendgrid-php.php");
// If not using Composer, uncomment the above line
$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"
);
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
try {
$response = $sg->client->suppression()->bounces()->get();
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}?>
No offense but have you even read the error you got?
It clearly states that authorization is required, your Sendgrid API key is either invalid or has no value.
@martijnmelchers yup i had resolved that issue but i am getting this kind of error now
200 Array ( [0] => HTTP/1.1 200 OK [1] => Server: nginx [2] => Date: Thu, 12 Jul 2018 14:00:19 GMT [3] => Content-Type: application/json [4] => Transfer-Encoding: chunked [5] => Connection: keep-alive [6] => Access-Control-Allow-Methods: HEAD, GET, OPTIONS, DELETE [7] => Access-Control-Max-Age: 21600 [8] => Access-Control-Expose-Headers: Link, Location [9] => Access-Control-Allow-Origin: * [10] => Access-Control-Allow-Headers: AUTHORIZATION, Content-Type, On-behalf-of, x-sg-elas-acl, X-Recaptcha [11] => Content-Security-Policy: default-src https://api.sendgrid.com; frame-src 'none'; object-src 'none' [12] => X-Content-Type-Options: nosniff [13] => Strict-Transport-Security: max-age=31536000 [14] => X-Ratelimit-Remaining: 599 [15] => X-Ratelimit-Limit: 600 [16] => X-Ratelimit-Reset: 1531404060 [17] => Link: ; rel="next"; title="1", ; rel="prev"; title="1", ; rel="last"; title="1", ; rel="first"; title="1" [18] => Powered-By: Mako [19] => [20] => ) []
dnt knw whether it is an error or just a message but still i am not able to send a mail through sendgrid
That is because your code:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("./sendgrid-php.php");
// If not using Composer, uncomment the above line
$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"
);
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
try {
$response = $sg->client->suppression()->bounces()->get(); // <-- You are getting bounces, you are not sending an email.
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
?>
You are getting the bounces, you are not sending an email. (check the first line after try {
Thanks for helping out @martijnmelchers!
@abhishekpwebdev,
Please let me know if you are still having issues.
thanks for helping @martijnmelchers
I am now able to send a test email but when i try to customize the code according to my requirement i am getting the error like this
Fatal error: Class 'SendGrid\Email' not found in /opt/lampp/htdocs/sendgrid/index.php
here is my code
require 'vendor/autoload.php';
$API_KEY = '';
$FROM_EMAIL = '[email protected]';
$TO_EMAIL = 'email';
$subject = "test";
$from = new SendGrid\Email(null, $FROM_EMAIL);
$to = new SendGrid\Email(null, $TO_EMAIL);
$htmlContent = '';
$content = new SendGrid\Content("text/html",$htmlContent);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$sg = new \SendGrid($API_KEY);
$response = $sg->client->mail()->send()->post($mail);
if ($response->statusCode() == 202) {
echo 'done';
} else {
echo 'false';
}
?>
i am using composer for installing sendgrid dependencies. i have the autoload.php in my vendor folder .-
You might have to include the following statement at the top:
use Sendgrid\Mail\Mail;
Also Sendgrid\Email is not an existing class, its Sendgrid\Mail
So either it is:
use Sendgrid\Mail\Mail;
$mail = new Mail();
or:
$mail = new \Sendgrid\Mail\Mail();
thanks, @martijnmelchers for responding
I tried to integrate with the third solution mentioned by you
$mail = new Sendgrid\Mail\Mail();
but I am getting the error.
Fatal error: Class 'Sendgrid\Mail\Mail' not found
code-
$subject = "test";
$from = new Sendgrid\Mail\Mail(null, $FROM_EMAIL);
$to =new Sendgrid\Mail\Mail(null, $TO_EMAIL);
$htmlContent = '';
$content = new SendGrid\Content("text/html",$htmlContent);
$mail = new Mail($from, $subject, $to, $content);
$sg = new \SendGrid($API_KEY);
$response = $sg->client->mail()->send()->post($mail);
if ($response->statusCode() == 202) {
// Successfully sent
echo 'done';
} else {
echo 'false';
}
This code should work:
Only for the Sendgrid/Sendgrid library version 7.0.0 or up!
<?php
require_once 'vendor/autoload.php'; // <-- replace this with your location of the loader
use SendGrid\Mail\Content;
use SendGrid\Mail\From;
use SendGrid\Mail\Mail;
use SendGrid\Mail\To;
$subject = "Welcome to Sengrid!";
$fromEmail = "[email protected]";
$toEmail = "[email protected]";
$htmlContent = "This is a <b>HTML</b> test!";
$from = new From($fromEmail);
$to = new To($toEmail);
$content = new Content("text/html", $htmlContent);
$mail = new Mail($from, $to, $subject);
$mail->addContent($content);
$sg = new SendGrid(getenv('SENDGRID_API_KEY')); // <-- If you dont have an environment variable replace this with your key
$response = $sg->client->mail()->send()->post($mail);
if ($response->statusCode() == 202) {
echo 'Mail sent!';
} else {
echo 'Mail not sent!';
}
Also: you should re-open this issue (should be right next to the comment button)
Hello @abhishekpwebdev,
Could you please let me know how you are installing the SDK? Thanks!
With Best Regards,
Elmer
Hi, I'm not using composer but I have the same error. The sendgrid-php.php file is inside the sendgrid-php folder. My code is the following:
`
// using SendGrid's PHP Library
// https://github.com/sendgrid/sendgrid-php
// require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
require("sendgrid-php/sendgrid-php.php");
// If not using Composer, uncomment the above line
$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('apikey'));
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";
}
?>`
Thanks
Hello @lucacrippa88,
That error happens when your API Key does not have the right permissions. This also happens when your 'apikey' environment is not set properly. I suggest you check the value of getenv('apikey'). If it is your API Key, then I'd check with support to see if there is an issue with the permissions on your API Key.
With Best Regards,
Elmer