From b6a0528b801956d23effed002006a6862b69eb3f Mon Sep 17 00:00:00 2001 From: WorldTeacher Date: Tue, 9 Dec 2025 14:25:12 +0100 Subject: [PATCH] fix(mail): properly encode mail subject into utf8 --- functions.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/functions.php b/functions.php index 0eb2dd7..ac24ca0 100644 --- a/functions.php +++ b/functions.php @@ -30,6 +30,19 @@ function validateEmail($email) return preg_match(EMAIL_REGEX, $email); } +/** + * Encode email subject to handle umlauts and special characters (RFC 2047) + */ +function encodeEmailSubject($subject) +{ + // Check if subject contains non-ASCII characters + if (preg_match('/[^\x20-\x7E]/', $subject)) { + // Use RFC 2047 encoding: =?charset?encoding?encoded-text?= + return '=?UTF-8?B?' . base64_encode($subject) . '?='; + } + return $subject; +} + /** * Sanitize input string */ @@ -206,7 +219,7 @@ function sendEmail($subject, $xmlContent, $toEmail = null) $headers .= "Content-Type: application/xml; charset=UTF-8\r\n"; $headers .= "X-Mailer: PHP/" . phpversion(); - $result = mail($to, $subject, $xmlContent, $headers); + $result = mail($to, encodeEmailSubject($subject), $xmlContent, $headers); if ($result) { writeLog("Email sent successfully via mail()", 'SUCCESS'); @@ -253,7 +266,7 @@ function sendEmailSMTP($subject, $xmlContent, $to, $from) // Content - XML as body $mail->isHTML(false); - $mail->Subject = $subject; + $mail->Subject = encodeEmailSubject($subject); $mail->Body = $xmlContent; $mail->ContentType = 'text/plain'; @@ -387,7 +400,7 @@ function sendEmailSMTP($subject, $xmlContent, $to, $from) // Send email headers and body $headers = "From: {$from}\r\n"; $headers .= "To: {$to}\r\n"; - $headers .= "Subject: {$subject}\r\n"; + $headers .= "Subject: " . encodeEmailSubject($subject) . "\r\n"; $headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n"; $headers .= "\r\n";