Page 1 of 1

[SOLVED] Detect if an .EML file is plain text or HTML

Posted: Thu Mar 29, 2007 7:29 am
by impulse()
I'm using a Thunderbird plugin to send selected e-mail to PHP to insert into a MySQL database and then I've created a mail centre to view uploaded e-mails. At first I was having problems reading plain text e-mails because new lines weren't being created so I used the following line of code:

Code: Select all

$bodyToDatabase = nl2br(htmlentities($bodyToDatabase));
Which places "<br>" where "\r", "\n" or "\r\n" are in a plain text e-mail. Now my problem

The htmlentities was to show any "<" or ">" that may have been user created in an e-mail.

Now my problem is that HTML e-mails are showing all the HTML tags, which isn't what I want. I think the solution is to handle the data differently depending on whether the HTML is plain text or whether it is HTML. How would PHP determine if an e-mail was a HTML e-mail or plain text?


Regards,

Posted: Thu Mar 29, 2007 7:40 am
by Benjamin
Well you could create a custom function to detect HTML..

Something similar to...

Code: Select all

if (preg_match("#<.*?>#s", $b))
    {

    } else {

    }
might work for you.

Posted: Thu Mar 29, 2007 8:03 am
by impulse()
Silly me. I've just studied the headers for plain-text/HTML e-mails and saw what I should've already known:

HTML E-mail
Content-Type: text/html; charset=ISO-8859-1

Plain-text
Content-Type: text/plain; charset=ISO-8859-1; format=flowed