我从联系表中收到了很多空邮件。因为它有验证,我不知道这是如何可能的。
这是我的表格(仅给出一行表示):
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-xs-10">
<div class="form-group">
<label>Aanhef *</label>
<div class="label-group">
<label class="radio-inline"><input class="radio" type="radio" required="required" value="Dhr." name="titel">Dhr.</label>
<label class="radio-inline"><input class="radio" type="radio" required="required" value="Mevr." name="titel">Mevr.</label>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="form_email">E-mail adres *</label>
<input id="form_email" type="email" name="email" class="form-control" pattern="^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,})$" placeholder="Uw e-mailadres *" required="required" data-error="Uw e-mailadres (zonder spaties!)">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
</form>
这是我的验证(contact.php):
<?php
// configure
$from = 'myemail';
$sendTo = 'myemail';
$senderNaam = $_POST['naam'];
$senderEmail = $_POST['email'];
$subject = 'Contact';
$okMessage = '<p>Send succesfully!</p>';
$errorMessage = '<p>Error!</p>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: ". $from. "\r\n";
$headers .= 'Reply-To: '. $senderNaam." <".$senderEmail.">\r\n";
// let's do the sending
try
{
$emailText = "New message\n=============================\n";
$emailText = "<table>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Naam:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['titel']." ".$_POST['naam']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Bedrijfsnaam:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['bedrijfsnaam']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Adres:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['straat']." ".$_POST['huisnummer']." ".$_POST['toevoeging']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Postcode:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['postcode']." ".$_POST['letters']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Woonplaats:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['woonplaats']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Telefoonnummer:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['kengetal']." ".$_POST['telefoon']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>E-mailadres:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['email']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Bericht:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['bericht']."</td></tr>
</table>";
mail($sendTo, $subject, $emailText, $headers);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
?>
来自这里的帖子有人说,我引用:“你有没有认为动作网址可以直接从浏览器中接收而没有任何类型的POST数据?这会使所有”字段“变空。”
这可能是问题,但我不知道如何解决这个问题。有人有想法,因为我的邮箱正在运行完整的LOL。
编辑1:好吧,我试过这个:
// add all your other fields here
if (!isset($_POST['naam']) || !isset($_POST['email'])) {
mail($sendTo, $subject, $emailText, $headers);
}
$responseArray = array('type' => 'success', 'message' => $okMessage);
但是没有结果,仍然可以在我的浏览器中打开contact.php,其中写着“表单发送成功”:(。
答案 0 :(得分:1)
您没有在PHP中执行任何类型的服务器端验证。所以是的,只需将我的浏览器指向www.yoursite.com/contact.php即可让我继续向您发送空白电子邮件。
此外,mail
函数不会抛出异常。因此,尝试将其包含在try
/ catch
块中是没有意义的。您应该检查该函数的返回值以了解它失败。
您可以对来自$_POST
的用户提供的输入执行一些基本验证,如此...
// add all your other fields here
if (!isset($_POST['naam']) || !isset($_POST['email'])) {
/* user did not supply there name/email don't send mail */
}
// you may also want to do additional validation like required input length or valid email
if (strlen($_POST['somefield']) < $requiredLength) {
/* input too short */
}
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
/* not a valid email address */
}
此外,您不应盲目地将用户提供的输入注入您的电子邮件标题中。例如,如果用户使用CRLF字符提供值,则可以将标题注入您的电子邮件中。在添加到电子邮件标题之前,您应该从$senderNaam
和$senderEmail
删除所有CRLF字符。
不要忘记,因为您正在使用HTML,所以您也可以像在浏览器中一样从用户输入注入HTML。请务必在输出中使用htmlentities
或htmlspecialchars
。
"<tr>" .
"<td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Naam:</td>" .
"<td style='padding-left: 10px; font-weight: bold;'>".
htmlspecialchars($_POST['titel']) ." ". htmlspecialchars($_POST['naam']) .
"</td>" .
"</tr>"
您还应该注意,仅凭验证仍然不会阻止任何人向您的联系表单发送垃圾邮件。只编写一个向这个PHP脚本发送数千个请求的脚本是微不足道的。
尝试在表单中添加reCaptcha之类的内容以防止此情况发生。