You need to send the appropriate headers with your email if you wish it to be interpreted as something other than plain text. Since many email clients don't support HTML email (usually by choice), one must always provide a plain-text version of the email as well. The way to do this is to use the multipart/alternative Content-Type.
The rest of your code could do with a tidy-up, too. Remember: in coding, if you find yourself repeating yourself, then either you're Doing It Wrong or you're using a really pants language. In this case, it's a little of both, but mostly the former.
Code:
<?php
// get posted data into local variables
function clean($tring) {
return str_replace(array("\r", "\n"), '', htmlentities(trim(stripslashes($tring))));
}
function absolute($url) {
return $url[0] === '/' ? $url : basename($_SERVER['REQUEST_URI']) . '/' . $url;
}
function fail_error() {
die(header('Location: ' . absolute('error.htm')));
}
function succeed() {
die(header('Location: ' . absolute('ok.htm')));
}
function nl2crlf($tring) {
return str_replace("\n", "\r\n", str_replace("\r", "", $tring));
}
$pvals = array('days', 'day', 'month', 'firstname', 'secondname', 'emailfrom', 'conemail');
$vs = array_map('clean', array_intersect_key(array_fill_keys($pvals, ''), array_change_key_case($_POST)));
if (in_array('', $vs)) fail_error();
unpack($vs);
$to = 'emailt@theemail.com';
$re = "Guest List, $days";
// Define a boundary to separate the HTML part from the rest of the email.
$boundary = '--mail-boundary-' . md5(microtime());
$headers = "From: <$emailfrom>\r\n"
. "Content-Type: multipart/alternative; boundary=$boundary\r\n";
// Prepare HTML email.
ob_start();
?>
<?php echo $boundary; ?>
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title><?php echo $re; ?></title>
</head>
<body>
<table>
<tr>
<th>Requested Date:</th>
<td><?php echo $days; ?> | <?php echo $day; ?> | <?php echo $month; ?></td>
</tr>
<tr>
<th>Given Name:</th>
<td><?php echo $firstname; ?></td>
</tr>
<tr>
<th>Family Name:</th>
<td><?php echo $secondname; ?></td>
</tr>
</body>
</html>
<?php
$html_body = nl2crlf(ob_get_clean());
// Prepare plain-text version for non-HTML clients.
ob_start();
?>
<?php echo $boundary; ?>
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
Requested Date: <?php echo $days; ?> | <?php echo $day; ?> | <?php echo $month; ?>
Given Name: <?php echo $firstname; ?>
Family Name: <?php echo $secondname; ?>
<?php
$text_body = nl2crlf(ob_get_clean());
$body = $text_body . $html_body;
if (mail($emailto, $re, $body, $headers))
succeed();
else
fail_error();
?>
Bookmarks