Rewrote rsvp-submit.php again #5
This commit is contained in:
+30
-41
@@ -1,58 +1,47 @@
|
||||
<?php
|
||||
require __DIR__ . '/vendor/autoload.php'; // PHPMailer autoload
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
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
|
||||
$dotenvPath = __DIR__ . '/../.env';
|
||||
if (!file_exists($dotenvPath)) {
|
||||
exit("Missing .env file");
|
||||
}
|
||||
$env = parse_ini_file($dotenvPath, false, INI_SCANNER_RAW);
|
||||
// Collect form data safely
|
||||
$name = $_POST['name'] ?? 'No Name';
|
||||
$drinks = $_POST['drinks'] ?? 'None';
|
||||
$allergies = $_POST['allergies'] ?? 'None';
|
||||
|
||||
$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);
|
||||
|
||||
try {
|
||||
// SMTP configuration
|
||||
$mail->isSMTP();
|
||||
$mail->Host = $SMTP_HOST;
|
||||
$mail->Host = $_ENV['SMTP_HOST'];
|
||||
$mail->SMTPAuth = true;
|
||||
$mail->Username = $SMTP_USER;
|
||||
$mail->Password = $SMTP_PASS;
|
||||
$mail->Username = $_ENV['SMTP_USER'];
|
||||
$mail->Password = $_ENV['SMTP_PASS'];
|
||||
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||||
$mail->Port = $SMTP_PORT;
|
||||
$mail->Port = (int)$_ENV['SMTP_PORT'];
|
||||
|
||||
$mail->setFrom($FROM_EMAIL, $FROM_NAME);
|
||||
$mail->addAddress($TO_EMAIL);
|
||||
// Email headers
|
||||
$mail->setFrom($_ENV['FROM_EMAIL'], $_ENV['FROM_NAME']);
|
||||
$mail->addAddress($_ENV['TO_EMAIL']);
|
||||
$mail->Subject = "New Wedding RSVP from $name";
|
||||
|
||||
// Email subject & body
|
||||
$mail->Subject = "New Wedding Guest RSVP Form: $first_name $last_name";
|
||||
$mail->Body = "Name: $first_name $last_name\nDrinks: $drinks\nAllergies: $allergies\n";
|
||||
// Optional: guest reply-to
|
||||
if (!empty($_POST['email'])) {
|
||||
$mail->addReplyTo($_POST['email'], $name);
|
||||
}
|
||||
|
||||
// Email body
|
||||
$body = "Name: $name\nDrinks: $drinks\nAllergies: $allergies\n";
|
||||
|
||||
$mail->Body = $body;
|
||||
|
||||
$mail->send();
|
||||
echo "Thank you! Your RSVP has been sent.";
|
||||
echo 'RSVP submitted successfully.';
|
||||
} catch (Exception $e) {
|
||||
error_log("RSVP mail error: {$mail->ErrorInfo}");
|
||||
echo "There was an error sending your RSVP. Please try again later.";
|
||||
echo "RSVP could not be sent. Mailer Error: {$mail->ErrorInfo}";
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user