Page 1 of 1

Formatting HTML emails retrieved using imap

Posted: Tue Mar 04, 2003 5:11 am
by voidstate
Hi

I have managed to write an application for checking POP3 and imap mailboxes, but I have a problem with HTML emails.

They include head tags which are messing up my page layout. It means that css is not inheritied by the content of the mail.

A second problem is that css within the message needs to be kept to allow text and other elements to display correctly, but at the same time some html emails sometimes have css that is applied to the body (such as backgrounds). This then gets applied to the whole page and obviously spoils the layout.

Has anyone encountered this before? I can't think of how to just remove css formatting that is applied to text/images/etc., but not the body, or to remove the whole head html but leave the necessary css.

Cheers

Fergus

Posted: Tue Mar 04, 2003 5:39 am
by volka
you might use <iframe>s to show the email (can't believe I write this, I hate iframes, but anyway...)

Posted: Tue Mar 04, 2003 5:42 am
by voidstate
urgh! me too. Hopefully I can find a better solution, perhaps a complicated eregi-based function?

Thanks for the reply though, volka,

fergus

Posted: Tue Mar 04, 2003 6:00 am
by volka
for embedded styles maybe a pattern like

Code: Select all

$pattern = '/([^\\s{}])\s*{([^}]*)}/';
preg_match_all($pattern, $styleElement, $matches);
then you could add those values to your own header, maybe even prepend another element/class to prevent the elements of your page from beeing altered
(e.g. div { background-color: silver; } :arrow: div.mail div { background-color: silver; } )

(haven't tested this, only an idea)

Posted: Tue Mar 04, 2003 6:06 am
by voidstate
Found this on the web which appears to do the job:

Code: Select all

$toBeRemovedArray = array (
				"'<html>'si",
				"'</html>'si",
				"'<body&#1111;^>]*>'si",
				"'</body>'si",
				"'<head&#1111;^>]*>.*?</head>'si",
				"'<style&#1111;^>]*>.*?</style>'si",
				"'<script&#1111;^>]*>.*?</script>'si",
				"'<object&#1111;^>]*>.*?</object>'si",
				"'<embed&#1111;^>]*>.*?</embed>'si",
				"'<applet&#1111;^>]*>.*?</applet>'si",
			);
$body = preg_replace($toBeRemovedArray, '', $body);


Thanks for your help, volka. Much appreciated. :D

Fergus