We are sending emails via sendgrid/sendgrid php package. While this worked well until last week on Laravel 6, it seems like Sengrid does not receive receipients added via $email->addTo("[email protected]", "Foo Bar");
anymore. We upgraded vom laravel6 to laravel8 last week, however I doubt that this is really related.
$email = new Mail();
$email->setTemplateId('myTemplateId');
$email->setFrom('[email protected]', 'Contact Form');
$email->addTo('[email protected]', "Foo Bar");
$email->addBcc('[email protected]', 'Blabla');
$email->setReplyTo($request->email, $request->name);
$email->setSubject("ZMy Subject");
$email->addDynamicTemplateDatas([
'name' => $request->name,
'email' => $request->email,
'subject' => $request->subject,
'message' => $request->message,
'company' => $company->company_name,
'subscribe' => $request->subscribe ? 'ja' : 'nein'
]);
//dd($email);
$sendgrid = new SendGrid(getenv('SENDGRID_API_KEY'));
try {
$response = $sendgrid->send($email);
if ($response->statusCode() != "202") {
throw new \Exception('Email exception: ' . $response->body());
}
$emailSent = true;
} catch (Throwable $e) {
throw new \Exception('Email exception: ' . $e->getMessage());
$emailSent = false;
}
The content of the $email
variable does look like this:
The API call does return status 202, so everything looks fine. However the activity feed in the sendgrid bakcend does not contain any receipient - and the mail is not sent.
Any ideas why this happens? Or does anyone experience the same?
Version: "sendgrid/sendgrid": "7.8.3"
"laravel/framework": "^8.0"
I found out that the reason for aboves behaviour was a corrupted handlebars code in the template. The php-code above itself was correct.
Personally I find it confusing that the API accepts requests to send emails with corrupt templates and returns 202 and does actually try to send it to an empty recipient. Its also expensive for Sendgrid. Instead of adding the email with the corrupted template to the send queue where it will definitely fail, I would suggest that Sendgrid validates the templates and blocks send attempts for emails with corrupted emails, returning an error code.