How can PHP be used to send text emails with proper encoding for special characters like umlauts?
When sending text emails with special characters like umlauts using PHP, it is important to properly encode the content to ensure it displays correctly for the recipient. One way to achieve this is by using the PHP `mb_encode_mimeheader()` function to encode the subject line and `mb_encode_mimeheader()` or `mb_encode_numericentity()` for the email body content.
$to = "recipient@example.com";
$subject = "Subject with ümläuts";
$body = "Email body with special characters like ümläuts";
$subject = mb_encode_mimeheader($subject, "UTF-8", "Q");
$body = mb_encode_mimeheader($body, "UTF-8", "Q");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/plain;charset=UTF-8" . "\r\n";
$headers .= "From: sender@example.com" . "\r\n";
mail($to, $subject, $body, $headers);