Click to See Complete Forum and Search --> : email contains HTML tags :(
swigrid
01-30-2008, 09:20 AM
hi,
my email sent by PHP contains HTML tags.
my code is:
$from = "info@xxx.co.uk";
$subject = "Verification E-mail";
// Common Headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: <'.$from.'>' . "\r\n";
$message = '
<html>
<head>
</head>
<body>
<p>
content
</p>
</body>
</html>
';
mail($to, $subject, $message, $headers);
My hosting company uses PHP 4.3 on their server if it is important.
Yelgnidroc
01-30-2008, 03:16 PM
Hope the following working example helps:
<?php
/* now we will create a MIME e-mail using $html_body and $text_body */
/* First the HTML body */
$html_body = '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
</head>
<style type="text/css" media="screen">
table
{
border-collapse:collapse;
border: 3pt ridge black;
background-color:#05a2e5;
width=600px;
}
td
{
font-family: "Arial", "Helvetica ", ;
color:black ;
Font-Size : 14px;
vertical-align:center;
background-color:#fff99d;
border: 1px solid black; padding:8px;
}
h1 {text-align:center; vertical-align:center; Font-Size : 22px; background-color=#05a2e5; color:yellow;}
p {font-family: "Arial", "Helvetica ", ; color:black ; Font-Size : 14px;
text-align:left; vertical-align:center; background-color:"#05a2e5"}
</style>
<style type="text/css" media="print">
table
{
border-collapse:collapse;
border: 3pt ridge black;
background:#05a2e5;
color:blue;
}
td
{
font-family: "Arial", "Helvetica ", ;
color:blue ;
Font-Size : 10pt;
vertical-align:center;
background-color:#fff99d;
border: 1px solid black; padding:3px;
}
h1 {text-align:center; vertical-align:center; Font-Size : 20pt; background-color=#05a2e5; color:yellow;}
p {font-family: "Arial", "Helvetica ", ; color:blue ; Font-Size : 10pt;
text-align:left; vertical-align:center; background:#05a2e5}
</style>
<body>
<!-- Main table start -->
<table>
<tr><td colspan=2; style="background-color:#05a2e5;">
<h1>Contact Form<br /></h1>
<br />
<p>
Thank you very much indeed for your enquiry; it is very much appreciated. Would you
please check the details below and advise us if you need to modify anything.
</p>
<p>We will endeavour to reply as soon as possible. Please call 0000000000 if you require an immediate response.
</p>
<p>
Kind regards.
<br />
<br />
xxxxxxxxxxxxxxxx
<br />
<br />
</p>
</td></tr>
<tr>
<td>Date and time form completed</td>
<td>' . date('d.m.Y') . ' - ' . date('G:i:s') . '</td></tr>';
$html_body .= "<tr><td>Name</td><td>" . $_POST['salutation'] . ' ' .$_POST['name'] . "</td></tr>";
$html_body .= "<tr><td>Telephone Number</td><td>" . $_POST['telephone'] . "</td></tr>";
$html_body .= "<tr><td>email address</td><td>" . $_POST['email'] . "</td></tr>";
$html_body .= "<tr><td>How we heard about</td><td>" . $_POST['referred_by'] . "</td></tr>";
$html_body .= "<tr><td>Notes</td><td>" . $_POST['notes'] . "</td></tr>";
$html_body .= "<tr><td>" . $_SERVER['REMOTE_ADDR'] . "</td><td>" . $_SERVER['HTTP_REFERER'] . "</td></tr>";
/* Now the plain text body */
$text_body = "Thank you very much indeed for your enquiry; it is very much appreciated. Would you please check the details below and advise us if you need to modify anything.\n\n";
$text_body .= "We will endeavour to reply as soon as possible. Please call 0000000000 if you require an immediate response.\n\n";
$text_body .= "Kind regards.\n\nxxxxxxxxxx\n\n\n";
$text_body .= 'Date ' . date('d.m.Y') . ' - Time ' . date('G:i:s') . "\n\n";
$text_body .= "Name --------------- " . $_POST['salutation'] . ' ' .$_POST['name'] . "\n\n";
$text_body.= "Telephone Number---- " . $_POST['telephone'] . "\n\n";
$text_body.= "email address------- " . $_POST['email'] . "\n\n";
$text_body.= "How we heard about - " . $_POST['referred_by'] . "\n\n";
$text_body.= "Notes -------------- " . $_POST['notes'] . "\n\n";
$text_body.= $_SERVER['REMOTE_ADDR'] . '- Referrer ' . $_SERVER['HTTP_REFERER'];
/* now create the MIME e-mail */
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/related;\n" .
" boundary=\"{$mime_boundary}\"";
$message=NULL;
$message .=
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$text_body . "\n\n";
// html part of message
$message .=
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$html_body . "\n\n";
$message .= "--{$mime_boundary}--\n";
$subject = 'Website Contact Form - ' . $_POST['salutation'] . ' ' .$_POST['name'];
$from = $_POST['email'];
$to = 'sales@xxxxxxxx.co.uk';
mail($to,$subject,$message,"From:$from".$headers);
$from = 'sales@xxxxxxxx.co.uk';
$to = $_POST['email'];
mail($to,$subject,$message,"From:$from".$headers);
?>
Yelgnidroc
01-30-2008, 03:19 PM
PS, there is more to the form that precedes this code to to spam relaying and sql database injections.
I always send e-mail in html and plain text in case the recipient's mail client can't read html.
swigrid
01-31-2008, 04:30 AM
Thank you very much, your example works very well for me.
GavinPearce
01-31-2008, 08:57 AM
Hey Yelgnidroc,
Could you possibly explain the $mime_boundary part for me? Have to admit I'm not 100% sure what the use in generating this MD5 sum is. Looks interesting though!
paulkoan
02-14-2008, 06:03 AM
A mime boundery just needs to be unique, and generating an md5 from a random number is a fairly conclusive way of reducing the chance of collisions :)
GavinPearce
02-14-2008, 06:06 AM
Hey Paul
I see. :D
Thanks!