Forum Moderators: coopster
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Form</title>
</head>
<body>
<form action="mail.php" method="post">
Your Name:<br>
<input type="text" name="name"><br><br>
E-mail:<br>
<input type="text" name = "email"><br><br>
Comments<br>
<textarea name="comments"></textarea><br><br>
<input type="checkbox" name="incoming_mailto[]" value="gmail">Send form to gmail address</input><br>
<input type="checkbox" name="incoming_mailto[]" value="yahoo">Send form to yahoo address</input><br>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?
function checkOK($field)
{
if (eregi("\r",$field) ¦¦ eregi("\n",$field)){
die("Invalid Input!");
}
}
// make array of all possible mails that could be used - a security check
$permittedmailsarray = Array(
'gmail' => 'skharrisla@gmail.com',
'yahoo'=> 'webtechla@yahoo.com'
);
if(is_array($_POST['incoming_mailto'])) {
foreach($_POST['incoming_mailto'] as $v){
$recipients = $permittedmailsarray[$v];
}
}
$name=$_POST['name'];
checkOK($name);
$email=$_POST['email'];
checkOK($email);
$comments=$_POST['comments'];
checkOK($comments);
$to="skh@webtechla.com";
$message="$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email";
if(mail($to,"Comments From Your Site",$message,"From: $email\n")) {
echo "Thanks for your feedback.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
?>
This line you have in your foreach loop:
$recipients = $permittedmailsarray[$v];
Whats happening there is you are assigning a variable and then over writing it each time the loop loops. So, the value will only be the value of the last iteration.
What are you trying to do exactly? Do you mind me asking? Prevent spamming in some way?
dc
foreach($_POST['incoming_mailto'] as $v){
$recipients = $permittedmailsarray[$v];
}
to this:
$recipients = array();
foreach($_POST['incoming_mailto'] as $v){
$recipients[] = $permittedmailsarray[$v];
}
and then implode the array in your mail function:
if (!empty($recipients))
{
$to_field = implode(",", $recipients);
if(mail($to_field,"Comments From Your Site",$message,"From: $email\n")) {
echo "Thanks for your feedback.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
}
Does that help at all?
dc