简体   繁体   中英

SendGrid gives fatal error

I've put in the code according to the documentation the only thing I did was add templating. Can anyone see what's wrong?

    <?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
    date_default_timezone_set('EST');
    # Include the API
    require 'vendor/autoload.php';
    require 'lib/SendGrid.php';
    require 'html2text.php';
    # Instantiate the client.
    $sendgrid = new SendGrid('...', '...', array("turn_off_ssl_verification" => true));
    $html = $_POST["message"];
    $text = convert_html_to_text($html);
    $date = date('l, F jS ');
    # Make the call to the client.
    $email = new SendGrid\Email();
    $email
        ->addTo('...')
       # ->addTo('...') uncomment on final script
        ->setFrom('...')
        ->setSubject('New reminder for ' . $date)
        ->setText($text)
        ->setHtml($html)
        ->addFilter("templates", "enabled", 1)
        ->addFilter("templates", "template_id", "a874a34a-a9b7-460b-a5ae-7226e68da0f1")
    ;
    print '<h1>Sent successfully</h1>';
    ?>

Result:

Fatal error: Class 'SendGrid\Email' not found in /home/[...]/public_html/[...]/sendgrid.php on line 29

It looks like the SendGrid\\Email class isn't being autoloaded properly. To fix it manually you can add require 'lib/SendGrid/Email.php'; .

If you are not using Composer, simply download and install the latest packaged release of the library as a zip.

Then require the library from package:

require("path/to/sendgrid-php/sendgrid-php.php");

Make sure you have php 5.8 OR 7.0. I had the same problem, did the following to get it to work:

  1. Updated composer.json
{
    "require": {
        "sendgrid/sendgrid": "~5.1"
    }
}
  1. php composer.phar update

  2. php composer.phar install

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM