diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index 3995bffe3..440de92b9 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -597,31 +597,35 @@ function rcmail_print_body($part, $p = array())
}
// text/html
else if ($part->ctype_secondary == 'html') {
- // charset was converted to UTF-8 in rcube_imap::get_message_part() -> change charset specification in HTML accordingly
- $html = $part->body;
- if (preg_match('/(\s+content=[\'"]\w+\/\w+;\s*charset)=([a-z0-9-_]+)/i', $html))
- $html = preg_replace('/(\s+content=[\'"]\w+\/\w+;\s*charset)=([a-z0-9-_]+)/i', '\\1='.RCMAIL_CHARSET, $html);
- else {
- // add
for malformed messages, washtml cannot work without that
- if (!preg_match('/(.*)<\\/head>/Uims', $html))
- $html = '' . $html;
- $html = substr_replace($html, '', intval(stripos($html, '')), 0);
- }
-
- // PHP bug #32547 workaround: remove title tag
- $html = preg_replace('/.*<\/title>/', '', $html);
+ $html = $part->body;
// special replacements (not properly handled by washtml class)
$html_search = array(
'/(<\/nobr>)(\s+)()/i', // space(s) between
'/(<[\/]*st1:[^>]+>)/i', // Microsoft's Smart Tags
+ '/.*<\/title>/i', // PHP bug #32547 workaround: remove title tag
+ '/]*>/im', // malformed html: remove html tags (#1485139)
+ '/<\/html>/i', // malformed html: remove html tags (#1485139)
);
$html_replace = array(
'\\1'.' '.'\\3',
'',
+ '',
+ '',
+ '',
);
$html = preg_replace($html_search, $html_replace, $html);
+ // charset was converted to UTF-8 in rcube_imap::get_message_part() -> change charset specification in HTML accordingly
+ if (preg_match('/(\s+content=[\'"]\w+\/\w+;\s*charset)=([a-z0-9-_]+)/i', $html))
+ $html = preg_replace('/(\s+content=[\'"]\w+\/\w+;\s*charset)=([a-z0-9-_]+)/i', '\\1='.RCMAIL_CHARSET, $html);
+ else {
+ // add head for malformed messages, washtml cannot work without that
+ if (!preg_match('/]*>(.*)<\/head>/Uims', $html))
+ $html = ''. $html;
+ $html = substr_replace($html, '', intval(stripos($html, '')), 0);
+ }
+
// clean HTML with washhtml by Frederic Motte
$wash_opts = array(
'show_washed' => false,