E-mail is sent only to the last confirmed/active user

Asked

Viewed 33 times

1

Well, here’s the thing.

I was doing some tests of sending Newsletter and I came across a problem.

The part of registration/ user insertion is done, working all right.

However, I was only sending it to an email, mine in the case. So I decided to insert a few more to check how the system would go. I noticed that it, independent of the email (Outlook, Gmail, Yahoo..), sends only to the last registered, ie the last user who confirmed the registration through the confirmation email that is sent to the email of the same. When confirming, the token and user status is changed.

Well, if it’s unclear, just ask.

Follow the code of the page in question:

<?php
if(!isset($_GET['id'])){
    header("Location: home.php?action=ver-newsletter"); exit;
}
$id = $_GET['id'];
$select = "SELECT * FROM newsletter WHERE id=:id";
$contagem = 1;
try{
    $result = $conexao->prepare($select);
    $result->bindParam(':id', $id, PDO::PARAM_INT);
    $result->execute();
    $contar = $result->rowCount();
    if($contar>0){
        while($show = $result->FETCH(PDO::FETCH_OBJ)){
            $idNews = $show->id;    
            $titulo = $show->titulo;    
            $data = $show->data;    
            $imagem = $show->imagem;    
            $descricao = $show->descricao;  
        }
    }else{
        echo 
        '<div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert">×</button>
        <strong>Aviso!</strong>Não há Newsletter cadastrada com o id informado!
        </div>';
    }
}catch(PDOException $error){
echo $error;
}
$novoNome = $imagem;
//ATUALIZAR
if(isset($_POST['enviar'])){
    $titulo         = trim(strip_tags($_POST['titulo']));
    $data           = trim(strip_tags($_POST['data']));
    $descricao  = $_POST['descricao'];
    if(!empty($_FILES['img']['name'])){
        //INFO IMAGEM
        $file           = $_FILES['img'];
        $numFile        = count(array_filter($file['name']));
        //PASTA
        $folder     = '../upload/newsletter/';
        //REQUISITOS
        $permite    = array('image/jpeg', 'image/png');
        $maxSize    = 1024 * 1024 * 1;
        //MENSAGENS
        $msg            = array();
        $errorMsg   = array(
            1 => 'O arquivo no upload é maior do que o limite definido em upload_max_filesize no php.ini.',
            2 => 'O arquivo ultrapassa o limite de tamanho em MAX_FILE_SIZE que foi especificado no formulário HTML',
            3 => 'o upload do arquivo foi feito parcialmente',
            4 => 'Não foi feito o upload do arquivo'
        );
        if($numFile <= 0){
            /*echo '<div class="alert alert-danger">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            Selecione uma imagem e tente novamente!
            </div>';*/
        }
        else if($numFile >=2){
            echo '<div class="alert alert-danger">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            Você ultrapassou o limite de upload. Selecione apenas 1 imagem!
            </div>';
        }else{
            for($i = 0; $i < $numFile; $i++){
                $name   = $file['name'][$i];
                $type       = $file['type'][$i];
                $size       = $file['size'][$i];
                $error      = $file['error'][$i];
                $tmp        = $file['tmp_name'][$i];

                $extensao = @end(explode('.', $name));
                $novoNome = rand().".$extensao";

                if($error != 0)
                $msg[] = "<b>$name :</b> ".$errorMsg[$error];
                else if(!in_array($type, $permite))
                $msg[] = "<b>$name :</b> Erro imagem não suportada!";
                else if($size > $maxSize)
                $msg[] = "<b>$name :</b> Erro imagem ultrapassa o limite de 5MB";
                else{
                    if(move_uploaded_file($tmp, $folder.'/'.$novoNome)){
                        //$msg[] = "<b>$name :</b> Upload Realizado com Sucesso!";
                        $arquivo = "../upload/newsletter/".$imagem;
                        unlink($arquivo);
                    }else{
                    $msg[] = "<b>$name :</b> Desculpe! Ocorreu um erro...";
                    }
                }
                foreach($msg as $pop)
                    //echo $pop.'<br>';
            }
        }
    }
    else{
    $novoNome = $imagem;
    }

    $select = "SELECT * FROM usersnewsletter WHERE status = 1";
    try{
        $result = $conexao->prepare($select);
        $result->execute();
        $contar = $result->rowCount();
        if(!$contar>0){
            echo 
            '<div class="alert alert-danger">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <strong>Aviso!</strong>Não há usuários ativos!
            </div>';
        }else{
            while($show = $result->FETCH(PDO::FETCH_OBJ)){
                $idUser     = $show->id;    
                $name       = $show->name;  
                $sobrenome  = $show->sobrenome; 
                $mail       = $show->mail;  
                $token      = $show->token; 
                $status     = $show->status;    

                require_once('envia-email/PHPMailer/class.phpmailer.php');

                $Email = new PHPMailer();
                $Email->SetLanguage("br");
                $Email->IsSMTP();
                $Email->SMTPAuth = true;
                $Email->Host = '';
                $Email->Port = ''; 
                $Email->Username = ''; 
                $Email->Password = '';                  
                $Email->IsHTML(true); 
                $Email->From = '';              
                $Email->FromName = utf8_decode('');
                $Email->AddReplyTo($mail, '');
                $Email->AddAddress($mail); 
                $Email->Subject =  utf8_decode ($titulo);
                $url    = 'http://www.#####.#####.br/newsletter/cancel.php?token='.$token;
                $Email->Body .= 
                utf8_decode 
                ('
                    <!DOCTYPE html>
                    <html>
                            <head>
                                <style>
                                    table, th, td {
                                        border: 1px solid black;
                                        border-collapse: collapse;
                                    }
                                    th, td {
                                        padding: 5px;
                                        text-align: left;    
                                    }
                                    #logo{
                                        width: 250px;
                                        text-align: center;
                                        border-bottom: 1px solid white;
                                        padding: 20px;
                                    }
                                    #titulo{
                                        width: 70%;
                                        padding: 1% 3%;
                                        text-align: center;
                                        border-top: 1px solid white;
                                        border-bottom: 1px solid white;
                                        background: #009BD2;
                                    }
                                    h1{
                                    color: white;
                                    }
                                    #por{
                                        padding-left: 5%;
                                        border-top: 1px solid white;
                                        border-bottom: 1px solid white;
                                    }
                                    #imgprinc{
                                        text-align: center;
                                        width: 100%;
                                        padding: 3% 5%;                                                 
                                        border-top: 1px solid white;
                                        border-bottom: 1px solid white;
                                    }
                                    #conteudo{
                                        padding: 3% 5% 2%;
                                        text-align: justify;
                                    }
                                    #footer{
                                        background: #009BD2;
                                        color: white;
                                    }
                                </style>
                            </head>
                        <body>
                            <table style="width:100%">
                                <tr>
                                    <th style="border-top: 1px solid white; border-bottom: 1px solid white; border-left: 1px solid white;">    </th>
                                    <th style="width: 250px; text-align: center; border-bottom: 1px solid white; padding: 20px;" id="logo">                                     
                                        <img id="logo" src="http://www.#####.#####.br/blog/admin/img/logonews.png">
                                    </th>
                                    <th style="border-top: 1px solid white; border-bottom: 1px solid white; border-right: 1px solid white;">   </th>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid white; border-bottom: 1px solid white; border-left: 1px solid white;">   </td>
                                    <td style="width: 70%; padding: 1% 3%; text-align: center; border-top: 1px solid white; border-bottom: 1px solid white; background: #009BD2;" id="titulo">
                                        <h1 style=" background: #009BD2; color: white;">'.$titulo.'</h1>
                                    </td>
                                    <td style="border-top: 1px solid white; border-bottom: 1px solid white; border-right: 1px solid white;">   </td>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid white; border-bottom: 1px solid white; border-left: 1px solid white;">    </td>
                                    <td style="padding-left: 5%; border-top: 1px solid white; border-bottom: 1px solid white;"  id="por">
                                        <small>                     
                                            '.$data.' '.date('H:i').'<br>                                               
                                            Por <strong>'.$nameLogged.'
                                        </small>
                                    </td>
                                    <td style="border-top: 1px solid white; border-bottom: 1px solid white; border-right: 1px solid white;">    </td>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid white; border-bottom: 1px solid white; border-left: 1px solid white;">    </td>
                                    <td style=" text-align: center; width: 100%; padding: 3% 5%; border-top: 1px solid white;   border-bottom: 1px solid white;"  id="imgprinc">
                                        <img src="http://www.#####.####.br/blog/upload/newsletter/'.$novoNome.'">
                                    </td>
                                    <td style="border-top: 1px solid white; border-bottom: 1px solid white; border-right: 1px solid white;">   </td>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid white; border-bottom: 1px solid white; border-left: 1px solid white; ">   </td>
                                    <td style="padding: 3% 5% 2%; text-align: justify;" id="conteudo">
                                        '.$descricao.'
                                    </td>
                                    <td style="border-top: 1px solid white; border-bottom: 1px solid white; border-right: 1px solid white;">   </td>
                                </tr>
                                <tr>
                                    <td style="border-top: 1px solid white; border-bottom: 1px solid white; border-left: 1px solid white; border-right: 1px solid #009BD2;">    </td>
                                    <td style="text-align: center; background: #009BD2; color:white; border: 1px solid #009BD2; padding: 15px 0">
                                        <small>     
                                            <strong>#####</strong>  #####
                                                <br>
                                                #####
                                                <br>                    
                                                #####
                                                <br>
                                                #####
                                                <br>
                                                Se não deseja mais recebe-los, 
                                                <strong>
                                                    <a style="color: white;" href="'.$url.'" target="_blank">
                                                        clique aqui.
                                                    </a>
                                            </strong>
                                        </small>
                                    </td>                                       
                                    <td style="border-top: 1px solid white; border-bottom: 1px solid white; border-right: 1px solid white;">   </td>
                                </tr>
                            </table>
                        </body>
                    </html>
                ');
            }               
        }

        if(!$Email->Send()){        
            echo 
            '<div class="alert alert-danger">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>Erro!</strong> Houve um problema ao enviar o e-mail! para'.$mail.'
            </div>';
            echo "Erro: " . $Email->ErrorInfo;
        }else{
            echo 
            '<div class="alert alert-success">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>Sucesso!</strong> A Newsletter foi enviada! 
            </div>';
        }
    }catch(PDOException $error){
        echo $error;
    }
}

?>

  • 1

    The instruction that sends email is outside the repeat loop. So send only to the last.

No answers

Browser other questions tagged

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