繁体   English   中英

以验证的形式接收空电子邮件

[英]Receiving empty emails in validated form

我从联系表中收到了很多空邮件。 因为它有验证,我不知道这是如何可能的。

这是我的表格(仅给出一行表示):

<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,上面写着“表单发送成功”:(。

您没有在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。 确保在输出中使用htmlentitieshtmlspecialchars

"<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的内容以防止这种情况发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM