Here is my code and issue Severity: error --> Exception: "$email" must be an instance of SendGrid\Mail\From or a valid email address
require 'vendor/autoload.php';
$email = new \SendGrid\Mail\Mail();
$email->setFrom($this->ecomhut->getConfig('salesEmail'));
$email->setSubject($subject);
$email->addTo($data['email']);
//$email->addBcc($this->ecomhut->getConfig('supportEmail'));
$email->addBcc($this->ecomhut->getConfig('ForwarderEmail'));
$email->setReplyTo($this->ecomhut->getConfig('supportEmail'),NULL);
$email->addContent(
"text/html", $template
);
$sendgrid = new \SendGrid($this->SENDGRID_API_KEY);
try {
$response = $sendgrid->send($email);
} catch (Exception $e) {
log_message('error', "Email Not Send form admin agianst ".$data['email']." \n" .print_r($response));
return false;
}
return true;
Twilio SendGrid developer evangelist here.
The SendGrid PHP library validates that email addresses are being set as the From and To addresses. In this case, the complaint was that the From email address wasn't a valid email address.
$email->setFrom($this->ecomhut->getConfig('salesEmail'));
When storing email addresses, in config or in a database, you need to ensure that the string is a valid email address, including removing leading or trailing white space. To help with this, you might consider using the trim
function to remove that white space
$email->setFrom(trim($this->ecomhut->getConfig('salesEmail')));
This could stop future mistakes from affecting the emails getting sent.