PHP Mailer sends email 2 times

Asked

Viewed 515 times

2

I’ve been looking for this problem here on the forum, but I haven’t found it. If there is a link and someone can send me I’m grateful. The problem is this:

This form of mine sends 2 emails to the recipient I set up. I looked at the code again and again, but I didn’t understand why.

PHP

$nome = $_POST['nome'];
$texto = $_POST['texto'];
$enviar= $_POST['enviar'];
if(isset($enviar)){
$corpoMSG = "Oi, meu nome é: ".$nome."<br /><br />".$texto;
$assunto = "Teste para envio de email";
require_once('phpmailer/class.phpmailer.php');
 $mail   = new PHPMailer();
 $mail->CharSet = 'UTF-8';
 $mail->SetFrom('[email protected]', 'Orçamento');
 $address = "[email protected]";
 $mail->AddAddress($address, "destinatario");
 $mail->Subject = $assunto;
 $mail->MsgHTML($corpoMSG);
 if(!$mail->Send()) {
echo "<div class='alert alert-danger'><p style='padding: 0; margin: 0;'>Email não enviado.</p></div>"; 
  } else {
echo "<div class='alert alert-success'><p style='padding: 0; margin: 0;'>
Email enviado com sucesso!</p></div>";  }}
  • is making two requisitions ?

  • how do I verify this?

  • 2

    go in your browser, right click, inspect element and console.

  • Is that all the code of the file? doesn’t seem to have any problems, check if there are any cc lost or of a echo at all addresses(to,cc and cco)

  • It is. html is just a form with two text inputs and the upload button.

  • Gabriel, here in the console nothing appears. In case I always use it for js. How do I use it for php?

  • 2

    Press F12 on Chrome, go to the "Network" tab, there it shows all the pages that are accessed. See if that page that sends is not called 2 times.

  • 1

    um... very interesting... In this tab appeared 2 times the page that sends the form. One with the post method (the one I’m using in the form) and the "Document" type. The other uses the get method and the "text/html type"

Show 3 more comments

1 answer

4


Assuming php code is in the same form file:

This error occurs because there is no verification whether the form was posted or not (whether the user clicked on Ubmit or not). That way every time the page loads the email is sent and when the form is posted the form is sent again.

You can fix your code by checking if the request is a post:

     if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nome = $_POST['nome'];
$texto = $_POST['texto'];

$enviar= $_POST['enviar'];
if(isset($enviar)){
$corpoMSG = "Oi, meu nome é: ".$nome."<br /><br />".$texto;
$assunto = "Teste para envio de email";
require_once('phpmailer/class.phpmailer.php');
 $mail   = new PHPMailer();
 $mail->CharSet = 'UTF-8';
 $mail->SetFrom('[email protected]', 'Orçamento');
 $address = "[email protected]";
 $mail->AddAddress($address, "destinatario");
 $mail->Subject = $assunto;
 $mail->MsgHTML($corpoMSG);
 if(!$mail->Send()) {
echo "<div class='alert alert-danger'><p style='padding: 0; margin: 0;'>Email não enviado.</p></div>"; 
  } else {
echo "<div class='alert alert-success'><p style='padding: 0; margin: 0;'>
Email enviado com sucesso!</p></div>";  }}
}
  • I tried to do what Raphael asked, but it didn’t help. I used this same form that sends the email to register in the database, and when I went to check the records there, it recorded twice the same record. Looks like he’s sending the page twice.

  • From your history on Chrome’s network, this is what’s happening. Give to try a gambiarra: within the parentheses of if, exchange everything for isset($_POST['name']) so the part of the email will only run if the name field is filled. If it’s not that, code is missing and you’ll have to post.

  • I did a test here, putting a page in the action form. I put in another way and gave it right! There was a detail that I didn’t report here and I don’t know if there would be a lot of interference, and that I’m using wordpress. the form is on a wordpress page and I was using a plugin to do this. I just changed the action and put it to a pure php page. And only registered once in the bank and only sent an email. I found it very strange, because I’ve done it other times, and only this time that gave problem.

  • @Diegosouza makes all the difference the code being inside wordpress, as I said before, if it didn’t work had code missing.

Browser other questions tagged

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