[PHP] Send mail per form condition

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,413
Reaction score
7,522
Location
Bellville
Hello can someone please assist me?

I have a simple contact form with a radio button to select your town, I want the email to be send to a specific email address depending on the town they select, currently my contact form just send it to me.

I fthey select Cape town it must send to [email protected] if they select pretoria it must go to [email protected] etc etc

HTML:
HTML:
<fieldset>

<!-- Form Name -->
<legend>Contact Us</legend>

<!-- Name input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="name">Name</label>  
  <div class="col-md-4">
  <input id="name" name="name" type="text" placeholder="Enter Name Here" class="form-control input-md">
    
  </div>
</div>

<!-- E-mail input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="email">E-mail</label>  
  <div class="col-md-4">
  <input id="email" name="email" type="text" placeholder="Enter E-mail Here" class="form-control input-md">
    
  </div>
</div>

<!-- Select Town -->
<div class="form-group">
  <label class="col-md-4 control-label" for="town">Town</label>
  <div class="col-md-4">
    <select id="town" name="town" class="form-control">
      <option value="Cape Town">Cape Town</option>
      <option value="Bellville">Bellville</option>
      <option value="Pretoria">Pretoria</option>
      <option value="Johannesburg">Johannesburg</option>
    </select>
  </div>
</div>

<!-- Message -->
<div class="form-group">
  <label class="col-md-4 control-label" for="message">Message</label>
  <div class="col-md-4">                     
    <textarea class="form-control" id="message" name="message" placeholder="Enter Message Here"></textarea>
  </div>
</div>

</fieldset>

