tho*_*hom 2 php email sendgrid-api-v3
我该如何解决这个问题?
调用 /sendgrid-php/lib/helpers/mail/Mail.php 第 729 行中未定义的函数 SendGrid\mb_convert_encoding()
这是我的代码
<?php
require("./sendgrid-php/sendgrid-php.php");
$from = new SendGrid\Email(null, "example@example.com");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email(null, "example@example.com");
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
// Send message as html
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = getenv('my key');
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
print_r($response->headers());
echo $response->body();
Run Code Online (Sandbox Code Playgroud)
简短回答:
您需要安装mbstringPHP 扩展。如果您使用的是 Ubuntu,命令可能如下所示:
sudo apt-get install php7.0-mbstring
Run Code Online (Sandbox Code Playgroud)
您可能需要根据您的 PHP 版本调整软件包。网上有很多安装资源mbstring。
长答案:
当 PHP 在命名空间内遇到函数调用时,它将尝试解析当前命名空间内的该函数。正如您所期望的,您使用的 SendGrid 库没有定义它自己的库,mb_convert_string()因此 PHP 将尝试检查名为 的函数的全局范围mb_convert_string()。
mb_convert_encoding()是扩展的一部分mbstring。因为您没有安装该扩展,所以该功能不存在。PHP 报告 SendGrid 命名空间中不存在函数,因为这是它检查的第一个位置。
很明显,SendGrid 开发人员希望该函数位于全局命名空间中。安装扩展程序就可以开始了。