contact.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // Content
  20. $name = htmlspecialchars($name);
  21. $email = htmlspecialchars($email);
  22. $phone = htmlspecialchars($phone);
  23. $message = htmlspecialchars($message);
  24. // Build
  25. $builder = new EmailBuilder;
  26. $builder->addParagraph("<b>Hi,</b>");
  27. $builder->addLeadParagraph("A new message was sent from the website!");
  28. $builder->addParagraph("<b>Name:</b><br />$name");
  29. $builder->addParagraph("<b>Email:</b><br />$email");
  30. $builder->addParagraph("<b>Contact Number:</b><br />$phone");
  31. $builder->addParagraph("<b>Message:</b><br />$message");
  32. $content = $builder->build();
  33. // Send
  34. $mail = new Email;
  35. $mail->setHtmlContent($content);
  36. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  37. $mail->addAddress("admin@" . DOMAIN);
  38. $mail->addReplyTo($email);
  39. $mail->setSubject("New message received");
  40. $mail->send();
  41. Core::addAlert("success", _("Message sent successfully. We will get back to you shortly."));
  42. HTTP::redirect("/contact");
  43. }
  44. catch(FormException $e) {
  45. $e->generateAlerts();
  46. }
  47. catch(Exception $e) {
  48. Core::addAlert("danger", $e->getMessage());
  49. }
  50. }
  51. View::includeOnce("header", compact("page_title"));
  52. View::include("contact");
  53. View::includeOnce("footer");
  54. ?>