未接收错误:class' sendgrid'找不到
尝试使用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文件与其他作曲家类一起使用。
到目前为止,我尝试过的步骤:
- 卸载SendGrid并重新安装。
- 手动致电sendgrid-php.php,以验证发送电子邮件工作的工作。
参考
作曲家。 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:
- Uninstall sendgrid and re-install.
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终将代码放入函数的内部,并且它可以毫无问题地工作。我需要autoLoad.php之后立即使用mailer.php。
如果有人遇到这个问题,这就是我所做的。显然,您需要根据您的需求进行自定义。
mailer.php
我不确定为什么我的原始代码不起作用,但是该解决方案可以正常工作,所以我将其保留。
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
I'm not sure why my original code did not work, but this solution works so I'll leave it at that.