View Full Version : html tags in mail()

01-24-2008, 12:55 PM
i am trying to format the feedback forum mail tht ppl can send...
i am getting the html tags as it is than the effect

i am using GMAIL


//check for required fields
if((!$_POST[name]) || (!$_POST[email]) || (!$_POST[comments])) {
echo " Please fill Up the Required Fields ".'<br /><a href="javascript:history.go(-1)">'."Click Here".'</a>';

$company = strtoupper($_REQUEST['company']) ;
$name = ucfirst($_REQUEST['name']) ;
$email = $_REQUEST['email'] ;
$phone = $_REQUEST['phone'] ;
$address = $_REQUEST['address'] ;
$city = $_REQUEST['city'] ;
$zipcode = $_REQUEST['zipcode'] ;
$comments = $_REQUEST['comments'] ;

$message ="<strong>Company:</strong> $company \n<strong>Name:</strong> $name \n<strong>Email:</strong> $email \n<strong>Phone:</strong> $phone \n<strong>Address:</strong> $address \n<strong>City:</strong> $city \n<strong>Zip Code:</strong> $zipcode \n\n<strong>Comments:</strong> $comments";

mail( "hmvrulz@gmail.com", "HMV.co.in : $name",
$message, "From: $email" );
header( "Location: index.html" );

01-24-2008, 01:45 PM
I don't understand. What are you trying to do?

What error are you getting?

01-24-2008, 02:46 PM
I think he's saying that the contents of $message are showing up literally, rather than being treated as HTML.

Try passing headers to the mail function that flag the message as HTML. Check the PHP reference for how to do this. Also, I noticed in a comment there that gmail needs the header lines to be terminated by a real newline (\n) rather than a Windows newline (\r\n).

01-24-2008, 07:40 PM
You could do this yourself or you could use a pre-written class such as PHPMailer or PEAR::mail to do it. Those classes have proven code in them and are re-usable. Sending mail with PHP is a little more complicated than just using the mail function.

If you want the mail to get to accounts on the big mail services, and survive all the things that can happen to email messages between the time that the MTA gets the message from mail(), till it reaches it's destination, the format and in many cases the content, have to be just so.

I suppose if you had an interest in learning the MIME email spec, you could put together your own class, but if you just want it to work and move on, using the above mentioned classes is the way to go. Especially when you want to do attachments and HTML. There is definitely no excuse for writing the same email code out for multiple projects, rather than just putting together some re-usable scripts to do it.