Issue Reading Some Emails in SuiteCRM - PHP Fatal Error (HtmlSanitizer)

The solution modifying line 116 of public/legacy/include/HtmlSanitizer.php removes the error, but affected emails still don’t display their body content.
After investigating one level up in the function call stack, I identified that the issue occurs with multipart/mixed emails containing a text/plain part. While I haven’t fully traced the root cause in the SuiteCRM codebase (my knowledge in PHP and suiteCRM code is limited), I’ve developed a temporary workaround.

Temporary Solution

I implemented the following workaround at line 4151 of public/legacy/InboundEmail/InboundEmail.php:

$emailMessage = $this->mailParser->parse($emailBody, false)->getHtmlContent(); // Original line 4150

// Start of workaround
if (!is_string($emailMessage)) {
    $emailMessage = $this->mailParser->parse($emailBody, false)->getTextContent();
    $clean_email = false;
}
// End of workaround

$emailMessage = $this->handleInlineImages($emailBody, $emailMessage); // Original line 4151

For multipart/mixed emails with text/plain parts, getHtmlContent() should not be called since there is no HTML content to retrieve. In these cases, getHtmlContent() returns null, which we can use to detect this situation. When null is detected, the workaround calls getTextContent() instead to properly retrieve the plain text content. While this solution works for my problematic mails, the underlying issue likely stems from incorrect email type detection earlier in the codebase.

Note: This is a temporary fix and should be replaced with a proper solution once the root cause is identified.

1 Like