Added final UTF-8 support to rsvp-submit.php

This commit is contained in:
spetznas
2026-05-17 00:46:26 +02:00
parent 767d37ff88
commit 3496f41596
+18 -14
View File
@@ -1,31 +1,37 @@
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require __DIR__ . '/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use Dotenv\Dotenv;
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../'); // root .env
$dotenv->load();
$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->safeLoad();
// Get form values
$name = $_POST['name'] ?? 'No Name';
$first_name = $_POST['first_name'] ?? '';
$last_name = $_POST['last_name'] ?? '';
$name = trim("$first_name $last_name") ?: 'No Name';
// Drinks can be an array
$drinks = $_POST['drinks'] ?? 'None';
if (is_array($drinks)) {
$drinks = implode(', ', $drinks);
}
// Allergies
$allergies = $_POST['allergies'] ?? 'None';
// Make sure UTF-8
// Convert to UTF-8
$name = mb_convert_encoding($name, 'UTF-8', 'auto');
$drinks = mb_convert_encoding($drinks, 'UTF-8', 'auto');
$allergies = mb_convert_encoding($allergies, 'UTF-8', 'auto');
// PHPMailer setup
$mail = new PHPMailer(true);
try {
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 2; // debug output
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = $_ENV['SMTP_HOST'];
$mail->SMTPAuth = true;
@@ -37,9 +43,7 @@ try {
$mail->setFrom($_ENV['FROM_EMAIL'], $_ENV['FROM_NAME']);
$mail->addAddress($_ENV['TO_EMAIL']);
// Encode subject properly for UTF-8
$subject = "New Wedding RSVP from $name";
$mail->Subject = mb_encode_mimeheader($subject, 'UTF-8');
$mail->Subject = mb_encode_mimeheader("New Wedding Guest: $name", 'UTF-8');
$body = <<<EOD
Name: $name
@@ -48,8 +52,8 @@ Allergies: $allergies
EOD;
$mail->Body = $body;
$mail->send();
echo 'RSVP submitted successfully.';
} catch (Exception $e) {
echo "RSVP could not be sent. Mailer Error: {$mail->ErrorInfo}";