In what scenarios should PHP developers avoid using htmlentities in email scripts to prevent delivery errors?

When sending emails in PHP, developers should avoid using htmlentities on the email content as it can cause delivery errors or render the email unreadable in some email clients. Instead, developers should use the mb_encode_mimeheader function to properly encode special characters in email headers.

// Use mb_encode_mimeheader to encode special characters in email headers
$subject = 'Subject with special characters áéíóú';
$encoded_subject = mb_encode_mimeheader($subject, 'UTF-8');

// Send email with properly encoded subject
mail('recipient@example.com', $encoded_subject, 'Email content', 'From: sender@example.com');