phpemailif-statementheadercarbon-copy

PHP if value, then other email?


I have a PHP form for my event page. Right now it works perfectly to email to myself, and I can CC it to whomever I like without problems. HOWEVER, I'd like to ONLY send it to a specific person if a certain event is selected from a drop box on my form, and CC it to me. This way the organization that is holding the event can receive a copy of the completed form. I would like to also send a customized message to the person it's being sent to if possible. So for example:

If the person filling out the form selects the event, "Heart Walk," then the form results should be sent to "Bob" at "bob@email.com" with a message saying, "Hi Bob, you have received a new volunteer for your Heart Walk event. Below is a copy of the registration."

And of course I get a copy.

Can I do an if/then statement or an array for the $sendtoemail command? And then copy it to me by putting the following:

$headers .= "Cc: me@myemail.com\r\n";

What would be the best way to do this? Thanks for your help! :)

EDIT :

Here is my code:

<?php
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value)
{
    $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($value);

    return $value;
}

$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}

//send an email from the server TO YOUR EMAIL
$fromname = $_REQUEST['firstname'];
$fromemail = $_REQUEST['email'];
$subject = "NMVN EVENT REGISTRATION";

$message = " <b>EVENT:</b> ".$_REQUEST['pickevent'];
$message .= " <br><br> <b>First Name:</b> ".$_REQUEST['firstname'];
$message .= " <br> <b>Last Name:</b> ".$_REQUEST['lastname'];
$message .= " <br> <b>Cell Phone:</b> ".$_REQUEST['cellphone'];
$message .= " <br> <b>Alternative Phone:</b> ".$_REQUEST['altphone'];

//This is the person who is going to receive the email
$sendtoname = "Mike Gandy";
$sendtoemail = "me@myemail.com";

//Email header stuff
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $fromname <$fromemail>\r\n";
$headers .= "To: $sendtoname <$sendtoemail>\r\n";
$headers .= "Reply-To: $fromname <$fromemail>\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-MSMail-Priority: Normal\r\n";
$headers .= "X-Mailer: Created by Mike Gandy";

//this next line creates and sends the email
mail($sendtoemail, $subject, $message, $headers);

?>

I was thinking about replacing this:

$sendtoname = "Mike Gandy";
$sendtoemail = "me@myemail.com";

With this:

if ($_REQUEST['pickevent'] == 'HeartWalk'))
//if "HeartWalk" is filled out, send to email
  {
  //send email
  $sendtoname = "Bob";
  $sendtoemail = "bob@email.com";
  }
elseif ($_REQUEST['pickevent'] == 'RaceCure'))
//if "RaceCure" is filled out, send to email
  {
  //send email
  $sendtoname = "Susan";
  $sendtoemail = "susan@email.com";
  }  
elseif ($_REQUEST['pickevent'] == 'Bowling'))
//if "Bowling" is filled out, send to email
  {
  //send email
  $sendtoname = "John";
  $sendtoemail = "john@email.com";
  }

But additionally I need to require that an event be selected, so the "else" would be a popup that says "Pick an event!". Does that help?

LAST EDIT :

Ok, so I got the validation working. Thank you. I am replacing this:

$sendtoname = "Mike Gandy";
$sendtoemail = "me@myemail.com";

With this:

switch(strtolower($event){
    case 'HeartWalk':
        $sendtoname('Bob')
        $sendtoemail('bob@email.com');
        break;
    case 'RaceCure':
        $sendtoname('Susan')
        $sendtoemail('susan@email.com');
        break;
    case 'Bowling':
        $sendtoname('John')
        $sendtoemail('john@email.com');
        break;
}

Adding this:

$headers .= "Cc: somebody@domain.com" . "\r\n";

And changed the beginning of my message to this:

$message = "Dear $sendtoname, <br><br>A new volunteer has registered for your "$_REQUEST['pickevent'];
$message .= " event. Below is a copy of their registration: ".$_REQUEST['blank'];
$message .= " <br><br> <b>First Name:</b> ".$_REQUEST['firstname'];

And included this in my form:

<input type="hidden" name="blank" value="">

Yes? No?


Solution

    1. Use the select HTML tag to create the drop down menu:

      <select name="event">
      <option value="heartwalk">Heart Walk</option>
      <option value="other">Other</option>
      </select>
      
    2. When handling the post, use the $_REQUEST["event"] for decision like:

      switch ($_REQUEST["event"])
      {
      case 'heartwalk':
          $sendtoemail = "heartwalk@example.com";
          break;
      default:
          $sendtoemail = "other@example.com";
          break;
      }
      
    3. For validation, you can start with JavaScript validation on client, when submitting the form. For example refer to: https://www.tutorialspoint.com/javascript/javascript_form_validations.htm

    Of course, some validation on server side is recommended too. Depending on how robust you want your system to be, you may start with simply not doing anything to redirecting browser back to the form and displaying message on the page.