{"action":"created","issue":{"url":"https://api.github.com/repos/osTicket/osTicket-1.8/issues/2272","labels_url":"https://api.github.com/repos/osTicket/osTicket-1.8/issues/2272/labels{/name}","comments_url":"https://api.github.com/repos/osTicket/osTicket-1.8/issues/2272/comments","events_url":"https://api.github.com/repos/osTicket/osTicket-1.8/issues/2272/events","html_url":"https://github.com/osTicket/osTicket-1.8/issues/2272","id":96398885,"number":2272,"title":"incorrectly parsing HTML in email","user":{"login":"talilon","id":8300542,"avatar_url":"https://avatars.githubusercontent.com/u/8300542?v=3","gravatar_id":"","url":"https://api.github.com/users/talilon","html_url":"https://github.com/talilon","followers_url":"https://api.github.com/users/talilon/followers","following_url":"https://api.github.com/users/talilon/following{/other_user}","gists_url":"https://api.github.com/users/talilon/gists{/gist_id}","starred_url":"https://api.github.com/users/talilon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/talilon/subscriptions","organizations_url":"https://api.github.com/users/talilon/orgs","repos_url":"https://api.github.com/users/talilon/repos","events_url":"https://api.github.com/users/talilon/events{/privacy}","received_events_url":"https://api.github.com/users/talilon/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/osTicket/osTicket-1.8/labels/email","name":"email","color":"fef2c0"},{"url":"https://api.github.com/repos/osTicket/osTicket-1.8/labels/php","name":"php","color":"5319e7"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":15,"created_at":"2015-07-21T19:28:40Z","updated_at":"2015-07-27T18:43:53Z","closed_at":null,"body":"osticket seems to be having trouble parsing messages when rich text is enabled. \r\nemail is parsed fine when rich text is disabled.\r\n![image](https://cloud.githubusercontent.com/assets/8300542/8810109/9c6ac44a-2fa1-11e5-948c-52952452e831.png)\r\n\r\nI'm using osticket v1.9.9-1-gbe2f138\r\nphp 5.3.8\r\nemail was sent from Outlook 2013\r\n"},"comment":{"url":"https://api.github.com/repos/osTicket/osTicket-1.8/issues/comments/125301811","html_url":"https://github.com/osTicket/osTicket-1.8/issues/2272#issuecomment-125301811","issue_url":"https://api.github.com/repos/osTicket/osTicket-1.8/issues/2272","id":125301811,"user":{"login":"greezybacon","id":672074,"avatar_url":"https://avatars.githubusercontent.com/u/672074?v=3","gravatar_id":"","url":"https://api.github.com/users/greezybacon","html_url":"https://github.com/greezybacon","followers_url":"https://api.github.com/users/greezybacon/followers","following_url":"https://api.github.com/users/greezybacon/following{/other_user}","gists_url":"https://api.github.com/users/greezybacon/gists{/gist_id}","starred_url":"https://api.github.com/users/greezybacon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/greezybacon/subscriptions","organizations_url":"https://api.github.com/users/greezybacon/orgs","repos_url":"https://api.github.com/users/greezybacon/repos","events_url":"https://api.github.com/users/greezybacon/events{/privacy}","received_events_url":"https://api.github.com/users/greezybacon/received_events","type":"User","site_admin":false},"created_at":"2015-07-27T18:43:53Z","updated_at":"2015-07-27T18:43:53Z","body":"It looks like the code in `html_balance` may corrupt HTML if it contains a `<head>` element. I'm thinking of proposing a patch like the following. Does it have any bearing on this problem?\r\n```patch\r\ndiff --git a/include/class.format.php b/include/class.format.php\r\nindex b8cfe03..f41cd40 100644\r\n--- a/include/class.format.php\r\n+++ b/include/class.format.php\r\n@@ -120,18 +120,32 @@ class Format {\r\n         if (!extension_loaded('dom'))\r\n             return $html;\r\n \r\n-        if (!trim($html))\r\n-            return $html;\r\n+        if ($html instanceof DomDocument) {\r\n+            $doc = $html;\r\n+        }\r\n+        else {\r\n+            if (!($trimmed = trim($html)))\r\n+                return $html;\r\n \r\n-        $doc = new DomDocument();\r\n-        $xhtml = '<?xml encoding=\"utf-8\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>'\r\n-            // Wrap the content in a <div> because libxml would use a <p>\r\n-            . \"<div>$html</div>\";\r\n-        $doc->encoding = 'utf-8';\r\n-        $doc->preserveWhitespace = false;\r\n-        $doc->recover = true;\r\n-        if (false === @$doc->loadHTML($xhtml))\r\n-            return $html;\r\n+            if ($trimmed != '<')\r\n+                // Wrap the content in a <div> because libxml would use a <p>\r\n+                $body = \"<div>{$body}</div>\";\r\n+\r\n+            if (strpos($body, '<head>'))\r\n+                $body = str_replace('<head>',\r\n+                    '<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>',\r\n+                    $body);\r\n+            else\r\n+                $body = '<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>'.$body;\r\n+\r\n+            $doc = new DomDocument();\r\n+            $xhtml = '<?xml encoding=\"utf-8\">'.$body;\r\n+            $doc->encoding = 'utf-8';\r\n+            $doc->preserveWhitespace = false;\r\n+            $doc->recover = true;\r\n+            if (false === @$doc->loadHTML($xhtml))\r\n+                return $html;\r\n+        }\r\n \r\n         if ($remove_empty) {\r\n             // Remove empty nodes\r\n```"}}