public function handleSubmit() { // validate the post if (isset($_POST['name']) && strlen($_POST['name']) > 0 && (isset($_POST['message']) && strlen($_POST['message']) > 0)) { // get and sanitise input $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING); $email = filter_var($_POST['email'], FILTER_SANITIZE_STRING); $phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING); $message = filter_var($_POST['message'], FILTER_SANITIZE_STRING); // validate the email // error_log("HERE!!!!" . filter_var($email, FILTER_VALIDATE_EMAIL)); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $this->response['result'] = false; $this->response['output'] = $this->form . "<p class=\"error\">Your email address appears to be invalid, please check and try again!</p>"; return false; } // check the HONEEEEYpot. if (strlen($_POST['leavemeblank']) > 0) { $this->response['result'] = false; $this->response['output'] = "Sorry you've supplied too much information!"; return false; } error_log("Attempting an email send"); // try and send the email $contactUs = new ContactUs(); $this->response['result'] = $contactUs->sendEmail($name, $email, $phone, $message); // response $this->response['output'] = "Thank you - we'll be in touch real soon"; } else { $this->response['result'] = false; $this->response['output'] = $this->form . "<p class=\"error\">Please check all required fields have been supplied.</p>"; } }
} $errors .= "A valid email address is required"; } return $errors; } public function sendEmail() { $output = array('wasSent' => false, 'error' => '', 'status' => ''); if ($this->isValidForm()) { $options = array("recipients" => $this->recipients, "subject" => $this->subject, "message" => $this->message, "senderEmail" => $this->fromEmail, "senderName" => $this->fromName); try { $mandrillMailer = new MandrillEmailSender($options); $output['result'] = $mandrillMailer->sendEmail(); $output['wasSent'] = true; } catch (Exception $e) { $output['error'] = 'Exception occurred. ' . $e->getMessage(); } if ($output["wasSent"]) { $output["status"] = "Thank you for contacting us. We will get back to you shortly"; } else { $output["status"] = "Sorry! Your message was not sent. Please try again later. Thank you"; } } else { $output['status'] = $this->getErrors(); } echo json_encode($output); } } $obj = new ContactUs(); $obj->sendEmail();