PDA

View Full Version : html table, arrrghhh!!



developing
11-27-2008, 12:07 AM
can someone please help me to insert a table ( html table) into the $body variable, i am constantly getting errors; i have no hair left code is below:

<?php
// get posted data into local variables

$EmailTo = "emailt@theemail.com";
$Subject .= "Guest List, $Days";
$Days = Trim(stripslashes($_POST['Days']));
$Day = Trim(stripslashes($_POST['Day']));
$Month = Trim(stripslashes($_POST['Month']));
$FirstName = Trim(stripslashes($_POST['FirstName']));
$SecondName = Trim(stripslashes($_POST['SecondName']));
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$ConEmail = Trim(stripslashes($_POST['ConEmail']));

// validation
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (Trim($Days)=="") $validationOK=false;
if (Trim($Day)=="") $validationOK=false;
if (Trim($Month)=="") $validationOK=false;
if (Trim($FirstName)=="") $validationOK=false;
if (Trim($SecondName)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}

// prepare email body text
$Body = "";
$Body .= "Requested Date: ";
$Body .= "$Days, | $Day | $Month";
$Body .= "\n";
$Body .= "First Name: ";
$Body .= $FirstName;
$Body .= "\n";
$Body .= "Second Name: ";
$Body .= $SecondName;
$Body .= "\n";



// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom> ");

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

BabblingIdjit
11-27-2008, 03:58 AM
$Body .= '<table><tr><td>data here</td></tr></table>';


If you'll be more specific than "i am constantly getting errors", a more specific answer can be given, but this should give you the general idea.

developing
11-27-2008, 11:53 AM
sorry what i mean by errors is that the email will show the html instead of a table;

What i am trying to do is have the information ($firstName etc) sent from a html form and the information to be displayed in a table sent to the recipient.

i would like to have these variables formatted in a table:

$Body = "";
$Body .= "Requested Date: ";
$Body .= "$Days, | $Day | $Month";
$Body .= "\n";
$Body .= "First Name: ";
$Body .= $FirstName;
$Body .= "\n";
$Body .= "Second Name: ";
$Body .= $SecondName;
$Body .= "\n";

Twey
11-27-2008, 01:35 PM
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.

<?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();
?>

developing
11-27-2008, 02:29 PM
ok will try this out when i reach back in the office; i used a php email generator the html form references this file externally ie method="cgi/contact.php"