sendgrid-php: SendGrid\Content->__construct Error

Issue Summary

I have copied the simple sample of an example and I have replaced text/plain for text/html. But it gave an error of SendGrid\Content->__construct. Remember I am not using Composer. I have already echoed and exported the SendGrid API key to .env.

Steps to Reproduce

  1. I have copied the example from this README.md and replaced something:
<?php

require("sendgrid-php/sendgrid-php.php");

header("Access-Control-Allow-Origin: *");

$from = new SendGrid\Email("Atlética UNISAL", "contato@atleticaunisal.com.br");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Atlética UNISAL", "contato@atleticaunisal.com.br");
$content = new SendGrid\Content("text/html", "and easy to do anywhere, even with PHP");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = getenv('myapikey');
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);

echo $response->statusCode();
print_r($response->headers());
echo $response->body();
  1. Here is the error:
[Fri Mar 23 20:20:57 2018] PHP Fatal error:  Uncaught Error: Call to undefined function SendGrid\mb_convert_encoding() in /home/ubuntu/Github/Email-PHP/exemplo 2/sendgrid-php/lib/helpers/mail/Mail.php:729
Stack trace:
#0 /home/ubuntu/Github/Email-PHP/exemplo 2/index.php(11): SendGrid\Content->__construct('text/html', 'and easy to do ...')
#1 {main}
  thrown in /home/ubuntu/Github/Email-PHP/exemplo 2/sendgrid-php/lib/helpers/mail/Mail.php on line 729
[Fri Mar 23 20:20:57 2018] 127.0.0.1:53082 [500]: /exemplo%202 - Uncaught Error: Call to undefined function SendGrid\mb_convert_encoding() in /home/ubuntu/Github/Email-PHP/exemplo 2/sendgrid-php/lib/helpers/mail/Mail.php:729
Stack trace:
#0 /home/ubuntu/Github/Email-PHP/exemplo 2/index.php(11): SendGrid\Content->__construct('text/html', 'and easy to do ...')
#1 {main}
  thrown in /home/ubuntu/Github/Email-PHP/exemplo 2/sendgrid-php/lib/helpers/mail/Mail.php on line 729

Technical details:

  • sendgrid-php Version: 5.6.0
  • PHP Version: 7.2
  • OS: Ubuntu 16.04
  • Installed packages: curl, postfix and soap
  • Curl enabled in php.ini

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 21 (8 by maintainers)

Most upvoted comments

Please reach out to Locaweb to find out how to set environment variables on their system.

They use Windows 2012. But we’ll talk to them.

I have already exported it via terminal before, and it is still the same authorisation error.

I am going to test on my macOS.

Hello @gusbemacbe,

While your solution works, we do not advise placing your API Key in your source code. Instead we recommend you store your API Key in an environment variable like so.

Thanks!

With Best Regards,

Elmer