Description of the problem
I am trying to import email content into a database table. Sometimes, I get an SQL error while inserting a message. I found that it fails when the message content is a binary string instead of a string.
For exemple, I get this in the console if I print a message that is imported successfully (Truncated)

However, I get this with problematic import:

I found out that if I use the function utf8_encode, I am successfully able to import it into SQL. The problem is that it "breaks" previously successfull imports accented characters:

What I have tried
- Detect if the string was a binary string with
ctype_print, returned false for both non binary and binary string. I would have then be able to callutf8_encodeonly if it was binary - Use of
unpack, did not work - Detect string encoding with
mb_detect_encoding, returnUTF-8for both - use
iconv, failed withiconv(): Detected an illegal character in input string - Cast the content as string using
(string)/settype($html, 'string')
Question How can I transform the binary string in a normal string so I can then import it in my database without breaking accented characters in other imports?