Allaire Homesite/PHP form email
Expert: Mark Jenkins - 9/10/2007
QuestionI am currently using a contact form on our website that was developed by someone elses long before I got here. Currently the form is working great on the website, the problem comes in when the information is sent to out in box. Basically, it has no formatting and is hard for everyone to read. My question is if there is a simple mistake in the following code.
<?php
//A very simple function to check if the email address is in the right format
Function validEmail($email)
{
if (!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+.[a-z]{2,4}$", $email))
return 0;
else
return 1;
}
Function cleanBody($text)
{
//First we see hand all qoutes correctly
$text = get_magic_quotes_gpc() ? stripslashes($text) : $text;
//Wordwrap anything longer than 80 characters
$text = wordwrap($text, 80, "
");
return $text;
}
//Function to create email Header createHeader( string $name, string $email, bool $enable_html_format);
Function createHeader($fromName, $fromEmail, $HtmlFormat)
{
// Added the Basic Header Stuff ( From, Replay to, Return Path
$header = "From: " . $fromName;
$header .= "<" . $fromEmail . ">
";
$header .= "Reply-To: " . $fromEmail . "
";
$header .= "Return-Path: " . $fromEmail;
// To send HTML mail, the Content-type header must be set
$header .= 'MIME-Version: 1.0' . "
";
if($HtmlFormat)
$header .= 'Content-type: text/html; charset=iso-8859-1' . "
";
return $header;
}
$errors = 0;
$SystemMessage = "";
if (isset($_POST['Submit'])) {
/* First let's check if everything is set */
if (empty($_POST['Name']))
{
$SystemMessage .= "<br>Please enter your name";
$errors = 1;
}
if (empty($_POST['Address']))
{
$SystemMessage .= "<br>Please enter your address";
$errors = 1;
}
if (empty($_POST['City']))
{
$SystemMessage .= "<br>Please enter your City";
$errors = 1;
}
if (empty($_POST['State']))
{
$SystemMessage .= "<br>Please enter your State";
$errors = 1;
}
if (empty($_POST['zipcode']))
{
$SystemMessage .= "<br>Please enter your Zip Code";
$errors = 1;
}
/* Check our Email */
if ( empty($_POST['Email']) ) {
$SystemMessage .= "<br>Please enter an email address";
$errors = 1;
}
/* Check our telephones number */
if ( empty($_POST['HomeTele']) ){
if( empty($_POST['CellTele']) ){
if( empty($_POST['WorkTele']) ) {
$messages .= "<br>Please enter a Phone Number";
$errors = 1;
}
}
}
if (!$errors)
{
//Mail Specific Stuff
$receiver = me@mysite.com;
//Enter a Test Email when needed
//$receiver = "@.com";
$subject = 'Inquiry from the Website';
$body = "<h2> General Information </h2>";
$body .= "Name : ". $_POST['Name'] ."<br>";
$body .= "Address : ". $_POST['Address'] ."<br>";
$body .= "City : ". $_POST['City'] ." ". $_POST['State'] .",". $_POST['zipcode'] ."<br>";
if(isset($_POST['HomeTele']))
$body .= "Home Phone: ". $_POST['HomeTele'] ."<br>";
if(isset($_POST['CellTele']))
$body .= "Cell Phone: ". $_POST['CellTele'] ."<br>";
if(isset($_POST['WorkTele']))
$body .= "Work Phone: ". $_POST['WorkTele'] ."<br>";
$body .= "Email Address: ". $_POST['Email'] ."<br>";
$body .= "<h2> Pool Information </h2>";
$body .= "Lake/Pond Address: ". $_POST['PondAddress'] ."<br>";
$body .= "Dimensions: ". $_POST['Length'] ."x".$_POST['Width'] . "<br>";
$body .= "Maxium Depth:". $_POST['Max'] ."<br>";
$body .= "Average Depth:". $_POST['avg'] ."<br>";
$body .= "<h2> Additional Remarks </h2>";
$body .= "Reffered by: ". $_POST['referralSource'] ."<br>";
$body .= "which I saw in/on ". $_POST['referral'] ."<br>";
$body .="Questions I have: <br>";
$body .= cleanBody($_POST['questions']);
$headers = createHeader($_POST['Name'],$_POST['Email'],1);
//Simple check to see if the return address is valid.
if (validEmail($_POST["Email"]))
{
//Try and send the message
if ( mail($receiver, $subject, $body, $headers ) )
$SystemMessage = "Thank you, ". $_POST["Name"] .", for your inquiry.";
//Error out if couldn't send
else {
$errors = 1;
$SystemMessage .= "Email System is currently offline. Try again later!";
}
//Error out if invalid address
} else {
$errors = 1;
$SystemMessage .= "Not a valid Email address: ". $from ."<br>";
}
}
//Display any system messages
echo '<div id="'. ($errors ? "warning" : "confirm") .'">'. $SystemMessage .'</div><br>';
}
?>
</p>
<!-- <div class='under'>Under Construction Please Do Not Enter Information</div> -->
<form name="info" action="<?= $_SERVER['PHP_SELF'] ?>" method="POST" >
<table border="0">
<tr>
<td colspan="3" class="bold">General Information: </td>
</tr>
<tr>
<td><span class="style1">*</span> Name:</td>
<td colspan="2"><input name="Name" type="text" size="20" maxlength="30" class="required" value="<?= $_POST['Name'] ?>"></td>
<tr>
<td><span class="style1">*</span> Address:</td>
<td colspan="2"><input name="Address" type="text" id="Address" size="30" maxlength="30" value="<?= $_POST['Address'] ?>"></td>
</tr>
<tr>
<td><span class="style1">*</span> City:</td>
<td><input name="City" type="text" id="City" size="25" maxlength="25" value="<?= $_POST['City'] ?>"></td>
<td>* State:
<select name="State" id="State">
<option SELECTED value="<?= $_POST['State'] ?>"> <?= $_POST['State'] ?></option>
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="DC">DC</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
</select>
* Zip:<input name="zipcode" type="text" id="zipcode" size="5" maxlength="5"value="<?= $_POST['zipcode'] ?>"></td>
</tr>
<tr>
<td><span class="style1">*</span> E-Mail</td>
<td colspan="2"><input name="Email" type="text" id="Email"value="<?= $_POST['Email'] ?>"></td>
</tr>
<tr>
<td colspan="3"><b><span class="style1">*</span> Please enter one of the following</b></td>
</tr>
<tr>
<td>Home Telephone:</td>
<td colspan="2"><input name="HomeTele" type="text" id="HomeTele" size="14" maxlength="14" value="<?= $_POST['HomeTele'] ?>"></td>
</tr>
<tr>
<td>Cell Telephone:</td>
<td colspan="2"><input name="CellTele" type="text" id="CellTele" size="14" maxlength="14" value="<?= $_POST['CellTele'] ?>"></td>
</tr>
<tr>
<td>Work Telephone:</td>
<td colspan="2"><input name="WorkTele" type="text" id="WorkTele" size="14" maxlength="14" value="<?= $_POST['WorkTele'] ?>"></td>
</tr>
</table>
<br>
<table border="0">
<tr>
<td colspan="2" class="bold">Lake/Pond Information:</td>
</tr>
<tr>
<td width="200">Size (feet): Length: <input name="Length" type="text" id="Length" size="5" maxlength="5"value="<?= $_POST['Length'] ?>"></td>
<td width=>Width: <input name="Width" type="text" id="Width" size="5" maxlength="5" value="<?= $_POST['Width'] ?>"></td>
</tr>
<tr>
<td width="200">Depth (feet): Max: <input name="Max" type="text" id="Max" size="5" maxlength="5" value="<?= $_POST['Max'] ?>"></td>
<td width=*>Average Depth: <input name="avg" type="text" id="avg" size="5" maxlength="5" value="<?= $_POST['avg'] ?>"></td>
</tr>
<tr>
<td colspan="2">Street Address of Lake/Pond: <input name="PondAddress" type="text" id="PondAddress" size="25" maxlength="25" value="<?= $_POST['PondAddress'] ?>"></td>
</tr>
</table>
<br>
<table border="0">
<tr>
<td colspan="2" class="bold"><p>Additional Information:</p></td>
</tr>
<tr>
<td colspan="2">How did you hear about us?
<select name="referralSource" id="referralSource">
<option SELECTED value="<?= $_POST['refferalSource'] ?>"> <?= $_POST['referralSource'] ?></option>
<option value="NewsPaper">Paper Ad</option>
<option value="Referral">Referral</option>
<option value="Commerical">Commerical</option>
<option value="Internet">Internet Search</option>
</select><br>
If Ad or Referral: <input name="referral" type="text" id="referral" size="25" maxlength="25" value="<?= $_POST['referral'] ?>"></td>
</tr>
<tr>
<td colspan="2"><p class="bold"><br>How may ABI assist you?<br>
<textarea name="questions" cols="60" rows="10" id="questions"><?= $_POST['questions'] ?></textarea></p></td>
</tr>
<tr>
<td colspan="2">
<center><input type="reset" name="Reset" value="Reset"> <input type="submit" name="Submit" value="Submit">
</center>
</td>
</tr>
</table>
</form>
<!--- Body Ends here ---!>
AnswerHi,
As I see it, there is no simple fix for the problem.
To format such an email in the way that you wish, HTML is required. There is very little HTML code in this sample that you sent me. it seems to be designed to output mostly in plain text.
You should be able to find a ferr script to do this on the web, quite easily.
Altenatively, a decent programmer could fix this up (or most likely write a new one) in 1-2 hours.
Regards,
Mark Jenkins