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

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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

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

Post 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,
Last edited by impulse() on Thu Mar 29, 2007 8:07 am, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

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