简体   繁体   中英

Add email address to php $email_to variable

I have a form that has a simple check box list. I need the form sent via email to all those based on the boxes that have been checked. Below is the basic logic bit I realize the code is way off.

$email_to = 'david@gmail.com,'.'

    if ($_POST['services'] == 'Electrical'){
        echo ",roger@gmail.com";
    }

    if ($_POST['services'] == 'Plumbing'){
        echo ",bill@gmail.com";
    }

    if ($_POST['services'] == 'Painting'){
        echo ",donnie@gmail.com";
    }
.';

So the idea is that if the services check box includes Electrical , then the email variable will include $email_to = 'david@gmail.com,roger@gmail.com'

And if the services check box includes Electrical AND Plumbing , then the email variable will include $email_to = 'david@gmail.com, roger@gmail.com, bill@gmail.com'

Any help would be appreciated.

HERE IS THE ACTUAL CODE THAT I TRIED TO IMPLEMENT AFTER RECEIVING YOUR SUGGESTIONS - BUT STILL DOESNT WORK - AND I TRIED ALL THREE SUGGESTIONS?

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
if(isset($_POST['email'])) {

$email_from = $_POST["email"];
$email_to = 'nettemple@gmail.com,{$email_from}';
    if($_POST['services'] == "Ecomtek"){ 
        $email_to .= ",sol@sungazer.com"; 
        }
    if($_POST['services'] == "E-Payroll"){ 
        $email_to .= ",sarah@nettemple.net"; 
        }
    if($_POST['services'] == "Humana"){ 
        $email_to .= ",dmeyers@scad.edu";
        }

$email_subject = "WEB INQUIRY";
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($email_to, $email_subject, $email_message, $headers); 

?>

THIS IS THE FORM....

<form name="htmlform" method="post" action="form_process2.php">

<input type="checkbox" name="services[]" value="Ecomtek" />&nbsp;Ecomtek<br />
<input type="checkbox" name="services[]" value="E-Payroll" />&nbsp;E-Payroll<br />
<input type="checkbox" name="services[]" value="Humana" />&nbsp;Humana<br />
<input type="checkbox" name="services[]" value="John Conti" />&nbsp;John Conti<br />
<input type="checkbox" name="services[]" value="Kentucky RX Card" />&nbsp;Kentucky RX Card<br />
<input type="checkbox" name="services[]" value="Lifelock" />&nbsp;Lifelock<br />
<input type="checkbox" name="services[]" value="Logans" />&nbsp;Logan's<br />
<input type="checkbox" name="services[]" value="Norton Healthcare" />&nbsp;Norton Healthcare<br />
<input type="checkbox" name="services[]" value="Office Depot" />&nbsp;Office Depot<br />
<input type="checkbox" name="services[]" value="ProWaste" />&nbsp;ProWaste<br />
<input type="checkbox" name="services[]" value="Thorntons" />&nbsp;Thorntons<br />


  <label for="firstname">*FirstName</label>
  <input  type="text" name="firstname" maxlength="150" size="50" class="formbox">

  <label for="lastname">*Last Name</label>
  <input  type="text" name="lastname" maxlength="150" size="50" class="formbox">

  <label for="company">*Company</label>
  <input  type="text" name="company" maxlength="150" size="50" class="form box">

  <label for="email">*Email Address</label>
  <input  type="text" name="email" maxlength="180" size="50" class="form box">

</form>

$_POST['services'] is an array so:

$email_to = 'david@gmail.com';

    if (in_array('Electrical',$_POST['services'])){
        $email_to .= ",roger@gmail.com";
    }

    if (in_array('Plumbing',$_POST['services'])){
        $email_to .= ",bill@gmail.com";
    }

    if (in_array('Painting',$_POST['services'])){
        $email_to .= ",donnie@gmail.com";
    }

.= appends the 2nd address to the first

You could use an array for this.

$emails = array();

if($_POST['services'] == 'Plumbing') {
    $emails[] = 'user@gmail.com';
}

if($_POST['services'] == 'Painting') {
    $emails[] = 'user@gmail.com';
}

When going to send the email, I would just iterate the array.

foreach( $emails as $email )
    mail($email, 'Subject', ....);

Added isset($_POST['services']) to validate variable before use, and for appending the string you can use .=

$email_to = 'david@gmail.com';
if(isset($_POST['services']){
    if ($_POST['services'] == 'Electrical'){
        $email_to .= ",roger@gmail.com";
    }
    if($_POST['services'] == 'Plumbing'{
        $email_to .= ",bill@gmail.com";
    }
    if($_POST['services'] == 'Painting'){
        $email_to .= ",donnie@gmail.com";
    }
}
$email_to = 'david@gmail.com';

    if (in_array('Electrical',$_POST['services'])){
        $email_to .= ",roger@gmail.com";
    }

    if (in_array('Plumbing',$_POST['services'])){
        $email_to .= ",bill@gmail.com";
    }

    if (in_array('Painting',$_POST['services'])){
        $email_to .= ",donnie@gmail.com";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM