Php email

Any way to add a have a email form that people can select who the email goes to?

Something like this


<td valign="top"">
  <label for="emailto">Instructor(s) *</label>
 </td>
 <td>
 <select name="emailto">
	<option value=""></option>
	<option value="<?php echo $12345 ?>">12345</option>
	<option value="<?php echo $67890 ?>">67890</option>
	<option value="<?php echo $12345 ?>;<?php echo $67890 ?>">Both</option>
	</td>
</tr>

I’m not sure how to write the sent_contact.php page. This is what i have now and it does not work is it possible?

<?php
if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "<?php echo $emailto ?>";
    $email_subject = "CCWS";

Assuming your drop down box contains actual email addresses this should work for you

if (isset($_POST['email']) && isset($_POST['emailto']) && !empty($_POST['emailto'])) {
    // Ensure the email is valid
    if (!preg_match('/^[a-z0-9!#$%&\\'*+\\/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\\'*+\\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+(?:[a-z]{2}|aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel)$/i', $_POST['emailto'])) {
        die('Invalid email address selected!');
    }
    
    $email_to = trim($_POST['emailto']);
    $email_subject = 'CCWS';
} else {
    // DO SOMETHING HERE WHEN NO EMAIL IS SELECTED
}

Great thanks the only part thats not working is the “both” option how would i make it use both email addresses in the form

Try the below

if (isset($_POST['email']) && isset($_POST['emailto']) && !empty($_POST['emailto'])) {
    $sendto = trim($_POST['emailto']);
    
    // Ensure the email is valid
    if (!preg_match('/^[a-z0-9!#$%&\\'*+\\/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\\'*+\\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+(?:[a-z]{2}|aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel)$/i', $sendto)) {
        die('Invalid email address selected!');
    }
    
    if (preg_match('/;/', $sendto)) {
        $sendto = explode(';', $sendto);
        $sendto = $sendto[0] . ', ' . $sendto[1];
    } else {
        $sendto = $sendto;
    }
    
    $email_subject = 'CCWS';
} else {
    // DO SOMETHING HERE WHEN NO EMAIL IS SELECTED
}

Thanks for you continued help I still get the error message. Maybe tho complete code would help. And do you mind showing me a easier to make all fields required? This is the first time I have ever used PHP to send e-mail and am not sure if this is the easiest way to do so.


<!---Start Contact form form---->
<form name="contactform" method="POST" action="sent_contact.php">
<table width="500px">
</tr>
<tr>
 <td valign="top"">
  <label for="emailto">Instructor(s) *</label>
 </td>
 <td>
 <select name="emailto">
	<option value="<?php echo $david ?>">David</option>
	<option value="<?php echo $rob ?>">Rob</option>
	<option value="<?php echo $david ?>;<?php echo $rob ?>">Both</option>
	</td>
</tr>
<tr>
 <td valign="top">
  <label for="first_name">First Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>
 
<tr>
 <td valign="top"">
  <label for="last_name">Last Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="last_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>
 
</tr>
<tr>
 <td valign="top">
  <label for="telephone">10 digit phone Number *<br />e.g. 1235550123</label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>		
	
<tr>
 <td valign="top">
  <label for="comments">Message *</label>
 </td>
 <td valign="top">
  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
 </td>
 
</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="reset" value="Clear form"><input type="submit" value="Send">  
 </td>
</tr>
</table>
</form>
<!---End contact form--->

Sent_contact.php


