contactSubmit.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * controllers/contactSubmit.php
  4. *
  5. * Handles the homepage contact form submission.
  6. * Validates input, sends an email via PHPMailer, redirects back with status.
  7. */
  8. require_once __DIR__ . '/../vendor/autoload.php';
  9. require_once __DIR__ . '/../config/mail.php';
  10. require_once __DIR__ . '/../lib/csrf.php';
  11. use PHPMailer\PHPMailer\PHPMailer;
  12. use PHPMailer\PHPMailer\SMTP;
  13. use PHPMailer\PHPMailer\Exception;
  14. if (session_status() === PHP_SESSION_NONE) {
  15. session_start();
  16. }
  17. // Only accept POST
  18. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  19. header('Location: /#contact');
  20. exit;
  21. }
  22. // CSRF check
  23. if (!verifyCsrfToken($_POST['csrf_token'] ?? '')) {
  24. $_SESSION['contact_error'] = 'Invalid form submission. Please try again.';
  25. header('Location: /#contact');
  26. exit;
  27. }
  28. // Collect + sanitise fields
  29. $firstName = trim(htmlspecialchars($_POST['first_name'] ?? '', ENT_QUOTES, 'UTF-8'));
  30. $lastName = trim(htmlspecialchars($_POST['last_name'] ?? '', ENT_QUOTES, 'UTF-8'));
  31. $email = trim(filter_var($_POST['email'] ?? '', FILTER_SANITIZE_EMAIL));
  32. $farmType = trim(htmlspecialchars($_POST['farm_type'] ?? '', ENT_QUOTES, 'UTF-8'));
  33. $message = trim(htmlspecialchars($_POST['message'] ?? '', ENT_QUOTES, 'UTF-8'));
  34. // Basic validation
  35. if (!$firstName || !$lastName) {
  36. $_SESSION['contact_error'] = 'Please enter your full name.';
  37. header('Location: /#contact');
  38. exit;
  39. }
  40. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  41. $_SESSION['contact_error'] = 'Please enter a valid email address.';
  42. header('Location: /#contact');
  43. exit;
  44. }
  45. if (!$message) {
  46. $_SESSION['contact_error'] = 'Please enter a message.';
  47. header('Location: /#contact');
  48. exit;
  49. }
  50. // Build email body
  51. $fullName = $firstName . ' ' . $lastName;
  52. $farmLabel = $farmType ?: 'Not specified';
  53. $bodyHtml = "
  54. <h2>New Contact Form Submission</h2>
  55. <table cellpadding='6' style='font-family:sans-serif;font-size:14px;'>
  56. <tr><td><strong>Name</strong></td><td>" . $fullName . "</td></tr>
  57. <tr><td><strong>Email</strong></td><td>" . $email . "</td></tr>
  58. <tr><td><strong>Farm Type</strong></td><td>" . $farmLabel . "</td></tr>
  59. <tr><td><strong>Message</strong></td><td>" . nl2br($message) . "</td></tr>
  60. </table>
  61. ";
  62. $bodyText = "Name: {$fullName}\nEmail: {$email}\nFarm Type: {$farmLabel}\n\nMessage:\n{$message}";
  63. // Send via PHPMailer
  64. $mail = new PHPMailer(true);
  65. try {
  66. $mail->isSMTP();
  67. $mail->Host = MAIL_HOST;
  68. $mail->SMTPAuth = true;
  69. $mail->Username = MAIL_USERNAME;
  70. $mail->Password = MAIL_PASSWORD;
  71. $mail->SMTPSecure = MAIL_ENCRYPTION === 'ssl' ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS;
  72. $mail->Port = MAIL_PORT;
  73. $mail->setFrom(MAIL_FROM, MAIL_FROM_NAME);
  74. $mail->addAddress(MAIL_TO);
  75. $mail->addReplyTo($email, $fullName);
  76. $mail->isHTML(true);
  77. $mail->Subject = 'Contact Form: ' . $fullName . ' (' . $farmLabel . ')';
  78. $mail->Body = $bodyHtml;
  79. $mail->AltBody = $bodyText;
  80. $mail->send();
  81. $_SESSION['contact_success'] = 'Thank you, ' . $firstName . '. We\'ll be in touch soon.';
  82. } catch (Exception $e) {
  83. error_log('Contact form mailer error: ' . $mail->ErrorInfo);
  84. $_SESSION['contact_error'] = 'Sorry, we couldn\'t send your message. Please try again later.';
  85. }
  86. header('Location: /#contact');
  87. exit;