简体   繁体   中英

How to Send email using Sendgrid?

How to send mail using Sendgrid, i had tried many time but give error, Error like class not found,i had also install composer but i cant understand, please give me solution ERROR IMAGE: 在此输入图像描述

require './vendor/autoload.php';
include_once('credentials.php');
$FROM_EMAIL = 'from@gmail.com';
$TO_EMAIL = 'to@gmail.com';
$subject = "test";
$from = new SendGrid\Email(null, $FROM_EMAIL);
$to = new SendGrid\Email(null, $TO_EMAIL);
$htmlContent = '<!DOCTYPE html><html><head><title></title></head><body><p>hi this is test</p></body></html>';
$content = new SendGrid\Content("text/html",$htmlContent);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$sg = new \SendGrid($API_KEY);
$response = $sg->client->mail()->send()->post($mail);
if ($response->statusCode() == 202) 
{
    echo 'done';
} 
else 
{
    echo 'false';
}

Had the same issue, you have to prefix SendGrid\\Email with a backslash \\ :

$from = new \SendGrid\Email(null, $FROM_EMAIL);
$to = new \SendGrid\Email(null, $TO_EMAIL);

For more details, it has been discuss in sendgrid-php issues https://github.com/sendgrid/sendgrid-php/issues/341

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