sendgrid failed does not contain a valid address code example

Example: sendgrid message: 'The from email does not contain a valid address.'

<?php
// If you are using Composer
require 'vendor/autoload.php';

$sg = new \SendGrid(getenv("SENDGRID_API_KEY"));

$from = new SendGrid\Email(null, "[email protected]");  //No issues here
$subject = "Hello World from the SendGrid PHP Library!";
$to = new SendGrid\Email(null, "[email protected]");  //Throws the above mentioned error
$content = new SendGrid\Content("text/plain", "Hello"); //I get the same error on replacing the content too with a variable passed to the script
$mail = new SendGrid\Mail($from, $subject, $to, $content);

$path = '/Users/thinkingserious/Workspace/sendgrid/sendgrid-php/releaseapk.apk';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);

$attachment = new SendGrid\Attachment();
$attachment->setContent(base64_encode($data));
$attachment->setType($type);
$attachment->setFilename("releaseapk.apk");
$attachment->setDisposition("attachment");
$attachment->setContentId("releaseapk");
$mail->addAttachment($attachment);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo $response->headers();
echo $response->body();

Tags:

Misc Example