I'm using sengrid api to send mails and i installed curl and WAMP server. mail sttus code is 0 and body and header is empty. how to solve this issue.?
i tried php version 7.1 and 7.2 but it doesn't work
$email = new \SendGrid\Mail\Mail();
$email->setFrom("donayasara94#gmail.com", $subject);
$email->setSubject($subject);
$email->addTo($receiver, $subject);
$email->addContent("text/plain", $body);
$email->addContent(
"text/html", $body
);
$sendgrid = new \SendGrid('api key');
try {
$response = $sendgrid->send($email);
$returnArr = [
'code' => $response->statusCode(),
'body' => $response->body()
];
echo json_encode($returnArr);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
//echo 'Caught exception: ' . $e->getMessage() . "\n";
} /*end sending email*/
}
Try this,
Download cacert.pem file from here
Save it to some location say c:\cacert\cacert.pem
Edit C:\wamp64\bin\apache\apache(ver)\bin\php.ini change the line
';curl.cainfo' to curl.cainfo=c:\cacert\cacert.pem
Restart all services in wamp
Source: https://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
Related
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,
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
Im working on an application where I've created a user registration system. So when a user registers an email is sent to the account, Im using an email class to send email. Before using sendgrid I was using phpmailer and it was working perfectly fine in this format, but it doesn't works with sendgrid.
This is the code
<?php
class email {
public function sendEmail($name, $email, $url, $type, $subject){
require '../../vendor/autoload.php'; // If you're using Composer (recommended)
$email = new \SendGrid\Mail\Mail();
$email->setFrom("mymail#gmail.com", "Example User");
$email->setSubject($subject);
$email->addTo($email, $name);
if($type === "CONFIRM"){
$email->addContent(
'text/html', 'CONFIRM MESSAGE'
);
} else if($type === "FORGOT"){
$email->addContent(
'text/html', 'FORGOT MESSAGE'
);
}
$apiKey = 'api_key';
$sendgrid = new \SendGrid($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";
}
}
}
?>
So when i use this code without if and else for '$type', it works completely fine, but does not works with if else and the page loading never ends!
Some help would be appreciated!!
Im working on an application where I've created a user registration system. So when a user registers an email is sent to the account, Im using an email class to send email. Before using sendgrid I was using phpmailer and it was working perfectly fine in this format, but it doesn't works with sendgrid.
So when i use this code without if and else for '$type', it works completely fine, but does not works with if else and the page loading never ends!
Some help would be appreciated!!
This is the code
<?php
class email {
public function sendEmail($name, $email, $url, $type, $subject){
require '../../vendor/autoload.php'; // If you're using Composer (recommended)
$email = new \SendGrid\Mail\Mail();
$email->setFrom("mymail#gmail.com", "Example User");
$email->setSubject($subject);
$email->addTo($email, $name);
if($type === "CONFIRM"){
$email->addContent(
'text/html', 'CONFIRM MESSAGE'
);
} else if($type === "FORGOT"){
$email->addContent(
'text/html', 'FORGOT MESSAGE'
);
}
$apiKey = 'api_key';
$sendgrid = new \SendGrid($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";
}
}
}
?>
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);