Rewrote rsvp-submit.php again #4
This commit is contained in:
+33
-34
@@ -1,59 +1,58 @@
|
|||||||
<?php
|
<?php
|
||||||
require __DIR__ . '/vendor/autoload.php'; // adjust path if needed
|
|
||||||
use PHPMailer\PHPMailer\PHPMailer;
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
use PHPMailer\PHPMailer\Exception;
|
use PHPMailer\PHPMailer\Exception;
|
||||||
|
|
||||||
// Load .env from /var/www/markinstefan.xyz/.env
|
require __DIR__ . '/vendor/autoload.php'; // <- same directory as rsvp-submit.php
|
||||||
$envPath = '/var/www/markinstefan.xyz/.env';
|
|
||||||
if (!file_exists($envPath)) {
|
|
||||||
exit('Missing .env file!');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Simple parser for key=value lines
|
// Load .env
|
||||||
$dotenv = [];
|
$dotenvPath = __DIR__ . '/../.env';
|
||||||
foreach (file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
|
if (!file_exists($dotenvPath)) {
|
||||||
if (preg_match('/^\s*([A-Z_]+)\s*=\s*(.*)\s*$/', $line, $matches)) {
|
exit("Missing .env file");
|
||||||
$dotenv[$matches[1]] = trim($matches[2], "\"'");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$env = parse_ini_file($dotenvPath, false, INI_SCANNER_RAW);
|
||||||
|
|
||||||
// Form validation
|
$SMTP_HOST = $env['SMTP_HOST'] ?? '';
|
||||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') exit('Invalid request');
|
$SMTP_PORT = $env['SMTP_PORT'] ?? 587;
|
||||||
if (!empty($_POST['website'])) exit; // honeypot
|
$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'] ?? '');
|
$first_name = htmlspecialchars($_POST['first_name'] ?? '');
|
||||||
$last_name = htmlspecialchars($_POST['last_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'] ?? '');
|
$allergies = htmlspecialchars($_POST['allergies'] ?? '');
|
||||||
if (!$first_name || !$last_name) exit('Missing required fields');
|
|
||||||
|
|
||||||
// Compose message
|
if (!$first_name || !$last_name) exit("Missing required fields");
|
||||||
$subject = "New Wedding RSVP from $first_name $last_name";
|
|
||||||
$body = "Name: $first_name $last_name\n";
|
|
||||||
$body .= "Drinks: $drinks\n";
|
|
||||||
$body .= "Allergies: $allergies\n";
|
|
||||||
|
|
||||||
// Send email via PHPMailer
|
// Prepare email
|
||||||
$mail = new PHPMailer(true);
|
$mail = new PHPMailer(true);
|
||||||
try {
|
try {
|
||||||
$mail->isSMTP();
|
$mail->isSMTP();
|
||||||
$mail->Host = $dotenv['SMTP_HOST'];
|
$mail->Host = $SMTP_HOST;
|
||||||
$mail->SMTPAuth = true;
|
$mail->SMTPAuth = true;
|
||||||
$mail->Username = $dotenv['SMTP_USER'];
|
$mail->Username = $SMTP_USER;
|
||||||
$mail->Password = $dotenv['SMTP_PASS'];
|
$mail->Password = $SMTP_PASS;
|
||||||
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||||||
$mail->Port = (int)$dotenv['SMTP_PORT'];
|
$mail->Port = $SMTP_PORT;
|
||||||
|
|
||||||
$mail->setFrom($dotenv['FROM_EMAIL'], $dotenv['FROM_NAME']);
|
$mail->setFrom($FROM_EMAIL, $FROM_NAME);
|
||||||
$mail->addAddress($dotenv['TO_EMAIL']);
|
$mail->addAddress($TO_EMAIL);
|
||||||
|
|
||||||
$mail->Subject = $subject;
|
// Email subject & body
|
||||||
$mail->Body = $body;
|
$mail->Subject = "New Wedding Guest RSVP Form: $first_name $last_name";
|
||||||
|
$mail->Body = "Name: $first_name $last_name\nDrinks: $drinks\nAllergies: $allergies\n";
|
||||||
|
|
||||||
$mail->send();
|
$mail->send();
|
||||||
echo "Thank you! Your RSVP has been sent.";
|
echo "Thank you! Your RSVP has been sent.";
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
error_log("Mailer Error: {$mail->ErrorInfo}");
|
error_log("RSVP mail error: {$mail->ErrorInfo}");
|
||||||
echo "Sorry, something went wrong. Please try again later.";
|
echo "There was an error sending your RSVP. Please try again later.";
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user