Thank you so much for your quick reply and for the fix! I have tested and it's working fine now!
I'm sorry that I didn't mention on the previous post, but there's also 2 "small" issues with this function. According to RFC 2047, the 'encoded-word' may not be more than 75 characters long, including 'charset', 'encoding', 'encoded-text', and delimiters (in this case, multiple 'encoded-word's --separated by CRLF SPACE-- may be used) . And the 'encoded-text' should not allow spaces and the '?' character. ('?' is currently being used 'as is' but instead should be encoded as =3F).
If I encode the subject "Are you sure?" it will result as "=?utf-8?Q?Are_you_sure??=" but should be "=?utf-8?Q?Are_you_sure=3F?=". For curiosity, this only shows as en error with the Mail client from Apple's iOS. All other mail readers that I've tested don't bother with this.
Another example:
"This is a really long subject line with a question mark at the end oh my god it never ends?"
=?UTF-8?Q?This_is_a_really_long_subject_line_with_a_question_mark_at_the_e?=
<CRLF SPACE> =?UTF-8?Q?nd_oh_my_god_it_never_ends=3F?=
For a quick fix, I ended replacing this specific function with my own, but I really want to thank you for taking your time to fix this so quickly. You're amazing!
Thank you!
Regards from Brazil,
Henrique