I get ghost emails before the test email

1

I have faced a curious problem. I made a basic form in php just to send email through the site. But I'm running some tests on the hosting service and I've received several phantom emails before the test email.

I've already checked to see if something is wrong with the code, but I believe that as basic as it may be, it's okay. Can someone give me a light?

<?php
    $msg=0;
    @$msg= $_POST['msg'];?> 

'<section class="form">
    <?php if($msg=="enviado"): ?>
        <h3>Mensagem enviada. Agradecemos seu contato!</h3>
    <?php else: ?>
        <form class="contact-form" action="contactForm.php" method="post">
            <input type="text" id="name" name="name" placeholder="Nome completo">
            <input type="text" id="mail" name="mail" placeholder="E-mail">
            <input type="text" id="telephone" name="telephone" placeholder="Telefone">
            <input type="text" id="company" name="company" placeholder="Sua empresa">
            <input type="text" id="subject" name="subject" placeholder="Assunto">
            <textarea id="message" name="message" placeholder="Deixe aqui sua mensagem!"></textarea>
            <button class="btn btn-success" type="submit" name="submit">Enviar</button>
        </form>
    <?php endif; ?>
</section>'

<?php
    $para= "[email protected]";
    $assunto="Contato pelo Site.";
    $name= $_POST['name'];
    $mail= $_POST['mail'];
    $telephone= $_POST['telephone'];
    $company= $_POST['company'];
    $subject= $_POST['subject'];
    $msg= $_POST['message'];

    $corpo= "<strong>Mensagem de contato!</strong><br><br>";
    $corpo .= "<strong>Nome: </strong> $name<br><br>";
    $corpo .= "<strong>E-mail: </strong> $mail<br><br>";
    $corpo .= "<strong>Telefone: </strong> $telephone<br><br>";
    $corpo .= "<strong>Empresa: </strong> $company<br><br>";
    $corpo .= "<strong>Assunto: </strong> $subject<br><br>";
    $corpo .= "<strong>Mensagem: </strong> $msg";

    $header="Content-Type: text/html; charset= utf-8\n";
    $header .="From: $mail Reply-to: $mail\n";

    mail($para,$assunto,$corpo,$header);

    header("location:index.php?msg=enviado");?>
    
asked by anonymous 06.03.2018 / 14:53

1 answer

2

I believe that when you open the page an email has already been sent, when it squeezes send it sends one filled, thus totaling two.

This is because your IF is confusing to the code, even because of this redirect, and in the code you posted there are two pages, index.php and contatcForm. php , which is likely to generate one more blank email.

What made an impression with that beginning by putting 0 (ZERO) in the variable that you used another programming language that needs to declare variables at the beginning of the code, that part will be unnecessary.

I'll leave the correct method here, but you'll need to learn the basics about PHP and logic before trying to build more systems with PHP.

For now this will work, but you will have to create a validator to force the user to fill in the fields.

registration form

And then if you want to validate email, phone, etc. you can use regular expression :

javascript-as-validate- one-form fields

Now let's go to HTML and PHP.

This will be the index.php :

<section class="form">
<?php
if(isset($_GET['msg']) && $_GET['msg'] === "enviado"){
    echo "      <h3><h3>Mensagem enviada. Agradecemos seu contato!</h3>\n";
}
?>
    <form class="contact-form" action="contactForm.php" method="post">
        <input type="text" id="name" name="name" placeholder="Nome completo">
        <input type="text" id="mail" name="mail" placeholder="E-mail">
        <input type="text" id="telephone" name="telephone" placeholder="Telefone">
        <input type="text" id="company" name="company" placeholder="Sua empresa">
        <input type="text" id="subject" name="subject" placeholder="Assunto">
        <textarea id="message" name="message" placeholder="Deixe aqui sua mensagem!"></textarea>
        <button class="btn btn-success" type="submit" name="submit">Enviar</button>
    </form>
</section>

This will be the contactForm.php :

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
    $corpo= "<strong>Mensagem de contato!</strong><br><br>";
    $corpo .= "<strong>Nome: </strong> ".$_POST['name']."<br><br>";
    $corpo .= "<strong>E-mail: </strong> ".$_POST['mail']."<br><br>";
    $corpo .= "<strong>Telefone: </strong> ".$_POST['telephone']."<br><br>";
    $corpo .= "<strong>Empresa: </strong> ".$_POST['company']."<br><br>";
    $corpo .= "<strong>Assunto: </strong> ".$_POST['subject']."<br><br>";
    $corpo .= "<strong>Mensagem: </strong> ".$_POST['message'];

    $header="Content-Type: text/html; charset= utf-8\n";
    $header .="From: ".$_POST['mail']." Reply-to: ".$_POST['mail']."\n";
    mail("[email protected]","Contato pelo Site.",$corpo,$header);
    header("location:index.php?msg=enviado");
}else{
    header("location:index.php");
}
?>
    
06.03.2018 / 21:33