&lt;?php
if (isset($_POST['email']) && isset($_POST['emailto']) && !empty($_POST['emailto'])) {
    $sendto = trim($_POST['emailto']);
    
    // Ensure the email is valid
    if (!preg_match('/^[a-z0-9!#$%&\\'*+\\/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\\'*+\\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+(?:[a-z]{2}|aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel)$/i', $sendto)) {
        die('Invalid email address selected!');
    }
    
    if (preg_match('/;/', $sendto)) {
        $sendto = explode(';', $sendto);
        $sendto = $sendto[0] . ', ' . $sendto[1];
    } else {
        $sendto = $sendto;
    }
    $email_subject = 'CCWS';
  
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.&lt;br /&gt;&lt;br /&gt;";
        echo $error."&lt;br /&gt;&lt;br /&gt;";
        echo "Please go back and fix these errors.&lt;br /&gt;&lt;br /&gt;";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to an unknown error with the form submitted. Please call to 
		register for a course or try again later. Sorry for the inconvenience.');       
    }
     
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // required
    $comments = $_POST['comments']; // required
     
    $error_message = "";
    $email_exp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/";
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'Email address is &lt;u&gt;not&lt;/u&gt; valid.&lt;br /&gt;';
  }
    $string_exp = "";
  if(!preg_match("/^[A-z]+$/",$first_name)) {
    $error_message = 'The First Name you entered does not appear to be valid.&lt;br /&gt;';
  }
  if(!preg_match("/^[M-z]+$/",$last_name)) {
    $error_message = 'The Last Name you entered does not appear to be valid.&lt;br /&gt;';
  }
  if(!preg_match("/^[0-9]{10}/", $telephone)) {
	$error_message	= 'Please enter valid 10 digit phone number.&lt;br /&gt;';
  }	
  if(strlen($comments) &lt; 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.&lt;br /&gt;';
  }
  if(strlen($error_message) &gt; 0) {
    died($error_message);
  }
    $email_message = "Form details below.\
\
";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "First Name: ".clean_string($first_name)."\
";
    $email_message .= "Last Name: ".clean_string($last_name)."\
";
    $email_message .= "Email: ".clean_string($email_from)."\
";
    $email_message .= "Telephone: ".clean_string($telephone)."\
";
    $email_message .= "Comments: ".clean_string($comments)."\
";
     
     
// create email headers
$headers = 'From: '.$email_from."\\r\
".
'Reply-To: '.$email_from."\\r\
" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?&gt;
 
&lt;!-- include your own success html here --&gt;
 &lt;strong&gt;&lt;u&gt;Thank's &lt;?php echo $first_name ?&gt;!&lt;/u&gt;&lt;/strong&gt;&lt;br /&gt;
 We have successfully recieved your contact form. We will contact you as soon as we can.

&lt;?php
}
?&gt;

Sorry got caught up with projects at work, the 2 links below should have working sources.

http://pastebin.com/ghAXRMNU - HTML form
http://pastebin.com/xBtbR8qj - Form validation

NOTE: In the HTML form code make sure to replace the PHP variables for the emails with your code as i used them for testing purposes.

Thanks that seems to work fine except on the confirmation page it shows

Array
(
[emailto] => david@test.com;rob@test.com
[first_name] => Zach
[last_name] => dksjafkj
[email] => email@test.com
[telephone] => 2222222222
[comments] => fsdafs faf dsa
[email_form] => Send
)
Thank’s Zach!
We have successfully receieved your contact form request and we will contact you as soon as possible.

Sorry i posted the debug version i was using, simply remove

echo '<pre>';
print_r($_POST);
echo '</pre>';

and replace

//if (mail($sendto, $email_subject, $email_message, $headers)) {
    echo "Thank's <strong><u>{$first_name}</u></strong>!<br />\
";
    echo "We have successfully receieved your contact form request and we will contact you as soon as possible.\
";
/*} else {
    echo "An error has occurred while trying to send your contact form request, please try again!<br /><a href=\\"javascript:history.go(-1)\\">Go Back</a>\
";
}*/

with

if (mail($sendto, $email_subject, $email_message, $headers)) {
    echo "Thank's <strong><u>{$first_name}</u></strong>!<br />\
";
    echo "We have successfully receieved your contact form request and we will contact you as soon as possible.\
";
} else {
    echo "An error has occurred while trying to send your contact form request, please try again!<br /><a href=\\"javascript:history.go(-1)\\">Go Back</a>\
";
}

Never mind on the last post I found why it was and stopped it. I am stuck again on another code. I want to copy the same format for my registration page, this is the code I tried to make by copying the contact form and adding fields.

Registration form


<!---Start Registration form---->
<form name="registationform" method="POST" action="confirmation.php">
<table width="550px">
</tr>
<tr>
 <td valign="top">
  <label for="first_name">First Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>
 
