Jon Harding
Jon Harding

Reputation: 4946

Send email to unique person based on drop-down selection

I have a form that needs to go to different owners depending on the location that the user selects in the contact form. I'm using a template and have been able to edit the fields to have them be the values I want, but having difficulty trying to figure out how to get it to the correct person.

<?php

if(!$_POST) exit;  

$email = $_POST['email'];

//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]  {2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('location','name','email','phone','message');
$required = array('location','name','email','phone','message');

$your_email = "[email protected]";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
  if(in_array($value,$required)){
    if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
    $email_content .= $value.': '.$_POST[$value]."\n";
  }
}

if(

if(@mail($your_email,$email_subject,$email_content)) {
    echo 'Message sent!'; 
} else {
    echo 'ERROR!';
}
   }
?>

I would like to change who the $your_email is pointed to based on the Location drop down menu. And always CC one specific person as well.

The form is at: http://amazinggaragefloors-net.si-sv3641.com/contactus.html

Upvotes: 0

Views: 2207

Answers (2)

Uncle Iroh
Uncle Iroh

Reputation: 386

I took the reply from ArendE and applied his wisdom to a form I just created.

in /contactengine.php I plugged in this code (replace line #4):

$Email_Query = $_POST['Query']; // the value from your form

if($Email_Query == "Publishing") { // a form value and a location
    $EmailTo = "[email protected]"; // the email for that location
} else if ($Email_Query == "Custom Programs") {
    $EmailTo = "[email protected]";
} else if ($Email_Query == "Events") {
    $EmailTo = "[email protected]";
} else if ($Email_Query == "Subscriptions") {
    $EmailTo = "[email protected]";
} else { // if no one is good, send it to this email
    $EmailTo = "[email protected]";
}

Again, I added this code to the same file above (after line 5)

$Query = Trim(stripslashes($_POST['Query'])); 

Also, I added this code to the same file above (after line 19)

$Body .= "Query: ";
$Body .= $Query;

I added a drop-down list to my form:

<select id="Query" name="Query">
    <option value="Publishing" name="Query1">Publishing</option>
    <option value="Custom Programs" name="Query2">Custom Programs</option>
    <option value="Events" name="Query3">Events</option>
    <option value="Subscriptions" name="Query4">Subscriptions</option>
</select>

Marvelous! Good job, your setup and ready to rock and roll...

Thanks for your attention,

p.s. I'm don't believe a "CC" email is delivered. During my form tests, I never received one, however, I'm happy with the closing else statement.

Upvotes: 0

ArendE
ArendE

Reputation: 997

$email_location = $_POST['location']; // the value from your form

if($email_location == "Atlanta") { // a form value and a location
    $your_email = "[email protected]"; // the email for that location
} else if ($email_location == "Colorado") {
    $your_email = "[email protected]";
} else if ($email_location == "Virginia") {
    $your_email = "[email protected]";
} else if ($email_location == "Kansas City") {
    $your_email = "[email protected]";
} else { // if no one is good, send it to this email
    $your_email = "[email protected]";
}

Put this at the place $your_email = "[email protected]"; and you'll be up and running :) Be warned that you do not let the user control a specific email adres, as it could be used to spam then. Always use static references, as now is the case with a location as reference.

Upvotes: 3

Related Questions