sendgrid unauthorized. Wrong credentials - php - php

I'm trying to implement sendgrid to my project and I have a basic function running the following:
require '../vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test#example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("tiernoyt#gmail.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid($_ENV['SENDGRID_API_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";
}
}
I get the error "permission denied: wrong credentials". I have the environmental variable set, I tried to put the value of the key with quotes and no quotes. It is also in my sendgrid.env file (which is gitignored) as the following line
export SENDGRID_API_KEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
If I change the code to $apiKey = 'xxxxxx'; $sendgrid = new \SendGrid($api); then it works. So it is obviously an error fetching the variable but I'm not sure as to why

Related

Getting "access forbidden" error with SendGrid marketingdb php

So, I'm able to send emails with Sendgrid just fine. My API key is set to "full access."
But whenever I try to interact with my contactdb (adding a contact, getting a list...anything), I get an "access forbidden" error.
require_once 'config.php';
require '../sendgrid-php/sendgrid-php.php';
$sendgrid = new \SendGrid(SENDGRID_API_KEY);
try {
$response = $sendgrid->client->contactdb()->lists()->get();
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Any idea why this is happening or more importantly, what the fix is?
I just spent an hour figuring this out. They haven't updated the docs. That's all. They have a new Marketing API that does not reflect the documentation.
See more here
Meanwhile, you can try again with marketing() instead of contactdb()
$response = $sendgrid->client->marketing()->lists()->get();

Call to undefined method Sendgrid::send() in Codeigniter

I am using sendgrid api v3. Everything works fine on my localserver with core php and now i move my code on server with codeigniter framework. It shows me following error.
Fatal error: Call to undefined method Sendgrid::send() in /var/www/html/adihex/application/helpers/sendgrid_helper.php on line 19
Here is my code
<?php
require_once FCPATH . 'sendgrid-php/sendgrid-php.php';
function send_mail()
{
$template_id = '********************************';
$api_key = '*************************************';
// echo '<pre>';
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test#example.com", "Example User");
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addTo("singhrobin1238014#gmail.com", "Example User");
$email->setTemplateId($template_id);
$email->addDynamicTemplateDatas([
'heading' => 'Welcome to Adihex',
]);
$sendgrid = new \SendGrid($api_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";
}
}
Please guide me where i am wrong.
Any solution appreciated!
Although its late but it might help someone ,
You might be having Multiple Classes with same name , SendGrid In this case the script is searching for send function in someother Class with same name,

Cant' reach https://api.sendgrid.com/v3/mail/send endpoint (?)

I've got an issue with sending e-mails with PHP using Sendgrid - I try to send the simpliest e-mail, I get 202 status, but no e-mails are delivered.
I've already contacted with SendGrid support team to confirm that my API KEY is valid and has correct permissions.
I use:
- PHP 7.3.0
- Composer 1.8.3
This is my composer.json:
{
"require": {
"sendgrid/sendgrid": "^7.3",
"sendgrid/php-http-client": "~3.9.6"
}
}
I've produced the simplest test endpoint to send an e-mail. It's in here -> http://konfiguratorszkolen.pl/send-email/test.php
The code in this PHP looks this:
<?php
require 'vendor/autoload.php';
$email = new \SendGrid\Mail\Mail();
$email->setFrom("testemail1#gmail.com", "Example User");
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addTo("testemail2#gmail.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid("MyValidAPIKey");
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";
}
?>
Sendgrid support team says that I don't send POST request to the https://api.sendgrid.com/v3/mail/send endpoint. But shouldn't $response = $sendgrid->send($email); do that job?
What might be the issue? How can I reach the endpoint?
I want to add, that this kind of code worked perfectly in May. It stopped a few weeks ago. What am I doing wrong?

How to properly send email with sendgrid via php

I have the code below which is from sendgrid for sending an email with my API key but I cannot get it to work as it displays
error Fatal error: Uncaught Error: Call to a member function send() on
string in ...
I have install composer as required
It seems, However, $sendgrid->send($email) doesn't actually return anything, so $mail is an empty variable.
Any idea on how to resolve this.
Api link source.
<?php
// https://sendgrid.com/docs/API_Reference/index.html
// 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.php");
// If not using Composer, uncomment the above line
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test#example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test#example.com", "Example User");
$email->addContent(
"text/plain", "and easy to do anywhere, even with PHP"
);
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
//$sendgrid = new \SendGrid(getenv('my api goes here'));
$sendgrid = 'my api goes here';
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";
}
You are trying to call a function on a string.
$sendgrid = 'my api goes here';
And later:
$response = $sendgrid->send($email);
What I think you meant:
$response = $email->send($email);

SendGrid sending to multiple recipients using api v3 & PHP library in Wordpress function

I am using SendGrid v3 api with the php library to try and send to multiple recipients as part of a WordPress function. I can successfully send emails using the SendGrid sample code and hard coding the multiple recipients. However, when I query the database to try build the list of to: email addresses it always fails with a 400 error. Here is the SendGrid code I am working with. It works fine with live data and hard coded. However, I can't seem to properly build the $tos array from my database query. I have read the documentation and every tutorial I can find. I also contacted Sendgrid support.
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test#example.com", "Example User");
$tos = [
"test+test1#example.com" => "Example User1",
"test+test2#example.com" => "Example User2",
"test+test3#example.com" => "Example User3"
];
$email->addTos($tos);
$email->setSubject("Sending with SendGrid is Fun");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_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";
}
Here is my WordPress query: $sends = $wpdb->get_results( "SELECT * FROM test" );
How do I properly code the $tos variable from my database query so that $email->addTos($tos) does not error out?
Thanks.
According to this page, the addTos function is defunct and no longer supported in SendGrid's API: "The addTos() method was moved to addTo() and does not currently support arrays being passed in".
Therefore:
Using your test database:
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test#example.com", "Example User");
$userResults = $wpdb->get_results( "SELECT `email` FROM `test`", ARRAY_A ); //Select as Associative Array
foreach($userResults as $userKey => $userValue){
$userEmail = $userValue['email']);
if ( is_email( $userEmail ) ) { //Validate properly formatted email structure
$email->addTo($userValue['email']); // If you hade a name column you could use: $email->addTo($userValue['email'], $userValue['name']);
}
}
$email->setSubject("Sending with SendGrid is Fun");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_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";
}
Using wp_users database:
$userResults = $wpdb->get_results( "SELECT `user_email`, `user_nicename` FROM `wp_users`", ARRAY_A );
foreach($userResults as $userKey => $userValue){
$email->addTo($userValue['user_email'], $userValue['user_nicename']);
}
I think #Jamie_D have some misunderstanding about addTos method because as per now in 19 June 2019 i am using addTos method and it is working perfectly ok,
Edit: and now sendgrid's employee has also confirmed it see this link:https://github.com/sendgrid/sendgrid-php/issues/127#issuecomment-508330993
when First time i have used it i got errors also so i have debugged and printed some variable in sendgrid library and i have got the point, may be you are also missing that point so you are getting error.
in $tos array you have to provide associative array in which email should be the keys and user's name should be the values, see below example:
Syntax:
$tos = [
//user emails => user names
"user1#example.com" => "Example User1",
"user2#example.com" => "Example User2",
"user3#example.com" => "Example User3"
];
$email->addTos($tos);
Example using wp_users table:
$tos = array();
$userResults = $wpdb->get_results( "SELECT `user_email`, `user_nicename` FROM `wp_users`", ARRAY_A );
foreach($userResults as $user){
$tos[$user['user_email']]= $user['user_nicename'];
}
$email->addTos($tos);
try it, it will definitely works and if doesn't then tell me in comments, thanks.

Categories