<tr>
 <td valign="top"">
  <label for="last_name">Last Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="last_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>
 
</tr>
<tr>
 <td valign="top">
  <label for="telephone">10 Digit Phone Number *<br />e.g. 1235550123 </label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>
<tr>
	<td valign="top">
		<label for="course">Course you would like to take *</label>
	</td>
	<td valign="top">
	<select name="course">
		<option></option>
		<option value=" Beginner Pistol course on <?php echo $nextbeginner ?>"> Beginner Pistol - <?php echo $nextbeginner ?></option>
		<option value="Beginner Pistol course on <?php echo $nextbeginner2 ?>"> Beginner Pistol - <?php echo $nextbeginner2 ?></option>
		<option value="Concealed Carry course on <?php echo $nextconcealed ?>"> Concealed Carry - <?php echo $nextconcealed ?></option>
		<option value=" Concealed Carry course on <?php echo $nextconcealed2 ?>"> Concealed Carry - <?php echo $nextconcealed2 ?></option>
		<option value="Advanced Pistol course on <?php echo $nextadvanced ?>"> Advanced Pistol - <?php echo $nextadvanced ?></option>
		<option value="Advanced Pistol course on <?php echo $nextadvanced2 ?>"> Advanced Pistol - <?php echo $nextadvanced2 ?></option>
		<option value="Rifle or Shotgun instruction on <?php echo $nextrifle ?>"> Rifle or Shotgun - <?php echo $nextrifle ?></option>
	</select>
	</td>
</tr>	
<tr>
	<td valign="top">
		How did you hear about CCWS? **
	</td>
	<td valign="top">
	<select name="found">
		<option></option>
		<option value="web">Web</option>
		<option value="friend">Friend</option>
		<option value="brochure">Brochure</option>
		<option value="card">Card</option>
		<option value="other">Other</option>
	</select>
	</td>
</tr>	
	
<tr>
 <td valign="top">
  <label for="comments">Comments and how many people in your group *</label>
 </td>
 <td valign="top">
  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
 </td>
 
</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="reset" value="Clear form"><input type="submit" value="Submit">   
 </td>
</tr>
</table>
</form>
<!---End Registration form--->


<?php
 
/**
 * Allows a developer to set a custom error message
 * using a pre-built error structure
 *
 * @param string $error
 * @return void
 */
function message_die($error) {
        echo 'We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.<br /><br />';
        echo $error . '<br /><br />Please go back and fix these errors.<br /><br /><a href="javascript:history.go(-1)">Go Back</a>';
        exit;
}
 
/**
 * Removes bad string values from a $_POST value to
 * ensure email injection is avoided
 *
 * @param string $string
 * @return string function
 */
function clean_string($string) {
        $bad = array('content-type', 'bcc:', 'to:', 'cc:', 'href');
        return str_replace($bad, '', $string);
}
 
// Regular expressions
$emailRegex     = '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/';
$lastNameRegex  = '/^[M-z]+$/';
$firstNameRegex = '/^[A-z]+$/';
$courseRegex = '/^[A-z]+[-]([A-z0-9_]+)+$/';
$foundRegex = '/^[A-z]+$/';
$telephoneRegex = '/^[0-9]{10}/';
 
