NetTemple
NetTemple

Reputation: 319

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 = '[email protected],'.'

    if ($_POST['services'] == 'Electrical'){
        echo ",[email protected]";
    }

    if ($_POST['services'] == 'Plumbing'){
        echo ",[email protected]";
    }

    if ($_POST['services'] == 'Painting'){
        echo ",[email protected]";
    }
.';

So the idea is that if the services check box includes Electrical, then the email variable will include $email_to = '[email protected],[email protected]'

And if the services check box includes Electrical AND Plumbing, then the email variable will include $email_to = '[email protected], [email protected], [email protected]'

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 = '[email protected],{$email_from}';
    if($_POST['services'] == "Ecomtek"){ 
        $email_to .= ",[email protected]"; 
        }
    if($_POST['services'] == "E-Payroll"){ 
        $email_to .= ",[email protected]"; 
        }
    if($_POST['services'] == "Humana"){ 
        $email_to .= ",[email protected]";
        }

$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>

Upvotes: 1

Views: 965

Answers (4)

user557846
user557846

Reputation:

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

$email_to = '[email protected]';

    if (in_array('Electrical',$_POST['services'])){
        $email_to .= ",[email protected]";
    }

    if (in_array('Plumbing',$_POST['services'])){
        $email_to .= ",[email protected]";
    }

    if (in_array('Painting',$_POST['services'])){
        $email_to .= ",[email protected]";
    }

.= appends the 2nd address to the first

Upvotes: 5

NetTemple
NetTemple

Reputation: 319

$email_to = '[email protected]';

    if (in_array('Electrical',$_POST['services'])){
        $email_to .= ",[email protected]";
    }

    if (in_array('Plumbing',$_POST['services'])){
        $email_to .= ",[email protected]";
    }

    if (in_array('Painting',$_POST['services'])){
        $email_to .= ",[email protected]";
    }

Upvotes: 1

Chris
Chris

Reputation: 4810

You could use an array for this.

$emails = array();

if($_POST['services'] == 'Plumbing') {
    $emails[] = '[email protected]';
}

if($_POST['services'] == 'Painting') {
    $emails[] = '[email protected]';
}

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

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

Upvotes: 1

Ganesh Ghalame
Ganesh Ghalame

Reputation: 7003

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

$email_to = '[email protected]';
if(isset($_POST['services']){
    if ($_POST['services'] == 'Electrical'){
        $email_to .= ",[email protected]";
    }
    if($_POST['services'] == 'Plumbing'{
        $email_to .= ",[email protected]";
    }
    if($_POST['services'] == 'Painting'){
        $email_to .= ",[email protected]";
    }
}

Upvotes: 1

Related Questions