Link to home
Start Free TrialLog in
Avatar of JakeSpencer
JakeSpencer

asked on

Form to different email depending on Postcode

Hello

I have a form on a website which when filled out sends an email.  What I would like to do is change it so that the form is sent to a different email address depending on the inputted postcode.  Is this something easily achievable, and where would I start?

The form code is below:

<form action="confirmation.php" method="post" name="NewsletterSignup.php">
  <span id="sprytextfield1">
  <label for="name">Full Name</label>
  *<br />
<input name="name" type="text" id="name" size="30" />
  <span class="textfieldRequiredMsg">A value is required.</span></span>
  <br /><br />
  <span id="sprytextfield2">
  <label for="email">Email Address*<br />
  </label>
  
  <input name="email" type="text" id="email" size="30" />
  <span class="textfieldRequiredMsg">A value is required.</span></span>
   <br /><br />
    <span id="sprytextfield3">
  <label for="address">Address*<br />
  </label>
  <textarea name="address" cols="30" rows="3" id="address"></textarea>
  <span class="textfieldRequiredMsg">A value is required.</span></span>
  <br />
  <br />
      <span id="sprytextfield4">
    <label for="postcode">Postcode*<br />
    </label>
  <input name="postcode" type="text" id="postcode" size="30" />
  <span class="textfieldRequiredMsg">A value is required.</span></span>
      <p><br />
        <br />
        <label>Telephone Number</label>
   <br />
        <input name="telephone" type="text" size="30" />
        <br /><br />
        <label>Type of Business<br />
        </label>
        <select name="business">
          <option value="Mobile">Mobile</option>
          <option value="Salon">Salon</option>
          <option value="Home">Home</option>
          <option value="Other">Other</option>
        </select>
        <br />
        <br />
        <label>Number of tans done per week </label>
        <br />
        <input name="tansperweek" type="text" size="30" />
<br /><br />
<p>Please choose below if you would like to be contacted about our offers and events. We will never use your address for anything but information about our products.</p>
<br />
  
  <select name="offers">
    <option value="YES, please add me to your list." selected="selected">YES, please add me to your list.</option>
    <option value="NO thanks, please do not add me to your list.">NO thanks, please do not add me to your list.</option>
  </select>
  <br /><br />
  
  
  <input name="signup" type="submit" value="Sign Up Now" />
</form>
 

Open in new window


And the action it calls to send the email:

<?php

$my_email = "mail@mydomain.com";

/*

Enter the continue link to offer the user after the form is sent.  If you do not change this, your visitor will be given a continue link to your homepage.

If you do change it, remove the "/" symbol below and replace with the name of the page to link to, eg: "mypage.htm" or "http://www.elsewhere.com/page.htm"

*/



/*

Step 3:

Save this file (FormToEmail.php) and upload it together with your webpage containing the form to your webspace.  IMPORTANT - The file name is case sensitive!  You must save it exactly as it is named above!  Do not put this script in your cgi-bin directory (folder) it may not work from there.

THAT'S IT, FINISHED!

You do not need to make any changes below this line.

*/

$errors = array();

// Remove $_COOKIE elements from $_REQUEST.

if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

// Check all fields for an email header.

function recursive_array_check_header($element_value)
{

global $set;

if(!is_array($element_value)){if(preg_match("/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i",$element_value)){$set = 1;}}
else
{

foreach($element_value as $value){if($set){break;} recursive_array_check_header($value);}

}

}

recursive_array_check_header($_REQUEST);

if($set){$errors[] = "You cannot send an email header";}

unset($set);

// Validate email field.

if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
{

if(preg_match("/(%0A|%0D|\n+|\r+|:)/i",$_REQUEST['email'])){$errors[] = "Email address may not contain a new line or a colon";}

$_REQUEST['email'] = trim($_REQUEST['email']);

if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}

}

// Check referrer is from same site.

if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}

// Check for a blank form.

function recursive_array_check_blank($element_value)
{

global $set;

if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
else
{

foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}

}

}

recursive_array_check_blank($_REQUEST);

if(!$set){$errors[] = "You cannot send a blank form";}

unset($set);

// Display any errors and exit if errors exist.

if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}

if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");}

// Build message.

function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}

$message = build_message($_REQUEST);

$message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."";

$message = stripslashes($message);

$subject = "Newsletter Signup Form";

$headers = "From: " . $_REQUEST['email'];

mail($my_email,$subject,$message,$headers);

?>

<!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" />

        <?php include('_inc/global.php'); ?>
        
<link href="css/style2.css" rel="stylesheet" type="text/css" />

	<script type="text/javascript" src="js/jquery.js"></script>
	<script type="text/javascript" src="js/jquery-easing-1.3.pack.js"></script>
	<script type="text/javascript" src="js/jquery-easing-compatibility.1.2.pack.js"></script>
	<script type="text/javascript" src="js/coda-slider.1.1.1.pack.js"></script>
	<script type="text/javascript" src="js/ix-slider.js"></script>