if (isset($_POST['email_form'])) {
        // Required fields
        $requiredFields = array(
                'emailto'    => array('length' => 1, 'regex' => false, 'error' => 'Please select a valid email address!'),
                'first_name' => array('length' => 1, 'regex' => $firstNameRegex, 'error' => 'Please enter a valid first name!'),
                'last_name'  => array('length' => 1, 'regex' => $lastNameRegex, 'error' => 'Please enter a valid last name!'),
				'course' 	 => array('length' => 1, 'regex' => $coursRegex, 'error' => 'Please select a course and date!'),
				'found'		 => array('length' => 1, 'regex' => $foundRegex, 'error' => 'Please let us know how you found us!'),
                'email'      => array('length' => 1, 'regex' => $emailRegex, 'error' => 'Please enter a valid email address!'),
                'telephone'  => array('length' => 1, 'regex' => $telephoneRegex, 'error' => 'Please enter a valid 10 digit telephone number!'),
                'comments'   => array('length' => 2, 'regex' => false, 'error' => 'Please enter a comment thats at least 2 characters long!')
        );
 
        foreach($requiredFields as $k => $v) {
                if ((isset($_POST[$k]) && strlen($_POST[$k]) < $v['length'] && $v['regex'] == false) ||
                        (isset($_POST[$k]) && (strlen($_POST[$k]) < $v['length'] || ($v['regex'] != false && !preg_match($v['regex'], $_POST[$k]))))) {
                        message_die($v['error']);
                } else {
                        // Clean up the string of any unwanted values
                        $_POST[$k] = clean_string(trim($_POST[$k]));
                }
        }
 
 
    $sendto = "test@yahoo.com";
       
    $email_subject = "CCWS";
     
    $first_name = $_POST['first_name'];
    $last_name  = $_POST['last_name'];
    $email_from = $_POST['email'];
    $telephone  = $_POST['telephone'];
	$course     = $_POST['course'];
	$found      = $_POST['found'];
    $comments   = $_POST['comments'];
       
    $email_message  = "Form details below.\
\
";
    $email_message .= "First Name: " . $first_name . "\
";
    $email_message .= "Last Name: " . $last_name . "\
";
    $email_message .= "Email: " . $email_from . "\
";
    $email_message .= "Telephone: " . $telephone . "\
";
	$email_message .= "Course: " . $course . "\
";
	$email_message .= "Found us: " . $found . "\
";
    $email_message .= "Comments: " . $comments . "\
";
       
        $headers  = "From: " . $email_from . "\\r\
";
        $headers .= "Reply-To: " . $email_from . "\\r\
";
        $headers .= "X-Mailer: PHP/" . phpversion() . "\\r\
";
       
        //if (mail($sendto, $email_subject, $email_message, $headers)) {
                echo "Thank's <strong><u>{$first_name}</u></strong>!<br />\
";
                echo "We have successfully receieved your registration form and we will contact you as soon as possible.\
";
        /*} else {
                echo "An error has occurred while trying to send your contact form request, please try again!<br /><a href=\\"javascript:history.go(-1)\\">Go Back</a>\
";
        }*/
} else {
        message_die('Hack attempt!');
}
 
?>

I am getting the error Hack attempt!

Simply change the submit button to the code below

<input type="submit" value="Submit" name="email_form">

Thanks again for your help. I am slowly picking this up. But know it says

“Notice: Undefined index: emailto in C:\wamp\www\CCWSphp_site\confirmation.php on line 53
Thank’s Zach!
We have successfully receieved your registration form and we will contact you as soon as possible.”


<?php
 
/**
 * Allows a developer to set a custom error message
 * using a pre-built error structure
 *
 * @param string $error
 * @return void
 */
function message_die($error) {
        echo 'We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.<br /><br />';
        echo $error . '<br /><br />Please go back and fix these errors.<br /><br /><a href="javascript:history.go(-1)">Go Back</a>';
        exit;
}
 
/**
 * Removes bad string values from a $_POST value to
 * ensure email injection is avoided
 *
 * @param string $string
 * @return string function
 */
function clean_string($string) {
        $bad = array('content-type', 'bcc:', 'to:', 'cc:', 'href');
        return str_replace($bad, '', $string);
}
 
// Regular expressions
$emailRegex     = '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/';
$lastNameRegex  = '/^[M-z]+$/';
$firstNameRegex = '/^[A-z]+$/';
$foundRegex = '/^[A-z]+$/';
$telephoneRegex = '/^[0-9]{10}/';
 
