Multiple sendTo with checkboxes in php mail function

I’m pretty new to php and i’m trying to create a form that will have multiple check boxes that will send an email to those check boxes.
So far i’ve got it as an “IF” loop and tried a “FOR” loop but could only get 1 check box at a time to work. I did start to look at counting the address in the array but couldnt get it to work…
HTML:

<label for="messageto">Email to:</label>
        <input type="checkbox" name="option" value="A" />Personal
        <input type="checkbox" name="option" value="B" />Student
</label>

PHP:


/*E-mail Elements*/
$emailSubject = $_POST['subject'];

/*Variables from the form */
$from= $_POST['from'];
$fromemail= $_POST['fromemail'];
$subject= $_POST['subject'];
$message= $_POST['message'];
$to= $_POST['option'];


/*E-Mail to different people*/
	if ($to == 'A')
		{
			$sendto = "sydney@localhost";
		}
	elseif ($to == 'B')
			{
				$sendto = "student@localhost";
			};

/*Body*/
$body = <<<EOD
<br><hr><br>
<strong>From:</strong> $from.<br>
<strong>Subject:</strong> $subject <br>
<strong>E-Mail:</strong> $fromemail <br>
<strong>Message:</strong> $message <br>
EOD;
$headers = "From: $fromemail\\r\
";
$headers .= "Content-type: text/html\\r\
";
$success = mail($sendto, $emailSubject, $body, $headers);

/* Rendering Results */
$theResults = <<<EOD
EOD;
echo "$theResults";

/*Redirecting Page*/
header('Location: thank-you.html');

any help would be really appreciated.

If the email is to be sent to either A or B then you would use a radio button, not a checkbox (you might even pre-select one to be sure an email is sent).

If the email is to be sent to A, or A and B or neither then you would use a checkbox, but you’d create it like so:


<?php
if( isset($_POST))
  var_dump($_POST);
?>

<form action="" method=post>
<hr />
<input type=radio name=radio value="X" selected=selected />
<input type=radio name=radio value="Y" />
<input type=radio name=radio value="Z" />
</hr>

<input type=checkbox name=cbox[] value="A" />
<input type=checkbox name=cbox[] value="B" />
<input type=checkbox name=cbox[] value="C" />
</hr>
<input type=submit />
</form>

put that into a test file and load it up and have a play, see what the results are.

Also, the code you showed us has a potentially common fault, you are doing a redirect using header() which must not be preceded by any output from PHP.


echo "$theResults"; // that is wrong, no need to quote the output in this case


// if $results had any contents at all, this would throw a big warning onto your page
/*Redirecting Page*/  
header('Location: thank-you.html');  

Thanks Cups,

I used some of your code and i managed to get it to work, i used a bit of debugging through var_dump to see where i was going wrong, but i managed to get it to work with radio buttons so i though i’d share :slight_smile:
Thanks again/


/*E-mail Elements*/
$emailSubject = $_POST['subject'];

/*Variables from the form */

$from= $_POST['from'];
$fromemail= $_POST['fromemail'];
$subject= $_POST['subject'];
$message= $_POST['message'];
$to= $_POST['rbox'];

//$results = print_r($to);

/*E-Mail to different stores*/
foreach($to as $results)
{
	if ($results == 'A')
	{
		$sendto = 'sydney@localhost';
	}
	elseif ($results == 'B')
	{
		$sendto = 'student@localhost';
	}

}

//var_dump($sendto); Testing

/*Body*/
$body = <<<EOD
<br><hr><br>
<strong>From:</strong> $from "< $fromemail >"<br>
<strong>Subject:</strong> $subject <br>
<strong>Message:</strong> You have recieved a message from $from <br> $message <br>
EOD;
$headers = "From: $fromemail\\r\
";
$headers .= "Content-type: text/html\\r\
";
$success = mail($sendto, $subject, $body, $headers);
/* Rendering Results */
$theResults = <<<EOD
EOD;
echo $theResults;

?>

Good, glad you are getting somewhere.

You will experience problems when the user does not supply you with $to variable - but as you have not posted any validation of the incoming variables it is probably the right time to point out to you that the script as shown is wide open to abuse.

Maybe you left all that out for the sake of brevity, otherwise read up on the FIEO principles and best practices when it comes to PHP.

One way to Filter the Inputted $to variable and to make the foreach() loop less onerous would be to only ‘look up’ an email address if the incoming ‘rbox’ variable is listed amongst the permitted values, and then keep that in an array and associate it with the correct value.

When $to is coming from a Radio button there is only one value passed, so you do not need the foreach loop.


$to = "D";

// maintain an array of people instead of a more 
// error prone if/elseif/else construction
// this list could also be generated from a database too

$allowed = array(
"A"=>"student@localhost", 
"B"=>"sydney@localhost", 
"C"=>"admin@localhost", 
);


// if the $to variable is in the array of permitted values
// then look up that key's actual value
// hence Filtering against expectations

    if( array_key_exists($to, $allowed)){
     $sendto = $allowed[$to];
    } else {
    // handle failure somehow...
    echo 'Token passed was not recognised, aborting';
    }

// only attempt the mail() if all the necessary values are there
if(isset($sendto))  echo $sendto;

The same idea can then be used for checkboxes which will need to contain a foreach loop.

yeah, i noticed when trying with checkboxes that nothing would happen if nothing was selecting ( because there was no $to variable).
I’ve just keep the foreach loops as i was trying to get checkboxes to work, which i can get if they choose one, but as soon as they choose more than one address php just seems to remember the last checkbox clicked. But i am getting close to a solution =)

I did leave out all the validation as i just had problem getting the checboxes to work. But i will be reading more into the FIEO principles. =)

Thanks for your help =)

You want to end up with the string ‘admin@localhost,student@localhost’ so you’d use string concatenation in the loop.

Heres the idea, with most of the details left out …


$sendto = "";  // initialise the var

foreach() {  // now do your loop

        $sendto .= 'student@localhost,';  // concat the string, NB no space between .= (important!)

}

// check $sendto is not still an empty string ...
if( $sendto ) echo $sendto;

Glad to hear abt the validation…