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.
<?php
require("sendgrid-php/sendgrid-php.php");
header("Access-Control-Allow-Origin: *");
$from = new SendGrid\Email("Atl茅tica UNISAL", "[email protected]");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Atl茅tica UNISAL", "[email protected]");
$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();
[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
php.iniHello @gusbemacbe,
I believe this is the solution. Please try it out and let me know how it goes. Thanks!
With Best Regards,
Elmer
He used Composer and I do not. I do not want to use Composer and copy the whole folder vendor to a simple website. If you say I have to use Composer, if I copy only the folder shark/simple_html_dom to the website folder, will it work?
What a waste of time! The package simple_html_dom is dead and the user shark is dead too.
I don't think you need Composer, just this:
Looks like you need to enable mbstring. See here for more detail.
As I have said, this package simple_html_dom is dead and doesn't exist, and I do not use Heroku.
In Ubuntu, I think this is what you need. Please take a look and let me know how it works out. Thanks!
I installed it, but it gave another error:
401Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Sat, 24 Mar 2018 13:39:08 GMT [3] => Content-Type: application/json [4] => Content-Length: 88 [5] => Connection: keep-alive [6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io [7] => Access-Control-Allow-Methods: POST [8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl [9] => Access-Control-Max-Age: 600 [10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html [11] => [12] => ) {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}
Hello @gusbemacbe,
A 401 error relates to authentication. Please verify that your API Key is being set correctly.
Thanks!
With Best Regards,
Elmer
I copied the correct API key and sourced the env files. But it is still invalid.
I am confused.
Do I keep $apiKey = getenv('SENDGRID_API_KEY'); or replace for $apiKey = getenv('SG.ob4w0...'); or $apiKey = getenv('ob4w0...);
I found the solution from the tutorial, I had to replace $apiKey = getenv('SG.ob4w0...'); for $apiKey = 'SG.ob4w0...'; and it worked.
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
The problem is that the PHP main file doesn't find the file .env. I placed two files .env and sendgrid.env in the home folder of a site and also inside the folder sendrgid-php and I sourced. Finally nothing worked. I did it during three days.
What OS are you using?
What hosting provider are you deploying to?
Generally, you set the environment variable at the OS level, outside of the scope of your code.

Please reach out to Locaweb to find out how to set environment variables on their system.
For local development, what happens when you execute the following from the command line? echo $SENDGRID_API_KEY You should see your SendGrid API Key returned. If so, then using getenv('SENDGRID_API_KEY'); from your PHP app should work. If you did not see your SendGrid API Key returned, you need to do export SENDGRID_API_KEY='SG.ob4w0...' from the command line.
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.
I have tested, but it gave an error: Notice: Undefined variable: apiKey in /Users/gusbemacbe/Sites/Atle虂tica UNISAL/3陋 versa虄o D/contacto-sendgrid.php on line 640
I placed sendgrid.env and .env in the home/user folder and also in the site folder and it gave the same error.
Ah, I fiex because I forgot of putting $apiKey, but it also gave the same error: {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}
It is probable that it be the server of Locaweb.
Although PHPMailer and SendGrid, I understood what happened.
Without getenv('SENDGRID_API_KEY'); of SendGrid and SMTP of PHPMailer, it works, the e-mails can be sent, but are classified as spams. As I upgraded the version of PHPMailer, I had to put SMTP. With getenv('SENDGRID_API_KEY'); of SendGrid and SMTP of PHPMailer, the e-mails are classified as insecure and can't be sent due to Loacweb's server because of lack of authentication, authorisation and credentials and of ancient server. I have already contacted Locaweb.
Thanks for keeping us updated. Please let me know what Locaweb tells you.