if (isset($_POST['email_form'])) {
        // Required fields
        $requiredFields = array(
                'emailto'    => array('length' => 1, 'regex' => false, 'error' => 'Please select a valid email address!'),
                'first_name' => array('length' => 1, 'regex' => $firstNameRegex, 'error' => 'Please enter a valid first name!'),
                'last_name'  => array('length' => 1, 'regex' => $lastNameRegex, 'error' => 'Please enter a valid last name!'),
				'found'		 => array('length' => 1, 'regex' => $foundRegex, 'error' => 'Please let us know how you found us!'),
                'email'      => array('length' => 1, 'regex' => $emailRegex, 'error' => 'Please enter a valid email address!'),
                'telephone'  => array('length' => 1, 'regex' => $telephoneRegex, 'error' => 'Please enter a valid 10 digit telephone number!'),
                'comments'   => array('length' => 2, 'regex' => false, 'error' => 'Please enter a comment thats at least 2 characters long!')
        );
 
        foreach($requiredFields as $k => $v) {
                if ((isset($_POST[$k]) && strlen($_POST[$k]) < $v['length'] && $v['regex'] == false) ||
                        (isset($_POST[$k]) && (strlen($_POST[$k]) < $v['length'] || ($v['regex'] != false && !preg_match($v['regex'], $_POST[$k]))))) {
                        message_die($v['error']);
                } else {
                        // Clean up the string of any unwanted values
                        $_POST[$k] = clean_string(trim($_POST[$k])); //Line 53
                }
        }
 
 
    $sendto = "test@yahoo.com";
       
    $email_subject = "CCWS";
     
    $first_name = $_POST['first_name'];
    $last_name  = $_POST['last_name'];
    $email_from = $_POST['email'];
    $telephone  = $_POST['telephone'];
	$course     = $_POST['course'];
	$found      = $_POST['found'];
    $comments   = $_POST['comments'];
       
    $email_message  = "Form details below.\
\
";
    $email_message .= "First Name: " . $first_name . "\
";
    $email_message .= "Last Name: " . $last_name . "\
";
    $email_message .= "Email: " . $email_from . "\
";
    $email_message .= "Telephone: " . $telephone . "\
";
	$email_message .= "Course: " . $course . "\
";
	$email_message .= "Found us: " . $found . "\
";
    $email_message .= "Comments: " . $comments . "\
";
       
        $headers  = "From: " . $email_from . "\\r\
";
        $headers .= "Reply-To: " . $email_from . "\\r\
";
        $headers .= "X-Mailer: PHP/" . phpversion() . "\\r\
";
       
        //if (mail($sendto, $email_subject, $email_message, $headers)) {
                echo "Thank's <strong><u>{$first_name}</u></strong>!<br />\
";
                echo "We have successfully receieved your registration form and we will contact you as soon as possible.\
";
        /*} else {
                echo "An error has occurred while trying to send your contact form request, please try again!<br /><a href=\\"javascript:history.go(-1)\\">Go Back</a>\
";
        }*/
} else {
        message_die('Hack attempt!');
}
 
?>

Sorry for asking so many questions.

In your new form you don’t have a field called “emailto” which is why the error is occurring. Simply remove this line from the array

'emailto'    => array('length' => 1, 'regex' => false, 'error' => 'Please select a valid email address!'),

Also make sure you un-comment the if statement for confirmation

if (mail($sendto, $email_subject, $email_message, $headers)) {
        echo "Thank's <strong><u>{$first_name}</u></strong>!<br />\
";
        echo "We have successfully receieved your registration form and we will contact you as soon as possible.\
";
} else {
        echo "An error has occurred while trying to send your contact form request, please try again!<br /><a href=\\"javascript:history.go(-1)\\">Go Back</a>\
";
}

Can I just point out that having email addresses in the form itself is a rather large security issue. Someone could (and probably will) view the source, edit it, save it and use your page to start sending email to anyone they want.

What is the better way to do this then? I am very new to this and would like to do it as safely as possible.

An easy and safe solution would be to use number identifiers in the drop down box then either using an IF or switch statement check what type of identifier has been passed and set the emails to be used. If you need an example or if it doesn’t make sense let me know.

So the value’s of the drop down would be like 1,2 and 3. 3 being both email addresses. But I don’t know how I would switch the value in the confirmation page.

If I haven’t bugged you enough do you mind showing me using the following code?

<?php
 
/**
 * Allows a developer to set a custom error message
 * using a pre-built error structure
 *
 * @param string $error
 * @return void
 */
