Rewrote rsvp-submit.php again #5

This commit is contained in:
spetznas
2026-05-16 23:33:07 +02:00
parent f7e9968d74
commit 2a6cb3fbd0
+30 -41
View File
@@ -1,58 +1,47 @@
<?php <?php
require __DIR__ . '/vendor/autoload.php'; // PHPMailer autoload
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception; use PHPMailer\PHPMailer\Exception;
require __DIR__ . '/vendor/autoload.php'; // <- same directory as rsvp-submit.php // Load .env variables
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Load .env // Collect form data safely
$dotenvPath = __DIR__ . '/../.env'; $name = $_POST['name'] ?? 'No Name';
if (!file_exists($dotenvPath)) { $drinks = $_POST['drinks'] ?? 'None';
exit("Missing .env file"); $allergies = $_POST['allergies'] ?? 'None';
}
$env = parse_ini_file($dotenvPath, false, INI_SCANNER_RAW);
$SMTP_HOST = $env['SMTP_HOST'] ?? '';
$SMTP_PORT = $env['SMTP_PORT'] ?? 587;
$SMTP_USER = $env['SMTP_USER'] ?? '';
$SMTP_PASS = $env['SMTP_PASS'] ?? '';
$FROM_EMAIL = $env['FROM_EMAIL'] ?? '';
$FROM_NAME = $env['FROM_NAME'] ?? '';
$TO_EMAIL = $env['TO_EMAIL'] ?? '';
if ($_SERVER["REQUEST_METHOD"] !== "POST") exit("Invalid request");
// Honeypot
if (!empty($_POST['website'])) exit;
// Sanitize inputs
$first_name = htmlspecialchars($_POST['first_name'] ?? '');
$last_name = htmlspecialchars($_POST['last_name'] ?? '');
$drinks = isset($_POST['drinks']) ? implode(", ", $_POST['drinks']) : "None";
$allergies = htmlspecialchars($_POST['allergies'] ?? '');
if (!$first_name || !$last_name) exit("Missing required fields");
// Prepare email
$mail = new PHPMailer(true); $mail = new PHPMailer(true);
try { try {
// SMTP configuration
$mail->isSMTP(); $mail->isSMTP();
$mail->Host = $SMTP_HOST; $mail->Host = $_ENV['SMTP_HOST'];
$mail->SMTPAuth = true; $mail->SMTPAuth = true;
$mail->Username = $SMTP_USER; $mail->Username = $_ENV['SMTP_USER'];
$mail->Password = $SMTP_PASS; $mail->Password = $_ENV['SMTP_PASS'];
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = $SMTP_PORT; $mail->Port = (int)$_ENV['SMTP_PORT'];
$mail->setFrom($FROM_EMAIL, $FROM_NAME); // Email headers
$mail->addAddress($TO_EMAIL); $mail->setFrom($_ENV['FROM_EMAIL'], $_ENV['FROM_NAME']);
$mail->addAddress($_ENV['TO_EMAIL']);
$mail->Subject = "New Wedding RSVP from $name";
// Email subject & body // Optional: guest reply-to
$mail->Subject = "New Wedding Guest RSVP Form: $first_name $last_name"; if (!empty($_POST['email'])) {
$mail->Body = "Name: $first_name $last_name\nDrinks: $drinks\nAllergies: $allergies\n"; $mail->addReplyTo($_POST['email'], $name);
}
// Email body
$body = "Name: $name\nDrinks: $drinks\nAllergies: $allergies\n";
$mail->Body = $body;
$mail->send(); $mail->send();
echo "Thank you! Your RSVP has been sent."; echo 'RSVP submitted successfully.';
} catch (Exception $e) { } catch (Exception $e) {
error_log("RSVP mail error: {$mail->ErrorInfo}"); echo "RSVP could not be sent. Mailer Error: {$mail->ErrorInfo}";
echo "There was an error sending your RSVP. Please try again later.";
} }
?>