Reverted rsvp-submit.php again #2
This commit is contained in:
+36
-24
@@ -1,38 +1,50 @@
|
||||
<?php
|
||||
require __DIR__ . '/vendor/autoload.php'; // PHPMailer
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
|
||||
exit("Invalid request");
|
||||
// Load .env
|
||||
if (!file_exists(__DIR__ . '/.env')) {
|
||||
exit('Missing .env file!');
|
||||
}
|
||||
$dotenv = parse_ini_file(__DIR__ . '/.env', false, INI_SCANNER_RAW);
|
||||
|
||||
/* Honeypot spam protection */
|
||||
if (!empty($_POST['website'])) {
|
||||
exit; // silently drop bot submission
|
||||
}
|
||||
// Form validation
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') exit('Invalid request');
|
||||
if (!empty($_POST['website'])) exit; // honeypot
|
||||
|
||||
/* Safely read form fields */
|
||||
$first_name = htmlspecialchars($_POST['first_name'] ?? '');
|
||||
$last_name = htmlspecialchars($_POST['last_name'] ?? '');
|
||||
|
||||
$drinks = isset($_POST['drinks']) ? implode(", ", $_POST['drinks']) : "None";
|
||||
$drinks = isset($_POST['drinks']) ? implode(', ', $_POST['drinks']) : 'None';
|
||||
$allergies = htmlspecialchars($_POST['allergies'] ?? '');
|
||||
if (!$first_name || !$last_name) exit('Missing required fields');
|
||||
|
||||
if (!$first_name || !$last_name) {
|
||||
exit("Missing required fields");
|
||||
}
|
||||
|
||||
/* Email setup */
|
||||
$to = "hochzeit@markinstefan.xyz";
|
||||
// Compose message
|
||||
$subject = "New Wedding RSVP from $first_name $last_name";
|
||||
$body = "Name: $first_name $last_name\n";
|
||||
$body .= "Drinks: $drinks\n";
|
||||
$body .= "Allergies: $allergies\n";
|
||||
|
||||
$message = "Name: $first_name $last_name\n";
|
||||
$message .= "Drinks: $drinks\n";
|
||||
$message .= "Allergies: $allergies\n";
|
||||
// Send email via PHPMailer
|
||||
$mail = new PHPMailer(true);
|
||||
try {
|
||||
$mail->isSMTP();
|
||||
$mail->Host = $dotenv['SMTP_HOST'];
|
||||
$mail->SMTPAuth = true;
|
||||
$mail->Username = $dotenv['SMTP_USER'];
|
||||
$mail->Password = $dotenv['SMTP_PASS'];
|
||||
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||||
$mail->Port = (int)$dotenv['SMTP_PORT'];
|
||||
|
||||
$headers = "From: no-reply@markinstefan.xyz\r\n";
|
||||
$headers .= "Reply-To: no-reply@markinstefan.xyz\r\n";
|
||||
$mail->setFrom($dotenv['FROM_EMAIL'], $dotenv['FROM_NAME']);
|
||||
$mail->addAddress($dotenv['TO_EMAIL']);
|
||||
|
||||
/* Send mail */
|
||||
mail($to, $subject, $message, $headers);
|
||||
$mail->Subject = $subject;
|
||||
$mail->Body = $body;
|
||||
|
||||
echo "Thank you! Your RSVP has been sent.";
|
||||
?>
|
||||
$mail->send();
|
||||
echo "Thank you! Your RSVP has been sent.";
|
||||
} catch (Exception $e) {
|
||||
error_log("Mailer Error: {$mail->ErrorInfo}");
|
||||
echo "Sorry, something went wrong. Please try again later.";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user