What could be causing the random exclamation marks in the text sent using the mail() function in PHP?

The random exclamation marks in the text sent using the mail() function in PHP could be caused by encoding issues. To solve this problem, you can set the content type of the email to text/plain and use the PHP function mb_encode_mimeheader() to properly encode the subject line.

$to = "recipient@example.com";
$subject = "Subject line with special characters";
$message = "Email message content with special characters";

$subject = mb_encode_mimeheader($subject, "UTF-8");
$headers = "From: sender@example.com\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

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