首页 > 解决方案 > 如何使用 Sendgrid 发送电子邮件?

问题描述

如何使用 Sendgrid 发送邮件,我尝试了很多次但给出错误,错误如找不到类,我也安装了 composer 但我无法理解,请给我解决方案 错误图像:在此处输入图像描述

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';
}

标签: phpemailsendgrid-api-v3

解决方案


有同样的问题,你必须SendGrid\Email用反斜杠作为前缀\

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

有关更多详细信息,已在sendgrid-php问题 中讨论https://github.com/sendgrid/sendgrid-php/issues/341


推荐阅读