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

Dear All,

I have successfully installed a SuiteCRM instance and configured both inbound and outbound email accounts. I can see the list of emails in my INBOX, and for most of the emails, everything works correctly. However, I’ve encountered an issue with some emails.

When I try to read or link certain emails, instead of displaying the email content, I receive the following error message:

[Fri Jan 17 09:59:15.410790 2025] [php:error] [pid 53899:tid 53899] [client 192.168.160.253:58670] PHP Fatal error:  Uncaught TypeError: SuiteCRM\\HtmlSanitizer::clean(): 
Argument #1 ($dirtyHtml) must be of type string, null given, 
called in /var/www/suitecrm/public/legacy/include/HtmlSanitizer.php on line 116 and 
defined in /var/www/suitecrm/public/legacy/include/HtmlSanitizer.php:142\n

Stack trace:
#0 /var/www/suitecrm/public/legacy/include/HtmlSanitizer.php(116): SuiteCRM\\HtmlSanitizer->clean()\n
#1 /var/www/suitecrm/public/legacy/modules/InboundEmail/InboundEmail.php(4154): SuiteCRM\\HtmlSanitizer::cleanHtml()\n
#2 /var/www/suitecrm/public/legacy/modules/InboundEmail/InboundEmail.php(5818): InboundEmail->getMessageTextWithUid()\n
#3 /var/www/suitecrm/public/legacy/modules/Emails/include/DetailView/EmailsNonImportedDetailView.php(89): InboundEmail->returnNonImportedEmail()\n
#4 /var/www/suitecrm/public/legacy/modules/Emails/views/view.detailnonimported.php(77): EmailsNonImportedDetailView->populateBean()\n
#5 /var/www/suitecrm/public/legacy/include/MVC/View/SugarView.php(208): EmailsViewDetailNonImported->preDisplay()\n
#6 /var/www/suitecrm/public/legacy/include/MVC/Controller/SugarController.php(432): SugarView->process()\n
#7 /var/www/suitecrm/public/legacy/include/MVC/Controller/SugarController.php(363): SugarController->processView()\n
#8 /var/www/suitecrm/public/legacy/include/MVC/SugarApplication.php(101): SugarController->execute()\n
#9 /var/www/suitecrm/public/legacy/index.php(52): SugarApplication->execute()\n
#10 {main}\n  thrown in /var/www/suitecrm/public/legacy/include/HtmlSanitizer.php on line 142, referer: https://crm.ciholo.com/

This error only occurs with certain emails, while others display without any problems.

Question:

Does anyone have any ideas about what might be causing this issue or how I could debug/fix it?
I’d appreciate any advice or insights!

Maybe helpful:


Check it on SuiteCRM GitHub issues:

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