sendgrid-php: Provide solution for "Class 'SendGrid\Client' not found" error, which occurs when files are missing

I am getting the following error on form submission

Class 'SendGrid\Client' not found

I am upgrading from v2 to v3 (v2 worked fine) and I am not using composer.

Any thoughts?

require_once($this->config->paths->root . 'api/sendgrid/lib/loader.php');

	if ($this->input->post->action == 'enquiryForm') {

 		if ($_POST['products']) {

 			foreach ($_POST['products'] as $item) {
 				if (is_array($item)) {
 					$products[] = $item;
 				}
 			}
 		}
 		if ($_POST['formFields']) {
 			parse_str($_POST['formFields'], $user_details);
 		}

		ob_start();
		   require_once('../../api/template--enquiry.php');
		   $body = ob_get_contents();
		ob_end_clean();

		$email = new \SendGrid\Mail\Mail();
		$email->setFrom($user_details['email'], null);
		$tos = [
		    "rich@****.***" => null
		];
		$email->addTos($tos);
		$email->setSubject("Sending with SendGrid is Fun");
		$email->addContent(
		    "text/html", $body
		);
		$sendgrid = new \SendGrid(getenv('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'));
		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 am using PHP 7.1 and latest commit 2889b96 (although as I am not using composer I had to add the following push requests: https://github.com/sendgrid/sendgrid-php/pull/633

About this issue

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

Commits related to this issue

Most upvoted comments

if you’re like me and hate composer (don’t even get me started) you can get this to work so- just so you know … everyone here is wrong … “they” (SendGrid) simply forgot to bundle everything properly, took out \SendGrid\Email() and replaced it with \SendGrid\Mail\Mail() and so the callers are slightly different … i.e.

        $sendgrid = new SendGrid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
	$email = new \SendGrid\Mail\Mail();
	$email->setFrom("test@example.com", "Example User");
	$email->setSubject("Sending with SendGrid is Fun");
	$email->addTo("bigman@getme.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>"
	);

	//optional to attach a file<START>
	$att1 = new \SendGrid\Mail\Attachment();
	$att1->setContent( file_get_contents("/path/to/some/test_attach.txt") );
	$att1->setType("text/plain");
	$att1->setFilename("test_attach.txt");
	$att1->setDisposition("attachment");
	$email->addAttachment( $att1 );
	//optional to attach a file</END>		

	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";
	}`

but you it this way you still need “php-http-client-master” and “sendgrid-php-master” (both repos) and the /lib/loader.php in “sendgrid-php-master” needs these two lines adding at the end

               require_once '/your/path/to/php-http-client-master/lib/Client.php';
               require_once '/your/path/to/php-http-client-master/lib/Response.php';

Incidentally I also edited sendgrid-php-master\sendgrid-php.php to this < ? php require DIR . ‘/lib/loader.php’; ? >