Jump to content

Change email address using dropdown box.


Ne0_Dev

Recommended Posts

Hi guys,

 

I am after a bit of help on the best way to change the email address the form contents is sent to by use of a dropdown box (select list).

The scenario is that I have a contact form that has a select list which lists uk counties.  I need the form to send it's contents to a different email address depending on which county is selected.

 

I have had a go at it however my current script is giving error "Warning: mail() [function.mail]: SMTP server response: 554 Error: no valid recipients"

 

Here's the code:

<?php
include('includes/corefuncs.inc.php');
if (function_exists('nukeMagicQuotes')) {
  nukeMagicQuotes();
  }
//process the email
if(array_key_exists('send', $_POST)) {
$to = $recipient;
$subject = 'Message from website';

//list expected fields
$expected = array  ('name', 'email', 'phone', 'company', 'county', 'message');

//set required fields
$required = array('name', 'email', 'county', 'message');

//create empty array for any missing fields
$missing = array();

//assume nothing is suspect
$suspect = false;
//create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';

//function to check for suspect phrases
function isSuspect($val, $pattern, &$suspect) {
//if the variable is an array, loop through each element
//and pass it recursively back to the same function
if (is_array($val)) {
foreach ($val as $item) {
isSuspect($item, $pattern, $suspect);
}
}
else {
//if one of the suspect phrases is found, set Boolean to true
if (preg_match($pattern, $val)) {
$suspect = true;
}
}
}
//check the $_POST array and any subarrays for suspect content
isSuspect($_POST, $pattern, $suspect);

if ($suspect) {
$mailSent = false;
unset($missing);
}
else {
//process the $_POST variables
foreach ($_POST as $key => $value) {
//assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
//if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
//otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
}

// validate the email address
  if (!empty($email)) {
    // regex to ensure no illegal characters in email address 
$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
// reject the email address if it doesn't match
if (!preg_match($checkEmail, $email)) {
  array_push($missing, 'email');
  }
}

//go ahead only if not suspect and all required fields ok
if (!$suspect && empty($missing)) {

//build the message
$message = "Name: $name\n\n";
$message .= "Company: $company\n\n";
$message .= "Telephone: $phone\n\n";
$message .= "Email: $email\n\n";
$message .= "County: $recipient\n\n";
$message .= "Message: $message";

//limit line length to 70 characters
$message = wordwrap($message, 70);

//create additional headers
$additionalHeaders = "From: \r\n";
if (!empty($email)) {
$additionalHeaders .= "\r\nReply-To: $email";
}

//send it
$mailSent = mail($to, $subject, $message, $additionalHeaders);
if ($mailSent) {
//$missing is no longer needed if the email is sent, so unset it
unset($missing);
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<link href="styles/contact.css" rel="stylesheet" type="text/css" />
<!--[if IE]>
<style type="text/css"> 
/* place css fixes for all versions of IE in this conditional comment */
.twoColFixLtHdr #mainContent { zoom: 1; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]-->
</head>

<body class="twoColFixLtHdr">

<div id="container">
  
  
   <div id="nav">
   
     <table width="892" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="244">
    <?php include("includes/menu.inc.php"); ?>
    </td>
    <td><img src="images/flashPH.jpg" width="648" height="132" /></td>
  </tr>
</table>

  </div>
  <div id="mainContent">
    <h1>Contact Us</h1>
    <form action="" method="post" name="contact">
    <p>
    <label for="name">Name:</label>
    <input name="name" type="text" class="formbox" />
    </p>
    <p>
    <label for="company">Company: </label>
    <input name="company" type="text" class="formbox" />
    </p>
    <p>
    <label for="phone">Telephone: </label>
    <input name="phone" type="text" class="formbox" />
    </p>
    <p>
    <label for="email">Email: </label>
    <input name="email" type="text" class="formbox" />
    </p>
<p>
<label for="recipient">County: </label>
<select name="recipient">
<option selected="selected">Please Select...</option>
<option value="berkshire">Berkshire</option>
<option value="hampshire">Hampshire</option>
<option value="yorkshire">Yorkshire</option>
</select>

<?php
$email1 = "[email protected]";
$email2 = "[email protected]";
$email3 = "[email protected]";
$email4 = "[email protected]";

switch ($_POST['recipient']) {
case "berkshire":
	$recipient = $email1;
break;
case "hampshire":
	$recipient = $email2;
break;
case "yorkshire":
	$recipient = $email3;
break;
default:
	$recipient = "[email protected]";
}
?>
</p>
    <p>
    <label for="message">Message: </label>
    <textarea name="message" cols="50" rows="10"></textarea>
    </p>
    <p>
    <input name="send" id="send" type="submit" value="Send Message"/>
    </p>
    </form>
  </div>
<!-- end #mainContent -->
<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --><br class="clearfloat" />
  <?php include("includes/footer.inc.php"); ?>
<!-- end #container --></div>
</body>
</html>

 

Any help or suggestions are most appreciated.

Link to comment
https://forums.phpfreaks.com/topic/115038-change-email-address-using-dropdown-box/
Share on other sites

you were so close...code was just a little out of order:

 

<?php
include('includes/corefuncs.inc.php');
if (function_exists('nukeMagicQuotes')) {
  nukeMagicQuotes();
  }
//process the email
if(array_key_exists('send', $_POST)) {
  switch ($_POST['county']) {
    case "berkshire": $to = "[email protected]"; break;
    case "hampshire": $to = "[email protected]"; break;
    case "yorkshire": $to = "[email protected]"; break;
    default:          $to = "[email protected]";
  }
  $subject = 'Message from website';

//list expected fields
$expected = array  ('name', 'email', 'phone', 'company', 'county', 'message');

//set required fields
$required = array('name', 'email', 'county', 'message');

//create empty array for any missing fields
$missing = array();

//assume nothing is suspect
$suspect = false;
//create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';

//function to check for suspect phrases
function isSuspect($val, $pattern, &$suspect) {
//if the variable is an array, loop through each element
//and pass it recursively back to the same function
if (is_array($val)) {
  foreach ($val as $item) {
  isSuspect($item, $pattern, $suspect);
  }
}
else {
//if one of the suspect phrases is found, set Boolean to true
if (preg_match($pattern, $val)) {
  $suspect = true;
  }
}
}
//check the $_POST array and any subarrays for suspect content
isSuspect($_POST, $pattern, $suspect);

if ($suspect) {
  $mailSent = false;
  unset($missing);
  }
else {
//process the $_POST variables
foreach ($_POST as $key => $value) {
//assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
//if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
  array_push($missing, $key);
  }
//otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
  ${$key} = $temp;
  }
}
}

// validate the email address
  if (!empty($email)) {
    // regex to ensure no illegal characters in email address 
  $checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
  // reject the email address if it doesn't match
  if (!preg_match($checkEmail, $email)) {
    array_push($missing, 'email');
    }
  }
  
//go ahead only if not suspect and all required fields ok
if (!$suspect && empty($missing)) {
  
//build the message
$message = "Name: $name\n\n";
$message .= "Company: $company\n\n";
$message .= "Telephone: $phone\n\n";
$message .= "Email: $email\n\n";
$message .= "County: $recipient\n\n";
$message .= "Message: $message";

//limit line length to 70 characters
$message = wordwrap($message, 70);

//create additional headers
$additionalHeaders = "From: \r\n";
if (!empty($email)) {
  $additionalHeaders .= "\r\nReply-To: $email";
  }

//send it
$mailSent = mail($to, $subject, $message, $additionalHeaders);
if ($mailSent) {
//$missing is no longer needed if the email is sent, so unset it
unset($missing);
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<link href="styles/contact.css" rel="stylesheet" type="text/css" />
<!--[if IE]>
<style type="text/css"> 
/* place css fixes for all versions of IE in this conditional comment */
.twoColFixLtHdr #mainContent { zoom: 1; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]-->
</head>

<body class="twoColFixLtHdr">

<div id="container">
  
  
   <div id="nav">
   
     <table width="892" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="244">
    <?php include("includes/menu.inc.php"); ?>
    </td>
    <td><img src="images/flashPH.jpg" width="648" height="132" /></td>
  </tr>
</table>

  </div>
  <div id="mainContent">
    <h1>Contact Us</h1>
    <form action="" method="post" name="contact">
    <p>
    <label for="name">Name:</label>
    <input name="name" type="text" class="formbox" />
    </p>
    <p>
    <label for="company">Company: </label>
    <input name="company" type="text" class="formbox" />
    </p>
    <p>
    <label for="phone">Telephone: </label>
    <input name="phone" type="text" class="formbox" />
    </p>
    <p>
    <label for="email">Email: </label>
    <input name="email" type="text" class="formbox" />
    </p>
  <p>
  <label for="recipient">County: </label>
  <select name="recipient">
  <option selected="selected">Please Select...</option>
  <option value="berkshire">Berkshire</option>
  <option value="hampshire">Hampshire</option>
  <option value="yorkshire">Yorkshire</option>
  </select>
  </p>
    <p>
    <label for="message">Message: </label>
    <textarea name="message" cols="50" rows="10"></textarea>
    </p>
    <p>
    <input name="send" id="send" type="submit" value="Send Message"/>
    </p>
    </form>
  </div>
<!-- end #mainContent -->
  <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --><br class="clearfloat" />
  <?php include("includes/footer.inc.php"); ?>
<!-- end #container --></div>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.