The MAIL function sends two emails to the recipient

Asked

Viewed 31 times

1

I use the function mail in several parts of my projects, however, in an application this function sends two emails to the recipient.

I have no idea why this is happening.

Follow the full code:

<?php
include ("./incluir/conexao.php");

$vconteudo = trim($_GET['conteudo']);
$vmail = trim($_GET['emailcopia']);

$noticia_copia=mysqli_query($con,"SELECT id, slug, titulo FROM noticias WHERE id = $vconteudo GROUP BY id");
while($painel_noticias_copia=mysqli_fetch_array($noticia_copia))
    {
        $vid = $painel_noticias_copia['id'];
        $vslug = $painel_noticias_copia['slug'];
        $vtitulo = $painel_noticias_copia['titulo'];
    }

                $to = $vmail;
                $subject = $vtitulo;
                $message = "

                <h1>Olá, inscrito!</h1>

                <div style='font-size:17px;'>Segue uma cópia do conteúdo que você solicitou.</div><p>

                <div style='font-size:17px;'>Este material foi preparado com muita dedicação.</div><p>

                <div style='font-size:17px;'><b>Pug: conheça tudo sobre a origem milenar da raça</b></div><p>                   

                <div style='font-size:17px;'>Acesse agora mesmo <a href='https://www.portalvidapet.com.br/{$vid}/{$vslug}' target='_blank'>www.portalvidapet.com.br/{$vid}/{$vslug}</a></div><p>


                ";

                $headers = "From: Portal Vida Pet <[email protected]>\n";
                $headers .= 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

                mail($to, $subject, $message, $headers);


            $cap_email=mysqli_query($con,"SELECT email FROM captura WHERE email = '$vmail' GROUP BY email");
            if (mysqli_num_rows($cap_email) < 1)
            {
                $painel=mysqli_query($con,"INSERT INTO captura(email) VALUES ('$vmail')");
            }

?>
  • I think the problem is in HTML. As mentioned by Leandro, the page can be called 2x.

  • the answer helped you? If yes, try to schedule it to help other colleagues.

1 answer

0

Let’s go some alternatives can be checked for this not to occur anymore:

1) Press F12 in the Chrome, goes in the guide "Network", there shows all the pages that are accessed. See if this page is not being called twice. For example, by being called twice once a method post and another document

2) If it appears twice for example, you can put an if in your code for only if it is method post you send your mail(), as follows: (Obs.: already happened to me and I managed to solve with the code below)

<?php 
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

//Seu código aqui

}

3) If you still persist, check your action in the form;

4) If it still persists, create a control variable and save in the bank your sent email, so make sure it has already been sent and do not send it again. Anyway, would these checks lock send twice.

p.s.: look to use phpMailer instead of mail()

Browser other questions tagged

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