What are some best practices for handling email decoding and encoding in PHP scripts to avoid issues with special characters?

When handling email decoding and encoding in PHP scripts, it is important to use the appropriate functions to avoid issues with special characters. One common approach is to use the mb_encode_mimeheader() function to properly encode special characters in email headers and mb_decode_mimeheader() function to decode them.

// Example of encoding a header with special characters
$subject = "Subject with special characters áéíóú";
$encoded_subject = mb_encode_mimeheader($subject, 'UTF-8');

// Example of decoding a header with special characters
$decoded_subject = mb_decode_mimeheader($encoded_subject);