Skip to content
PHP Logo

How can the PHP mail function be configured to handle special characters like ä, ö, ü?

Special characters like ä, ö, ü can be handled in the PHP mail function by setting the proper headers for the email content type and encoding. This can be achieved by specifying the charset as UTF-8 in the email headers. Additionally, the content of the email should be properly encoded using the mb_encode_mimeheader function to ensure that special characters are displayed correctly.

php
$to = 'recipient@example.com';
$subject = 'Subject with special characters ä, ö, ü';
$message = 'Message with special characters ä, ö, ü';

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
$headers .= "From: sender@example.com" . "\r\n";

$subject = mb_encode_mimeheader($subject, "UTF-8");

mail($to, $subject, $message, $headers);