I get ghost emails before the test email

Asked

Viewed 64 times

1

I have faced a curious problem. I have drawn up a basic form in php only to send email through the site. But I’m running some tests on the hosting service and I’ve been receiving several ghost emails before the test email.

I’ve researched to see if there’s something wrong with the code, but I believe as basic as it is, it’s okay. Someone can shed some 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");?>
  • doubt ever thought of using a framework like codeingiter? Te would solve a lot of aspects

  • 1

    What are these "ghost emails"?

  • Isn’t it because the mail function will always be executed, even if you don’t click send? I think you have to have a condition first.

  • @igorhenrique no, because the ghost emails appear along with the test email after clicking on Send.

  • @dvd When I click Send to a test email, I receive in my inbox the e-mail with Name, Asssunto, Email, etc filled as it was sent. But along with the test email comes two phantom emails, with no content filled.

  • Which comes first the filled or empty email?

  • @Rpgboss usually reach both voids before. There are times when two voids and two test equal.

  • is using sendmail?

  • @igorhenrique no, only php’s mail function.

  • Why not use Php Mailer (https://github.com/PHPMailer/PHPMailer) ?

Show 5 more comments

1 answer

1


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

That’s why your IF is confused for the code, including because of this redirection and in the code you posted there are two pages, the index php. and the contactcForm.php, which probably generates one more blank email.

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

I’ll leave the correct method here, but you’ll need to learn the basics of PHP and logic before trying to create 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-how-to-validate-fields-of-a-form

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");
}
?>

Browser other questions tagged

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