$errors .= "Please enter a valid email \n"; } if (empty($message) || !preg_match("/^[a-zA-Z ]*\$/", $message)) { $errors .= "Please enter a valid message \n"; } if (!empty($errors)) { $json = array('error' => $errors); echo JSONResponse::send($json); return; } $sendgrid = new SendGrid('API_KEY_HERE'); $email = new SendGrid\Email(); $email->addTo($app->config->get('smtp')->to)->setFrom('*****@*****.**')->setSubject('Inquiry from ' . $name)->setText($message); try { $sendgrid->send($email); } catch (\SendGrid\Exception $e) { $json = array('error' => ''); foreach ($e->getErrors() as $err) { $json['error'] .= $err . "\n"; } echo JSONResponse::send($json); } $json = array('message' => 'success'); echo JSONResponse::send($json); } catch (Exception $e) { $json = array('error' => $e->getMessage()); echo JSONResponse::send($json); } }); }); });