PHP:
PHP:
<?php
if(!isset($_POST['submit']))
{
	//This page should not be accessed directly. Need to submit the form.
	echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$town = $_POST['town'];
$message = $_POST['message'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "NichtComrade, you sneaky russian bear";
    exit;
}

$email_from = '[email protected]';//<== My Email Address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message".
    
$to = "[email protected]";//<== My Email Address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
   
?>
 
I was thinking of doing something like this:

PHP:
if(town == capetown ){
    mail()

} elseif(town == pretoria ){
    mail()

} elseif(town == Bellville ){
    mail()

}
etc etc

Not really sure if I am going about this correctly
 
Wait!!

What about this:

What if I make the towns emails in the html?

HTML:
    <select id="town" name="town" class="form-control">
      <option value="[email protected]">Cape Town</option>
      <option value="[email protected]">Bellville</option>
      <option value="[email protected]">Pretoria</option>
      <option value="[email protected]">Johannesburg</option>
    </select>

PHP:
mail($_POST['town'], etc etc etc )
 
Greetings Sir

There are a few ways to do it I would guess.

One would be as you suggest a simple IF statement, but why not as follows.

PHP:
if ($town == "capetown")
{
$to = "[email protected]";
}

if ($town == "pretoria")
{
$to = "[email protected]";
}

if ($town == "johannesburg")
{
$to = "[email protected]";
}
Simple yet effective, anyone can understand.

Another approach could be to define your OPTION slightly different in HTML. Define it there as
HTML:
 <option value="Cape Town - [email protected]">Cape Town</option>
 <option value="Bellville - [email protected]">Bellville</option>
 <option value="Pretoria - [email protected]">Pretoria</option>

Then back in your PHP

Instead of $town = $_POST['town'];

Do
PHP:
$townAndEmail = explode("-",$_POST["town"]);
$town = $townAndEmail[0];
$to = $townAndEmail[1];

That should work perfectly.

This will lead to you only needing to maintain your HTML portion as the PHP will fix itself if you were to expand and add towns.
 
Greetings Sir

There are a few ways to do it I would guess.

One would be as you suggest a simple IF statement, but why not as follows.

PHP:
if ($town == "capetown")
{
$to = "[email protected]";
}

if ($town == "pretoria")
{
$to = "[email protected]";
}

if ($town == "johannesburg")
{
$to = "[email protected]";
}
Simple yet effective, anyone can understand.

Another approach could be to define your OPTION slightly different in HTML. Define it there as
HTML:
 <option value="Cape Town - [email protected]">Cape Town</option>
 <option value="Bellville - [email protected]">Bellville</option>
 <option value="Pretoria - [email protected]">Pretoria</option>

Then back in your PHP

Instead of $town = $_POST['town'];

Do
PHP:
$townAndEmail = explode("-",$_POST["town"]);
$town = $townAndEmail[0];
$to = $townAndEmail[1];

That should work perfectly.

This will lead to you only needing to maintain your HTML portion as the PHP will fix itself if you were to expand and add towns.

You're a super star!!
 
Thankee thankee

Pretty much either way will work, even the ways you suggested will also work. That's the wonderful thing about coding there are many ways to do everything, each with their own drawbacks and benefits tho.
 
Thankee thankee

Pretty much either way will work, even the ways you suggested will also work. That's the wonderful thing about coding there are many ways to do everything, each with their own drawbacks and benefits tho.

I love this world
 
In my opinion, I would not put the email addresses in in <option> .... simply viewing the source would show the addresses.

Spammer heaven !!! That way spammers would be able to bypass the form completely.
 
In my opinion, I would not put the email addresses in in <option> .... simply viewing the source would show the addresses.

Spammer heaven !!! That way spammers would be able to bypass the form completely.

This is also true, best way then probably will be the if or case in php route
 
So my switch case thing was correct?

God dammit I love this new world I discovered.
 
yeah - then you can also have:
Code:
$town = $_POST['town'];
switch ($town) {
    case 'capetown':
    case 'bellville':
        $email_address = '[email protected]';
        break;
    case 'pretoria':
       $email_address = '[email protected]';
        break;
    default:
       $email_address = '[email protected]';
}
mail($email_address, etc etc etc )

'capetown' and 'bellville' get the same $email_address
 
Wait!!

What about this:

What if I make the towns emails in the html?

HTML:
    <select id="town" name="town" class="form-control">
      <option value="[email protected]">Cape Town</option>
      <option value="[email protected]">Bellville</option>
      <option value="[email protected]">Pretoria</option>
      <option value="[email protected]">Johannesburg</option>
    </select>

PHP:
mail($_POST['town'], etc etc etc )

No not do this nor anything relating to this method. If one were to change the value of the option, it would change the email destination... So someone could effectively spam another address by changing
Code:
<option value="[email protected]">Cape Town</option>
to
Code:
<option value="[email protected]">Cape Town</option>
 
No not do this nor anything relating to this method. If one were to change the value of the option, it would change the email destination... So someone could effectively spam another address by changing
Code:
<option value="[email protected]">Cape Town</option>
to
Code:
<option value="[email protected]">Cape Town</option>

Jip we pointed that out earlier I am going with the switch case method
 
Jip we pointed that out earlier I am going with the switch case method

Don't hardcode the email addresses either. Rather pull them from a db or text list . That way you don't mess around with your code when you need to fiddle with the email addresses.

Then your select case will not be even required as you are matching the row based on the option selected. And pulling the email address from there
 
Don't hardcode the email addresses either. Rather pull them from a db or text list . That way you don't mess around with your code when you need to fiddle with the email addresses.

Then your select case will not be even required as you are matching the row based on the option selected. And pulling the email address from there

You'll need to show me in code

I'm a visual learner
 
You'll need to show me in code

I'm a visual learner

Am in bed, so maybe tomorrow.

Just try and always keep your data and code separate.

In this case the names of cities and their respective email addresses are actually data.

If you are using csv then use something like this to read the file into an array: www.w3schools.com/php/func_filesystem_fgetcsv.asp

Then use something like this to search the array for the value (City) : www.w3schools.com/php/func_array_search.asp
 
Lets say... Cape Town has two branches across the street of each other.... ( [email protected] [email protected] )

Let's say... If 10 people select cape town the end result should be that each received 5 emails.

The emails should be send as follows:

cpt1
cpt2
cpt1
cpt2
cpt1
cpt2
cpt1
cpt2
cpt1
cpt2

Does this make sense?

I basically want 1 locations mail to be split between two branches, 1 for you, 1 for you, etc etc.

I was thinking of putting them in an array and then use a random number between 1 and 2 to select the mail from the array, but I want to know what would be best here as I actually have no clue.
 
Top
Sign up to the MyBroadband newsletter
X