Formatting HTML emails retrieved using imap

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
voidstate
Forum Newbie
Posts: 8
Joined: Wed Jan 22, 2003 8:01 am

Formatting HTML emails retrieved using imap

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you might use <iframe>s to show the email (can't believe I write this, I hate iframes, but anyway...)
voidstate
Forum Newbie
Posts: 8
Joined: Wed Jan 22, 2003 8:01 am

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)
voidstate
Forum Newbie
Posts: 8
Joined: Wed Jan 22, 2003 8:01 am

Post 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
Post Reply