sendgrid-php: Class not found

Hello, i’m having trouble to setup a sendgrid form on a website. Getting error-> PHP Fatal error: Uncaught Error: Class ‘SendGrid\Mail’ not found in /app/contact.php:14.

This is my contact.php code:

<?php 
   require 'vendor/autoload.php';
   $name     = trim($_POST["name"]);
   $subject  = "testsubject";
   $mailFrom = trim($_POST["email"]);
   $message  = trim($_POST["message"]);
   $to = "email@example.com";
   $apiKey = getenv('API_KEY');
   if ($name != null && $message != null && $mailFrom != null) {
       if (!filter_var($mailFrom, FILTER_VALIDATE_EMAIL) === false) {
   		$mail = new SendGrid\Mail($mailFrom, $subject, $to, $message);
   		$sg = new \SendGrid($apiKey);
   		$response = $sg->client->mail()->send()->post($mail);
   		echo $response->statusCode();
   		print_r($response->headers());
   		echo $response->body();
   		$signal = "ok";
   		$msg    = "success message";	
   	}
   	else{
   		$signal = "bad";
   		$msg    = "error message";
   	}			
   } else {
       $signal = "bad";
       $msg    = "error message";
   }
   $data = array(
       'signal' => $signal,
       'msg' => $msg
   );
   echo json_encode($data);
?>

This is my composer.json code:

{
    "require": {
        "sendgrid/sendgrid": "^7.0"
    }
}

It also happened when i tried to use Sendgrid\Email. Is there something wrong with the code?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

You should use SendGrid\Mail\Mail