function message_die($error) {
        echo 'We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.<br /><br />';
        echo $error . '<br /><br />Please go back and fix these errors.<br /><br /><a href="javascript:history.go(-1)">Go Back</a>';
        exit;
}
 
/**
 * Removes bad string values from a $_POST value to
 * ensure email injection is avoided
 *
 * @param string $string
 * @return string function
 */
function clean_string($string) {
        $bad = array('content-type', 'bcc:', 'to:', 'cc:', 'href');
        return str_replace($bad, '', $string);
}
 
// Regular expressions
$emailRegex     = '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/';
$lastNameRegex  = '/^[M-z]+$/';
$firstNameRegex = '/^[A-z]+$/';
$telephoneRegex = '/^[0-9]{10}/';
 
if (isset($_POST['email_form'])) {
        // Required fields
        $requiredFields = array(
                'emailto'    => array('length' => 1, 'regex' => false, 'error' => 'Please select a valid email address!'),
                'first_name' => array('length' => 1, 'regex' => $firstNameRegex, 'error' => 'Please enter a valid first name!'),
                'last_name'  => array('length' => 1, 'regex' => $lastNameRegex, 'error' => 'Please enter a valid last name!'),
                'email'      => array('length' => 1, 'regex' => $emailRegex, 'error' => 'Please enter a valid email address!'),
                'telephone'  => array('length' => 1, 'regex' => $telephoneRegex, 'error' => 'Please enter a valid 10 digit telephone number!'),
                'comments'   => array('length' => 2, 'regex' => false, 'error' => 'Please enter a comment thats at least 2 characters long!')
        );
 
        foreach($requiredFields as $k => $v) {
                if ((isset($_POST[$k]) && strlen($_POST[$k]) < $v['length'] && $v['regex'] == false) ||
                        (isset($_POST[$k]) && (strlen($_POST[$k]) < $v['length'] || ($v['regex'] != false && !preg_match($v['regex'], $_POST[$k]))))) {
                        message_die($v['error']);
                } else {
                        // Clean up the string of any unwanted values
                        $_POST[$k] = clean_string(trim($_POST[$k]));
                }
        } 
 
    $sendto = $_POST['emailto'];
   
        if (preg_match("/;/", $sendto)) {
        $sendto = explode(";", $sendto);
        $sendto = $sendto[0] . ", " . $sendto[1];
    } else {
        $sendto = $sendto;
    }
       
    $email_subject = "CCWS";
     
    $first_name = $_POST['first_name'];
    $last_name  = $_POST['last_name'];
    $email_from = $_POST['email'];
    $telephone  = $_POST['telephone'];
    $comments   = $_POST['comments'];
       
    $email_message  = "Form details below.\
\
";
    $email_message .= "First Name: " . $first_name . "\
";
    $email_message .= "Last Name: " . $last_name . "\
";
    $email_message .= "Email: " . $email_from . "\
";
    $email_message .= "Telephone: " . $telephone . "\
";
    $email_message .= "Comments: " . $comments . "\
";
       
        $headers  = "From: " . $email_from . "\\r\
";
        $headers .= "Reply-To: " . $email_from . "\\r\
";
        $headers .= "X-Mailer: PHP/" . phpversion() . "\\r\
";
       
        //if (mail($sendto, $email_subject, $email_message, $headers)) {
                echo "Thank's <strong><u>{$first_name}</u></strong>!<br />\
";
                echo "We have successfully receieved your contact form at and we will contact you as soon as possible.\
";
        /*} else {
                echo "An error has occurred while trying to send your contact form request, please try again!<br /><a href=\\"javascript:history.go(-1)\\">Go Back</a>\
";
        }*/
} else {
        message_die('Hack attempt!');
}
 
?>

Yep your spot on with that bud, below is the updated code with the switch. All you need to do is update the email addresses.

<?php

/**
 * Allows a developer to set a custom error message
 * using a pre-built error structure
 *
 * @param string $error
 * @return void
 */
function message_die($error) {
    echo 'We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.<br /><br />';
    echo $error . '<br /><br />Please go back and fix these errors.<br /><br /><a href="javascript:history.go(-1)">Go Back</a>';
    exit;
}

