PHP mailer to submit to different Emails based on selection

I was wondering if someone could point me in the right direction.  I am looking to create a form that has options to send to different people depending on the selection.  So if they select Joe Smo, the form will be submitted to Joesmo@xxx.com, or if Jane doe is sleceted, it will goto jandoe@xxxx.com.  Here is my code so far....

I know how to do a simple form to just a certain person, just can't wrap my hands around a certain selection to goto different people, or for that matter different groups of people if I wanted to.

<td align="right" valign="top"> <form method="POST" action="contact2.php">
                  <table width="100%" border="0" cellpadding="2" cellspacing="0" class="small-body-text-gry">
                    <tr>
                      <td width="85"><div align="right" class="style1"> Manager:</div></td>
                      <td class="med-body-text-gry"><select name="cto" id="formcontactmgrs_cto" class="small-body-text">
                          <option value="joesmoe@gmail.com" selected="selected">Joe Smo</option>
                          <option value="janedoe@gmail.com">Jane Doe</option>
                          </select>
                          <span class="redTest"> *</span></td>
                    </tr>
                    <tr>
                      <td><div align="right" class="style1">Date:</div></td>
                      <td class="med-body-text-gry"><input type="text" name="Date2" /></td>
                    </tr>
                    <tr>
                      <td><div align="right" class="style1">Your Email:</div></td>
                      <td class="med-body-text-gry"><input type="text" name="YourEmail2" />
                        <span class="redTest">*</span></td>
                    </tr>
                    <tr>
                      <td><div align="right"><span class="style1">Phone</span>:</div></td>
                      <td class="med-body-text-gry"><input type="text" name="Phone2" /></td>
                    </tr>
                    <tr>
                      <td valign="top"><div align="right" class="style1">Message:</div></td>
                      <td valign="top" class="med-body-text-gry"><span class="redTest">
                        <textarea name="Message2"></textarea>
                        *</span></td>
                    </tr>
                    <tr>
                      <td colspan="2"><div align="center">
                          <input name="submit2" type="submit" class="small-body-text" value="send now" />
                          </div></td>
                    </tr>
                  </table>
                </form>






!!!!PHP Code!!!!!!!!

