'Uncaught Error: Class 'Sendgrid' not found in

I'm getting an error when trying to use the Sendgrid API. The error comes up as:

Fatal error: Uncaught Error: Class 'Sendgrid' not found in signin.php:xxx Stack trace: #0 index.php(yy): require() #1 {main} thrown in signin.php on line xxx.

I installed Sendgrid using composer. I then register the autoloader like:

index.php

require __DIR__.'/../vendor/autoload.php';

Then follow the guide like on the link below.

signin.php

use SendGrid\Mail\From;
use SendGrid\Mail\To;
use SendGrid\Mail\Mail;

$from = new From("[email protected]", "Example User");
$to = [
    new To(
        "[email protected]",
        "Example User",
        [
            'subject' => 'Subject'
        ]
    )
];
$email = new Mail(
    $from,
    $tos
);

$email->setTemplateId("REDACTED");
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));

The autoload.php file works with other composer classes.

Steps I've tried so far:

  1. Uninstall sendgrid and re-install.
  2. Called the sendgrid-php.php manually to verify sending emails work.

Reference

https://github.com/sendgrid/sendgrid-php/blob/main/USE_CASES.md#send-an-email-to-a-single-recipient

composer.json

{
    "require": {
        "sendgrid/sendgrid": "^8.0",
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source