Hi this is my code, have set it up using a composer, and have all the required files. Slightly confused about the file orders as every tutorial I've watched on youtube has something different to what I get when i download it...
require 'vendor/autoload.php';
$API_KEY = "******************************************";
$email = new \SendGrid\Mail\Mail();
$email->setFrom("********************", "*****");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("*****************************", "*******");
$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($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";
}
Helo @obroekhuis,
Could you please walk me through the specific steps you took when you installed, including your OS?
With Best Regards,
Elmer
Same problem here. Looks like an invalid namespace of class Mail is used. Changing from "\SendGridMailMail" to "\SendGridMail" helps, but next error immediately appears:
Fatal error: Uncaught Error: Call to undefined method SendGrid\Mail::addTo()
Something is probably wrong with this example.
Note: the code is fully copied from official API setup page and only a single modification has been made - changed path to autoloader.
Found the problem. It was my bad - I was experimenting with an outdated sendgrid package version.
@obroekhuis, this could be your issue too: check sendgrid version in composer.json. By the time of this comment it should not be lower that 7.
Hello @obroekhuis,
Are you still experiencing this issue? Just checking back in.
With Best Regards,
Elmer
I don't have this problem and the codes that @obroekhuis provides are worked successfully.
I'm going to go ahead and close this. Please feel free to reopen if you still have an issue @obroekhuis. Thanks!
Most helpful comment
I don't have this problem and the codes that @obroekhuis provides are worked successfully.