Spam in the contact form

Asked

Viewed 418 times

1

Hello. I am suffering from spamming in my Portfolio contact form. I have already put the fields 'name', 'email' and 'message' as mandatory and with the tests I did, it is not possible to send without filling these fields. But every hour comes in my email that form sent only with the field completed email, are random emails. How do I make it stop? Is there any way to do it without putting Captcha on my form?

This is my form:

<form action="sendemail.php" method="post" id="contact-form" class="form-horizontal">
        <fieldset>
                        <div class="form-group">

                          <div class="col-sm-8">
                            <input type="text"  placeholder="Nome" class="form-control" name="nome" id="nome" required="Preencha o seu nome">
                          </div>
                        </div>
                        <div class="form-group">

                          <div class="col-sm-8">
                            <input type="text" placeholder="Email" class="form-control" name="email" id="email">
                          </div>
                        </div>
                        <div class="form-group">

                          <div class="col-sm-8">
                            <input type="text" placeholder="Assunto" class="form-control" name="assunto" id="assunto">
                          </div>
                        </div>
                        <div class="form-group">

                          <div class="col-sm-8">
                            <textarea placeholder="Mensagem" class="form-control" name="mensagem" id="mensagem" rows="3" required=""></textarea>
                          </div>
                        </div>
                 <div class="col-sm-8">
                    <button type="submit" class="btn btn-success">Enviar</button>
                      </div>

                    </fieldset>
                    </form>

And this is the file that sends the form:

    <?php
if(isset($_POST['email'])) {

// Debes editar las próximas dos líneas de código de acuerdo con tus preferencias
$email_to = "[email protected]";
$email_subject = "Contato | Meu Portfólio";
$email_from = "marciaprates.com";


// Aquí se deberían validar los datos ingresados por el usuario
if(!isset($_POST['nome']) ||
!isset($_POST['email']) ||
!isset($_POST['assunto']) ||
!isset($_POST['mensagem'])) {

echo "<b>Não foi possível enviar o formulário. </b><br />";
echo "Por favor, tente novamente!<br />";
die();
}

$email_message = "Formulário do meu Portfólio:\n\n";
$email_message .= "Nome: " . $_POST['nome'] . "\n";
$email_message .= "E-mail: " . $_POST['email'] . "\n";
$email_message .= "Assunto: " . $_POST['assunto'] . "\n\n";
$email_message .= "Mensagem: " . $_POST['mensagem'] . "\n\n";


// Ahora se envía el e-mail usando la función mail() de PHP
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);

echo "<b>O formulário foi enviado com sucesso!</b>";

}
?>

<script>
  setTimeout('window.location.href="index.html"',1700)
</script>

Could someone please help me? I can’t take any more of these emails coming in every hour with just the filled email.

2 answers

1


The isset(); check if the variable was started, even if it is empty it will be started, so always the condition you created will not work, what you can do is the following, change the isset(); one strlen();, for example:

<?php
if(strlen($_POST['email']) > 0) {

// Debes editar las próximas dos líneas de código de acuerdo con tus preferencias
$email_to = "[email protected]";
$email_subject = "Contato | Meu Portfólio";
$email_from = "marciaprates.com";


// Aquí se deberían validar los datos ingresados por el usuario
if(!strlen($_POST['nome']) > 0 ||
!strlen($_POST['email']) > 0 ||
!strlen($_POST['assunto']) > 0 ||
!strlen($_POST['mensagem']) > 0) {

echo "<b>Não foi possível enviar o formulário. </b><br />";
echo "Por favor, tente novamente!<br />";
die();
}

$email_message = "Formulário do meu Portfólio:\n\n";
$email_message .= "Nome: " . $_POST['nome'] . "\n";
$email_message .= "E-mail: " . $_POST['email'] . "\n";
$email_message .= "Assunto: " . $_POST['assunto'] . "\n\n";
$email_message .= "Mensagem: " . $_POST['mensagem'] . "\n\n";


// Ahora se envía el e-mail usando la función mail() de PHP
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);

echo "<b>O formulário foi enviado com sucesso!</b>";

}
?>

<script>
  setTimeout('window.location.href="index.html"',1700)
</script>

Surely there are other ways to do it, but these days this is the one I think most practice in your case.

Test and see if it works as you need it.

OBS.: What you can also help block spam is to add one captcha to your form, so will avoid filling by robots.

  • Thank you, I’ll try here and see if it works.

  • You are welcome, I updated the answer and put an Obs, I think it will also help you.

  • The recapcha of google is very quiet when it is human filling it is only that boring bixo when to some suspicion, alias worked well ?

  • By then strlen(); is working. I’ll wait until tomorrow to see if I don’t get any more empty forms. If I don’t get it, I’ll just leave it to strlen. If not, I’ll test the recapcha. Thanks for the help :)

  • show another tip, is this, a read in this question: https://stackoverflow.com/questions/5855811/how-to-validate-an-email-in-php it validates to know if the data entered is an email, I believe it is a validation relevant to your case, will ensure that the user has actually completed a valid email

  • Whoa, thanks. I’ll try to adapt to mine.

Show 1 more comment

1

Try changing the isset by strlen(), because isset only checks if it is already exists strlen( ) > 0 checks if the String is empty. But, the same idea is to use the Recaptcha.

  • Thanks Mauriney, for the tip. I will test strlen() until tomorrow, if it does not work I test with the recaptcha same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.