PHP表单:基于下拉选择的多个收件人

时间:2014-07-18 01:30:40

标签: php forms header-injection

我需要根据用户下拉菜单选择向多个不同的收件人发送表单。这是我到目前为止所读到的内容......我可以说它成功了,但我没有收到电子邮件。请帮忙!!

HTML:

<select id="sendto" class="css-select" name="sendto">
<option id="sales" value="gmail" name="sendto">Gmail</option>
<option id="support" value="yahoo" name="sendto">yahoo</option>
</select>

PHP:

<?php

$i = $_POST["sendto"];
switch ($i) {
case "gmail":
    $sendto = "gmail@gmail.com";
    break;
case "recpro":
    $sendto = "yahoo@yahoo.com";
    break;
default:
    $sendto = "gmail@gmail.com"; //opional
    break;
} 

function sanitize( $s ){
$injections = array('/(\n+)/i',
'/(\r+)/i',
'/(\t+)/i',
'/(%0A+)/i',
'/(%0D+)/i',
'/(%08+)/i',
'/(%09+)/i'
);
$s = preg_replace( $injections, '', $s );

return $s;
}  
//catch the posted data
$first_name = sanitize( $_POST['first_name'] );
$last_name = sanitize( $_POST['last_name'] );
$email = sanitize( $_POST['email'] );
$telephone = sanitize( $_POST['telelphone'] );

$body = $telephone."\n\n";
$body.= $first_name."<$email>";
$headers = "From: $last_name<$email>";

if(mail($send_to, $subject, $body, $headers)):
echo "success";
else:
echo "error";
endif;
?>

我需要它是头部注射安全。

1 个答案:

答案 0 :(得分:0)

我刚刚改变了

$i = $_POST["sendto"]; switch ($i) { case "gmail": $sendto = "gmail@gmail.com"; break; case "recpro": $sendto = "yahoo@yahoo.com"; break; default: $sendto = "gmail@gmail.com"; //opional break; }

到这个

$i = $_POST["sendto"]; if ($i == "gmail"){ $sendto = "gmail@gmail.com"; } elseif ($i == "yahoo") { $sendto = "gmail@yahoo.com"; } else { $sendto = "gmail@gmail.com"; }

和邮件部分:

`

$headers = "From: " . strip_tags($_POST['your-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['your-email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

if(@mail($sendto, $subject, $mail_body, $headers)):
echo "success";
else:
echo "error";
endif;

`