contact.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. $page_title = "Contact";
  3. Core::addBreadcrumb("/contact", $page_title);
  4. $submit = http::value("submit");
  5. if($submit) {
  6. try {
  7. Core::validateCSRFToken($submit);
  8. $name = http::value("name", "trim");
  9. $email = http::value("email", "email");
  10. $phone = http::value("phone", "trim");
  11. $message = http::value("message", "trim");
  12. $form_exception = new FormException;
  13. if(!Form::checkRecaptcha()) $form_exception->addError("reCAPTCHA check failed.");
  14. if(empty($name)) $form_exception->addError("Name is required.");
  15. if(empty($email)) $form_exception->addError("Email address is invalid.");
  16. if(empty($phone)) $form_exception->addError("Contact number is required.");
  17. if(empty($message)) $form_exception->addError("Message is required.");
  18. if($form_exception->hasErrors()) throw $form_exception;
  19. $mail = new Email();
  20. $mail->loadHtmlTemplate("contact");
  21. $mail->setValues([
  22. "name" => htmlspecialchars($name),
  23. "email" => htmlspecialchars($email),
  24. "phone" => htmlspecialchars($phone),
  25. "message" => htmlspecialchars($message),
  26. ]);
  27. $mail->setFrom(EMAIL_FROM, EMAIL_FROM_NAME);
  28. $mail->addAddress("admin@" . DOMAIN);
  29. $mail->addReplyTo($email);
  30. $mail->setSubject("New message received");
  31. $mail->send();
  32. Core::addAlert("success", "Message sent successfully. We will get back to you shortly.");
  33. HTTP::redirect("/contact");
  34. }
  35. catch(FormException $e) {
  36. $e->generateAlerts();
  37. }
  38. catch(Exception $e) {
  39. Core::addAlert("danger", $e->getMessage());
  40. }
  41. }
  42. View::includeOnce("header", compact("page_title"));
  43. View::include("contact");
  44. View::includeOnce("footer");
  45. ?>