未接收错误:class' sendgrid'找不到

发布于 2025-01-30 09:38:24 字数 1711 浏览 1 评论 0原文

尝试使用SendGrid API时,我会遇到错误。错误出现为:

Fatal error: Uncaught Error: Class 'Sendgrid' not found in signin.php:xxx Stack trace: #0 index.php(yy): require() #1 {main} thrown in signin.php on line xxx.

我使用作曲家安装了sendgrid。然后,我按照:

index.php

require __DIR__.'/../vendor/autoload.php';

注册自动加载器,然后按照下面的链接遵循指南。

signin.php

use SendGrid\Mail\From;
use SendGrid\Mail\To;
use SendGrid\Mail\Mail;

$from = new From("[email protected]", "Example User");
$to = [
    new To(
        "[email protected]",
        "Example User",
        [
            'subject' => 'Subject'
        ]
    )
];
$email = new Mail(
    $from,
    $tos
);

$email->setTemplateId("REDACTED");
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));

autoload.php文件与其他作曲家类一起使用。

到目前为止,我尝试过的步骤:

  1. 卸载SendGrid并重新安装。
  2. 手动致电sendgrid-php.php,以验证发送电子邮件工作的工作。

参考

https://github.com/sendgrid/sendgrid-php/blob/main/main/main/use_cases.md#send-an-an-email-email-email-to-a-single-recipient

作曲家。 JSON

{
    "require": {
        "sendgrid/sendgrid": "^8.0",
    }
}

I'm getting an error when trying to use the Sendgrid API. The error comes up as:

Fatal error: Uncaught Error: Class 'Sendgrid' not found in signin.php:xxx Stack trace: #0 index.php(yy): require() #1 {main} thrown in signin.php on line xxx.

I installed Sendgrid using composer. I then register the autoloader like:

index.php

require __DIR__.'/../vendor/autoload.php';

Then follow the guide like on the link below.

signin.php

use SendGrid\Mail\From;
use SendGrid\Mail\To;
use SendGrid\Mail\Mail;

$from = new From("[email protected]", "Example User");
$to = [
    new To(
        "[email protected]",
        "Example User",
        [
            'subject' => 'Subject'
        ]
    )
];
$email = new Mail(
    $from,
    $tos
);

$email->setTemplateId("REDACTED");
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));

The autoload.php file works with other composer classes.

Steps I've tried so far:

  1. Uninstall sendgrid and re-install.
  2. Called the sendgrid-php.php manually to verify sending emails work.

Reference

https://github.com/sendgrid/sendgrid-php/blob/main/USE_CASES.md#send-an-email-to-a-single-recipient

composer.json

{
    "require": {
        "sendgrid/sendgrid": "^8.0",
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌︼了一个春 2025-02-06 09:38:24

我最终将代码放入函数的内部,并且它可以毫无问题地工作。我需要autoLoad.php之后立即使用mailer.php。

如果有人遇到这个问题,这就是我所做的。显然,您需要根据您的需求进行自定义。

mailer.php

<?php

declare(strict_types=1);

use SendGrid\Mail\From;
use SendGrid\Mail\To;
use SendGrid\Mail\Mail;

function mailer() {
    $from = new From("[email protected]", "Example User");
    $to = new To(
        "[email protected]",
        "Example User",
        [
            'subject' => 'Subject'
        ]
    );
    $email = new Mail($from, $to);

    $email->setTemplateId("REDACTED");
    $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
}

我不确定为什么我的原始代码不起作用,但是该解决方案可以正常工作,所以我将其保留。

I ended up putting the code inside of a function and it worked without issue. I required mailer.php right after autoload.php.

In case someone runs into this issue, here is what I did. Obviously, you'll want to customize it to your needs.

mailer.php

<?php

declare(strict_types=1);

use SendGrid\Mail\From;
use SendGrid\Mail\To;
use SendGrid\Mail\Mail;

function mailer() {
    $from = new From("[email protected]", "Example User");
    $to = new To(
        "[email protected]",
        "Example User",
        [
            'subject' => 'Subject'
        ]
    );
    $email = new Mail($from, $to);

    $email->setTemplateId("REDACTED");
    $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
}

I'm not sure why my original code did not work, but this solution works so I'll leave it at that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文