how I can solve this problem?
Call to undefined function SendGrid\mb_convert_encoding() in /sendgrid-php/lib/helpers/mail/Mail.php on line 729
this is my code
<?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();
Short answer:
You need to install the mbstring
extension for PHP. If you're using Ubuntu, the command will probably be something like this:
sudo apt-get install php7.0-mbstring
You may need to adjust the package depending on your version of PHP. There are many resources online for installing mbstring
.
Long answer:
When PHP encounters a function call inside a namespace, it will attempt to resolve that function inside the current namespace. As you would expect, the SendGrid library you're using doesn't define it's own mb_convert_string()
so PHP will then attempt to check the global scope for a function called mb_convert_string()
.
mb_convert_encoding()
is part of the mbstring
extension. And because you don't have that extension installed, the function doesn't exist. PHP reports that a function doesn't exist in the SendGrid namespace because that's the first place it checked.
It's clear that the SendGrid developers are expecting the function to be in the global namespace. Install the extension and you should be good to go.