</head>

<body>

<div id="outer-wrapper">

	<div id="inner-wrapper">    <div id="header">
    <div id="headerL"><h1 id="logo"></h1></div>    <div id="headerM"></div>
    <div id="headerR"><h2 id="Blogo"></h2></div>
    </div>
        <div id="top-menu"><?php include('_inc/menu.php'); ?></div>
        <div id="content-wrapper">
        <div id="side-bar">
        
		<?php include('_inc/sidepanel-child.php'); ?>

        <?php include('_inc/sidepanel2.php'); ?>


        </div>
        <div id="content"> 
          <h1>Thank you<br />
          </h1>
          <p><br />
          Thank you for your enquiry. We will get back to you very shortly.<br />
  <br />
  <br />
          </p>
          <p>&nbsp;</p>
          <p><img src="images/layout/mail.jpg" alt="Thank you mail" width="306" height="285" class="mail" /></p>
          <h1>&nbsp;</h1>
          <p><br />
            <br />
          </p>
        </div>
        <div id="content-end-spacer"></div>
        <div id="clear"></div>
        <div id="box-wrapper">
         <?php include('_inc/4b3.php'); ?>
        </div>        <div id="quick-wrapper">
          
        </div>
        <div id="qclear"></div>        
        <div id="footer-wrapper">
        <?php include('_inc/footer_nav.php'); ?>
        </div>
		</div>

	</div>
</div>
</body>
</html>

Open in new window


Many Thanks for any assistance.
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

Well I guess you would start at the top of the processing form. You will need to check which postcode was submitted, then change the $my_email value depending on which postcode was submitted.

first, work out which postcodes will be sent to which email address. Perhaps create an array to store the postcodes for each email address you wish to send to then do a simple "if()" statement to workout which email address to use.

if($_REQUEST['postcode'] /////blah blah then set the email adresss
{
$my_email = "email1@yourdomain.com";
}else{
$my_email = "email2@yourdomain.com";
}

Then your script will send to the right email address. So create several arrays to hold the different groups of postcodes, then you can search each array for the posted (submitted) postcode and use that in your if statement.

Hope this helps
Avatar of DoCBReeD
DoCBReeD

Are you looking to do a small handful of postcodes? if so use the following, or are you looking to do a greater list that needs a database involved?

if(isset($_POST['postcode'])){
    switch($_POST['postcode']){
        case '54313' : $my_email = "54313@mydomain.com"; break;
        case '54312' : $my_email = "54312@mydomain.com"; break;
        case '54311' : $my_email = "54311@mydomain.com"; break;
        default : $my_email = "mail@mydomain.com"; break;
    }
}else{
    $my_email = "mail@mydomain.com";
}

Open in new window

At the very beginning of php file replace
$my_email = "mail@mydomain.com";

with following code
$postcode = $_POST['postcode'];
if( $postcode=='ABC' ){
  $my_email = "mail@mydomain.com";
}
elseif( $postcode=='DEF' ){
  $my_email = "mail@otherdomain.com";
}
elseif( .... // code other possibilities

Open in new window

Avatar of JakeSpencer

ASKER

Thanks for the help guys.  I've just been informed that we only split the leads by the first 2 letters of the postcode.  

So if postcode is WR51BG it needs to only use the first 2 characters (WR) and ignore the rest.  If the postcode is B60 1TY it only needs to use the B and strip out the rest.

Does this make it much harder?

Thanks again
Does this make it much harder? <- Can you differentiate between different post codes using only 1 letter? If so then most of our examples above will work.
Because we use postcode zones to assign emails, the 1st 2 (or) 1 letters of the postcode will suffice.

Is there a way to tell the script to drop everything after the 1st 2 characters (or 1 if the 2nd character is a number)?

Many Thanks, I really appreciate the help.
If these are UK postcodes then you split them up using a regular expression

<?php

     $pattern = '#^([a-z]+)[0-9]+[a-z]?\s*[0-9]{1}[a-z]{2}$#i';

     preg_match( $pattern, "B60 1ty", $match );
     echo $match [1] . "<br/>";

     preg_match( $pattern, "WR51BG", $match );
     echo $match [1] . "<br/>";

Open in new window

If you want only the first character of the postcode, you can use substr($thing, 0, 1); and for the first two characters you can use substr($thing, 0, 2);

To know if the second character is a number, you can do something like this (string positions are numbered from zero):

$second = substr($thing,1,1);
if (preg_match('/[0-9]/', $second)) { /* THE SECOND POSITION IS A NUMBER */ }

See:
http://us.php.net/manual/en/function.substr.php
http://us.php.net/manual/en/function.preg-match.php
ASKER CERTIFIED SOLUTION
Avatar of DoCBReeD
DoCBReeD

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THAT IS BRILLIANT!!!!!  Many Thanks