Click to See Complete Forum and Search --> : Using php to send email in html format


xenia
05-08-2006, 11:53 AM
Hello everyone,

I am using the mail function in order to send an email with the information taken from a form.
The function is working fine but when I tryied to format the body of the message using html code it just print this:



<html><head><title>Enquiry Form</title></head><body>
<table border='1'><tr><td>Enquiry Form</td></tr>
<tr><th>Pick up date</th>
<th>Pick up hour</th>
<th>Drop off date</th>
<th>Drop off hour</th>
<th>Car Type</th>
<th>Price</th>
<th>Prefix</th>
<th>Prefix</th>
</tr>
<tr><td>08/05/2006</td>
<td>19:00</td>
<td>11/05/2006</td>
<td>19:00</td>
<td>Fiat</td>
<td></td>
<td>Ms</td>
<td>Xenia</td>
</tr></table></body></html>


And here is the code:

$to = "xenia@test.com";
$subject = "My subject";
//$txt = "Pick up date: ". $pickupdate . $dropoffdate . $prefix;
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

$txt= "<html><head><title>Emver Enquiry Form</title></head><body>
<table border=\"1\"><tr><td>Emver Enquiry Form</td></tr>
<tr><th>Pick up date</th>
<th>Pick up hour</th>
<th>Drop off date</th>
<th>Drop off hour</th>
<th>Car Type</th>
<th>Price</th>
<th>Prefix</th>
<th>Prefix</th>
</tr>
<tr><td>". $pickupdate ."</td>
<td>". $pickuphour . "</td>
<td>". $dropoffdate . "</td>
<td>". $dropoffhour . "</td>
<td>". $model . "</td>
<td>". $price . "</td>
<td>". $prefix . "</td>
<td>". $firstname . "</td>
</tr></table></body></html>";




mail($to,$subject,$txt,$headers);


Can you please help me where is the error?

Thank you,
Xenia

Huevoos
05-08-2006, 11:59 AM
$headers . = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

have to tell it is html in the header

xenia
05-09-2006, 04:08 AM
I wrote the two lines you suggest and now a strange thing happens.When I send the email to a hotmail account it displays correct the format. When I send to a gmail and outlook account it displays the following:



Content-type: text/html; charset=iso-8859-1 From: webmaster@example.com
CC: somebodyelse@example.com
X-NAS-Language: English
X-NAS-Bayes: #0: 1; #1: 1.95286E-022
Subject: [Norton AntiSpam] My subject
X-NAS-Classification: 1
X-NAS-MessageID: 952
X-NAS-Validation: {A67171EF-25FF-4809-BCA5-0B20FE39D386}

<html><head><title> Enquiry Form</title></head><body>
<table border="1"><tr><td>Emver Enquiry Form</td></tr>
<tr><th>Pick up date</th>
<th>Pick up hour</th>
<th>Drop off date</th>
<th>Drop off hour</th>
<th>Car Type</th>
<th>Price</th>
<th>Prefix</th>
<th>Prefix</th>
</tr>
<tr><td>10/05/2006</td>
<td>08:00</td>
<td>12/05/2006</td>
<td>08:00</td>
<td>Fiat</td>
<td></td>
<td>Mr</td>
<td>test</td>
</tr></table></body></html>

[/HTML]




What you think might be the problem?


Thanks,
Xenia

Huevoos
05-09-2006, 09:50 AM
mmm..... try changing the order of the headers this way

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

xenia
05-09-2006, 11:12 AM
I tryied but again the same problem.

What else you think?

Huevoos
05-09-2006, 11:32 AM
mmm..... dunno have you looked at the mail man??? http://mx.php.net/manual/en/function.mail.php
Is very strange, here are the headers of the contactform on my blog, see if those work for you:

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Alguien desde EsBasura <'. $from . '>' . "\r\n";

if($mail = mail($to, $subject, $body, $headers))
return "<b style='color: yellow; font-size: 125%;'>Mail enviado</b>";
else
return "<b style='color: red; font-size: 125%;'>Mail no se pudo enviar</b>";
}

Louigi Verona
05-10-2006, 12:18 AM
I have had a similar problem and guys from these forums helped me a lot. The problem with MIME headers is that a slight thing spoils it - additional space added to the end of the string and it might not work, you have to be very precise. In your example there probably an \r\n missing somewhere.

However, there is this great class to handle ANY kind of e-mail. Once I bumped into it, I never had any probelms with email again! Here's the link to their website - this class is free for everybody's use.

PHP Mailer (http://phpmailer.sourceforge.net/)

The site has examples how the class works. It handles all the MIME headers for you. Try it and forget about all those tiring MIME coding.

The Little Guy
05-10-2006, 12:21 AM
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Who this is from \r\n" . "X-Mailer: PHP/" . phpversion();

$result = mail("$emailadd", "$subject", $message, $headers);

NogDog
05-10-2006, 01:36 AM
I have had a similar problem and guys from these forums helped me a lot. The problem with MIME headers is that a slight thing spoils it - additional space added to the end of the string and it might not work, you have to be very precise. In your example there probably an \r\n missing somewhere.

However, there is this great class to handle ANY kind of e-mail. Once I bumped into it, I never had any probelms with email again! Here's the link to their website - this class is free for everybody's use.

PHP Mailer (http://phpmailer.sourceforge.net/)

The site has examples how the class works. It handles all the MIME headers for you. Try it and forget about all those tiring MIME coding.
I've been using that, too. It's very useful and handles many different issues you might run into sending emails, such as adding attachments. It also comes with a SMTP class which can be useful in certain situations.

Louigi Verona
05-10-2006, 03:07 AM
I've been using that, too. It's very useful and handles many different issues you might run into sending emails, such as adding attachments. It also comes with a SMTP class which can be useful in certain situations.

Yep! Actually, SMTP was the reason why I started searching for such a class in the first place. What I found was even more.