fix(mail): properly encode mail subject into utf8

This commit is contained in:
2025-12-09 14:25:12 +01:00
parent 41b903cd00
commit b6a0528b80

View File

@@ -30,6 +30,19 @@ function validateEmail($email)
return preg_match(EMAIL_REGEX, $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 * Sanitize input string
*/ */
@@ -206,7 +219,7 @@ function sendEmail($subject, $xmlContent, $toEmail = null)
$headers .= "Content-Type: application/xml; charset=UTF-8\r\n"; $headers .= "Content-Type: application/xml; charset=UTF-8\r\n";
$headers .= "X-Mailer: PHP/" . phpversion(); $headers .= "X-Mailer: PHP/" . phpversion();
$result = mail($to, $subject, $xmlContent, $headers); $result = mail($to, encodeEmailSubject($subject), $xmlContent, $headers);
if ($result) { if ($result) {
writeLog("Email sent successfully via mail()", 'SUCCESS'); writeLog("Email sent successfully via mail()", 'SUCCESS');
@@ -253,7 +266,7 @@ function sendEmailSMTP($subject, $xmlContent, $to, $from)
// Content - XML as body // Content - XML as body
$mail->isHTML(false); $mail->isHTML(false);
$mail->Subject = $subject; $mail->Subject = encodeEmailSubject($subject);
$mail->Body = $xmlContent; $mail->Body = $xmlContent;
$mail->ContentType = 'text/plain'; $mail->ContentType = 'text/plain';
@@ -387,7 +400,7 @@ function sendEmailSMTP($subject, $xmlContent, $to, $from)
// Send email headers and body // Send email headers and body
$headers = "From: {$from}\r\n"; $headers = "From: {$from}\r\n";
$headers .= "To: {$to}\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 .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n"; $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .= "\r\n"; $headers .= "\r\n";