/**
 * Removes bad string values from a $_POST value to
 * ensure email injection is avoided
 *
 * @param string $string
 * @return string function
 */
function clean_string($string) {
    $bad = array('content-type', 'bcc:', 'to:', 'cc:', 'href');
    return str_replace($bad, '', $string);
}

// Regular expressions
$emailRegex     = '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/';
$lastNameRegex  = '/^[M-z]+$/';
$firstNameRegex = '/^[A-z]+$/';
$telephoneRegex = '/^[0-9]{10}/';

if (isset($_POST['email_form'])) {
    // Required fields
    $requiredFields = array(
            'emailto'    => array('length' => 1, 'regex' => false, 'error' => 'Please select a valid email address!'),
            'first_name' => array('length' => 1, 'regex' => $firstNameRegex, 'error' => 'Please enter a valid first name!'),
            'last_name'  => array('length' => 1, 'regex' => $lastNameRegex, 'error' => 'Please enter a valid last name!'),
            'email'      => array('length' => 1, 'regex' => $emailRegex, 'error' => 'Please enter a valid email address!'),
            'telephone'  => array('length' => 1, 'regex' => $telephoneRegex, 'error' => 'Please enter a valid 10 digit telephone number!'),
            'comments'   => array('length' => 2, 'regex' => false, 'error' => 'Please enter a comment thats at least 2 characters long!')
    );

    foreach($requiredFields as $k => $v) {
        if ((isset($_POST[$k]) && strlen($_POST[$k]) < $v['length'] && $v['regex'] == false) ||
            (isset($_POST[$k]) && (strlen($_POST[$k]) < $v['length'] || ($v['regex'] != false && !preg_match($v['regex'], $_POST[$k]))))) {
            message_die($v['error']);
        } else {
            // Clean up the string of any unwanted values
            $_POST[$k] = clean_string(trim($_POST[$k]));
        }
    }
    
    switch($_POST['emailto']) {
        case 1:
            $sendto = 'emaila@test.com';
        break;
        case 2:
            $sendto = 'emailb@test.com';
        break;
        case 3:
            $sendto = 'emaila@test.com, emailb@test.com';
        break;
        default:
            message_die('The person you elected to send the email to doesn\\'t exist, please select a valid person to send the email to!');
    }
    
    $email_subject = "CCWS";
    
    $first_name = $_POST['first_name'];
    $last_name  = $_POST['last_name'];
    $email_from = $_POST['email'];
    $telephone  = $_POST['telephone'];
    $comments   = $_POST['comments'];
    
    $email_message  = "Form details below.\
\
";
    $email_message .= "First Name: " . $first_name . "\
";
    $email_message .= "Last Name: " . $last_name . "\
";
    $email_message .= "Email: " . $email_from . "\
";
    $email_message .= "Telephone: " . $telephone . "\
";
    $email_message .= "Comments: " . $comments . "\
";
    
    $headers  = "From: " . $email_from . "\\r\
";
    $headers .= "Reply-To: " . $email_from . "\\r\
";
    $headers .= "X-Mailer: PHP/" . phpversion() . "\\r\
";
    
    if (mail($sendto, $email_subject, $email_message, $headers)) {
        echo "Thank's <strong><u>{$first_name}</u></strong>!<br />\
";
        echo "We have successfully receieved your contact form at and we will contact you as soon as possible.\
";
    } else {
        echo "An error has occurred while trying to send your contact form request, please try again!<br /><a href=\\"javascript:history.go(-1)\\">Go Back</a>\
";
    }
} else {
    message_die('Hack attempt!');
}

?>

Thanks! I really appreciate it. The site I am putting together for a friend just went online like 12 hours ago. I think i got myself in over my head. Now when I go and test the contact form and registration form it says that every thing worked fine and that the email was sent. But he does not receive any emails. I think it is because of the php.ini file. He does not have a email account at his domain name yet. And is wanting the emails to be sent from a Gmail account he has set up. Again I have no clue how to go about editing the php.ini file. if you don’t mind will you show me an example?