<?php
// get posted data into local variables
$EmailFrom = "smtp@domain.com";
$EmailTo = "selected.value";
$Subject = "A customer is trying to contact you from domain.com";
$YourEmail2 = Trim(stripslashes($_POST['YourEmail2'])); 
$Date2 = Trim(stripslashes($_POST['Date2'])); 
$Phone2 = Trim(stripslashes($_POST['Phone2'])); 
$Message2 = Trim(stripslashes($_POST['Message2'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "YourEmail: ";
$Body .= $YourEmail2;
$Body .= "\n";
$Body .= "Date: ";
$Body .= $Date2;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone2;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message2;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contactus.php\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

Open in new window

cstallingsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HackneyCabCommented:
Well, if you want to allow more than one target to be selected at a time, add a "multiple" attribute to your select element, and add square brackets to its control name, like this:

<select name="cto[]" id="formcontactmgrs_cto" class="small-body-text" multiple="multiple">

Now PHP will turn the submitted values of cto into an array, which you can iterate through like this:

$targets = $_POST['cto'];
foreach($targets as $recipient) {
    echo $recipient;
}

However, you probably don't want to have actual email addresses in the source code for your page, so I recommend you use codes or numbers to represent each person, and then convert the codes using an array (which will be in the PHP source code, which should never be visible to the public, so remains hidden). Something like this:

$conversion_array = array(
    'joe_s' => 'joesmo@gmail.com',
    'jane_d' => 'janedoe@gmail.com'
);
$targets = $_POST['cto'];
foreach($targets as $recipient) {
    echo $conversion_array[$recipient];
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cstallingsAuthor Commented:
so something like this would work?

(I think I am still off on the php part of it)
<td align="right" valign="top"> <form method="POST" action="contact2.php"> 
                  <table width="100%" border="0" cellpadding="2" cellspacing="0" class="small-body-text-gry"> 
                    <tr> 
                      <td width="85"><div align="right" class="style1"> Manager:</div></td> 
                      <td class="med-body-text-gry"><select name="cto" id="formcontactmgrs_cto" class="small-body-text"> 
                          <option value="joe_smo" selected="selected">Joe Smo</option> 
                          <option value="jane_doe">Jane Doe</option> 
                          </select> 
                          <span class="redTest"> *</span></td> 
                    </tr> 
                    <tr> 
                      <td><div align="right" class="style1">Date:</div></td> 
                      <td class="med-body-text-gry"><input type="text" name="Date2" /></td> 
                    </tr> 
                    <tr> 
                      <td><div align="right" class="style1">Your Email:</div></td> 
                      <td class="med-body-text-gry"><input type="text" name="YourEmail2" /> 
                        <span class="redTest">*</span></td> 
                    </tr> 
                    <tr> 
                      <td><div align="right"><span class="style1">Phone</span>:</div></td> 
                      <td class="med-body-text-gry"><input type="text" name="Phone2" /></td> 
                    </tr> 
                    <tr> 
                      <td valign="top"><div align="right" class="style1">Message:</div></td> 
                      <td valign="top" class="med-body-text-gry"><span class="redTest"> 
                        <textarea name="Message2"></textarea> 
                        *</span></td> 
                    </tr> 
                    <tr> 
                      <td colspan="2"><div align="center"> 
                          <input name="submit2" type="submit" class="small-body-text" value="send now" /> 
                          </div></td> 
                    </tr> 
                  </table> 
                </form> 
 
 
 
 
 
 
!!!!PHP Code!!!!!!!! 
 
<?php 
// get posted data into local variables 
$EmailFrom = $conversion_array = array(
    'joe_smo' => 'joesmo@gmail.com',
    'jane_doe' => 'janedoe@gmail.com'
);
$targets = $_POST['cto'];
foreach($targets as $recipient) {
    echo $conversion_array[$recipient];
}
$EmailTo = "selected.value"; 
$Subject = "A customer is trying to contact you from domain.com"; 
$YourEmail2 = Trim(stripslashes($_POST['YourEmail2']));  
$Date2 = Trim(stripslashes($_POST['Date2']));  
$Phone2 = Trim(stripslashes($_POST['Phone2']));  
$Message2 = Trim(stripslashes($_POST['Message2']));  
 
// validation 
$validationOK=true; 
if (!$validationOK) { 
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; 
  exit; 
} 
 
// prepare email body text 
$Body = ""; 
$Body .= "YourEmail: "; 
$Body .= $YourEmail2; 
$Body .= "\n"; 
$Body .= "Date: "; 
$Body .= $Date2; 
$Body .= "\n"; 
$Body .= "Phone: "; 
$Body .= $Phone2; 
$Body .= "\n"; 
$Body .= "Message: "; 
$Body .= $Message2; 
$Body .= "\n"; 
 
// send email  
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); 
 
// redirect to success page  
if ($success){ 
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contactus.php\">"; 
} 
else{ 
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; 
} 
?>

Open in new window

0
hieloCommented:

<td align="right" valign="top"> <form method="POST" action="contact2.php"> 
                  <table width="100%" border="0" cellpadding="2" cellspacing="0" class="small-body-text-gry"> 
                    <tr> 
                      <td width="85"><div align="right" class="style1"> Manager:</div></td> 
                      <td class="med-body-text-gry"><select name="cto[]" id="formcontactmgrs_cto" class="small-body-text"> 
                          <option value="joe_smo" selected="selected">Joe Smo</option> 
                          <option value="jane_doe">Jane Doe</option> 
                          </select> 
                          <span class="redTest"> *</span></td> 
                    </tr> 
                    <tr> 
                      <td><div align="right" class="style1">Date:</div></td> 
                      <td class="med-body-text-gry"><input type="text" name="Date2" /></td> 
                    </tr> 
                    <tr> 
                      <td><div align="right" class="style1">Your Email:</div></td> 
                      <td class="med-body-text-gry"><input type="text" name="YourEmail2" /> 
                        <span class="redTest">*</span></td> 
                    </tr> 
                    <tr> 
                      <td><div align="right"><span class="style1">Phone</span>:</div></td> 
                      <td class="med-body-text-gry"><input type="text" name="Phone2" /></td> 
                    </tr> 
                    <tr> 
                      <td valign="top"><div align="right" class="style1">Message:</div></td> 
                      <td valign="top" class="med-body-text-gry"><span class="redTest"> 
                        <textarea name="Message2"></textarea> 
                        *</span></td> 
                    </tr> 
                    <tr> 
                      <td colspan="2"><div align="center"> 
                          <input name="submit2" type="submit" class="small-body-text" value="send now" /> 
                          </div></td> 
                    </tr> 
                  </table> 
                </form> 
 
 
 
 
 
 
!!!!PHP Code!!!!!!!! 
 
<?php 
     // validation 
     $validationOK=true; 
     if (!$validationOK) { 
       print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; 
       exit; 
     } 

// get posted data into local variables 
$EmailFrom = $conversion_array = array(
    'joe_smo' => 'joesmo@gmail.com',
    'jane_doe' => 'janedoe@gmail.com'
);
$targets = $_POST['cto'];
foreach($targets as $recipient) {
    
     $EmailTo = $EmailFrom[ $recipient ]; 
     $Subject = "A customer is trying to contact you from domain.com"; 
     $YourEmail2 = Trim(stripslashes($_POST['YourEmail2']));  
     $Date2 = Trim(stripslashes($_POST['Date2']));  
     $Phone2 = Trim(stripslashes($_POST['Phone2']));  
     $Message2 = Trim(stripslashes($_POST['Message2']));  
      
      
     // prepare email body text 
     $Body = ""; 
     $Body .= "YourEmail: "; 
     $Body .= $YourEmail2; 
     $Body .= "\n"; 
     $Body .= "Date: "; 
     $Body .= $Date2; 
     $Body .= "\n"; 
     $Body .= "Phone: "; 
     $Body .= $Phone2; 
     $Body .= "\n"; 
     $Body .= "Message: "; 
     $Body .= $Message2; 
     $Body .= "\n"; 
      
     // send email  
     $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); 
      
     // redirect to success page  
     if ($success){ 
       print "<meta http-equiv=\"refresh\" content=\"0;URL=contactus.php\">"; 
     } 
     else{ 
       print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; 
     } 
	exit;
}
?>

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

cstallingsAuthor Commented:
Thanks for the response, getting close, but getting error on line 16 of the php code with this....
foreach($targets as $recipient) {

Open in new window

0
cstallingsAuthor Commented:
meant to say that is line 16
0
hieloCommented:
your select name needs brackets at then end=>[]:
<select name="cto[]" id="formcontactmgrs_cto" class="small-body-text">
0
cstallingsAuthor Commented:
Awesome it is working!!!!!

Now just one more question, how do I get it to say from mywebsite instead of "Array@domain.com" when the email is sent to the receipient?  I would like for it to say "Mydomain@domain.com instead of the "Array"
0
hieloCommented:
on this line:
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

provide the "From:" address you want - ex:
$success = mail($EmailTo, $Subject, $Body, "From: cstallings@cstallings.com");
0
HackneyCabCommented:
Just a final warning about security. Be careful never to put user-supplied (form) data into your $Subject or additional header parameters in a call to PHP's mail function. (At least, not before thoroughly checking that it is safe.)

If you allow user-supplied data into your headers (and the email subject is a header) you leave the door open for site crackers to inject headers into your emails. This allows them to hijack your email feature, and send spam email to whomsoever they please, all from your server. This can lead to your server being listed as a spam gateway, which means your legitimate emails may not reach their targets because they get marked as spam.

So beware. If you have to allow user data into email headers, do some research for tips on how to check that the data is safe to place into email headers. (For instance, you need to make sure there are no control characters such as newlines or carriage returns.)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.