How to send the respective messages to emails coming from the php database?

Asked

Viewed 33 times

-2

WARNING: HICCUP AT THE END OF THE POST

I’m finalizing a project however I’m stuck in the following situation! I’m making a Newsletter system where the database has three fields, the permission to send, the email field, and one with unique cancellation code for each registration! When sending multiple recipients with PHP Mailer even works, it sends to all of While normally, however the cancellation code of the respective email is not going right, only the first loop code is being sent to all!

follows code

 $check_newsletter =$mysqli->query("SELECT email,desassinar_code FROM newsletter WHERE permissao ='sim'");

 include_once("inc/phpmailer/PHPMailerAutoload.php");

                $mail = new PHPMailer;
                $mail->CharSet = "UTF-8";
                $mail->isSMTP();
                $mail->SMTPAuth = true;
                $mail->Host = "$host_config";
                $mail->Port = "$port_config";
                $mail->SMTPSecure = "tls";
                $mail->Username = "$email_config";
                $mail->Password = "$senha_config";
                $mail->SetFrom ($email_config,$title_web);
                $mail->addReplyTo('[email protected]');
                if($title_web!=''){
                $mail->FromName = "$title";
                }                    
                $mail->Subject=$titulo;     
                $mail->WordWrap=50;
                $mail->IsHTML(true);


            while($n_p=$check_newsletter->fetch_array()){ 

              $body = "Para deixar de receber esse e-mails, cancele sua inscrição <a href='".url."/cancel-newsletter/".$n_p['desassinar_code']."'><strong style='color:#3C8FDC;'>clicando aqui</strong>";
              $mail->msgHTML($body);  
              $mail->AddBCC($n_p['email']);  

        }



        if($mail->Send()){           
            ...
            }else{
               ...    
            }   

That’s it, guys, we’re missing several pieces of code that I don’t think are in question, thank you all.

OBS:I’ve tried to change some positions, like by "$mail->msgHTML($body)", out of the loop, up, or down and nothing...

SOLUTION

Doing a search on other topics and testing I managed to resolve by changing the loop (while) to:

    while($n_p=$check_newsletter->fetch_array()){   
              $mail->clearAllRecipients();  // Não esqueça isso 
              $mail->addAddress($n_p['pca_email']); // addAddress era addBBC  
              $mail->Body = "Para deixar de receber esse e-mails, cancele sua inscrição <a href='".url."/cancel-newsletter/".$n_p['pca_desassinar_code']."'><strong style='color:#3C8FDC;'>clicando aqui</strong>";                                 
              $sucess_news = $mail->Send(); 

}


    O método "clearAllRecipients();" É muito importante, pois ele limpará o destinatário da última volta para que a seção 'para' do email não mostre todos os destinatários do boletim.             


  • all emails are valid email formats?

  • Yes, I have a whole administrative system operating and managing these details, alias, the code is sending the emails and arriving at the inbox normally, what I needed was the cancellation code I put in while "bd $n_p['disassemr_code']" also arrived in each of the respective emails that are in the loop, but only the first code is coming in all, the reading is repeating the first code for all recipients!

  • Note that in the "$body" there is a cancellation code for each one! but only the first bd code goes to all very strange recipients, until it seems that I’m echo out of the repeat loop

1 answer

1


Hello, actually you’re not giving the echo out of the loop, but sending only the last loop code. The code of $mail->Send() and its respective IF must be inside the while loop. In reality it is only going to ALL because you add the BCC inside the loop, the correct would be every loop of the loop you generate a new email by restarting the OBJECT, imagine that this loop has 500 emails, the first user will receive 500 copies of the same email. the correct would be:

    $check_newsletter =$mysqli->query("SELECT email,desassinar_code FROM newsletter WHERE permissao ='sim'");
include_once("inc/phpmailer/PHPMailerAutoload.php");
while($n_p=$check_newsletter->fetch_array()){ 
$body = "Para deixar de receber esse e-mails, cancele sua inscrição <a href='".url."/cancel-newsletter/".$n_p['desassinar_code']."'><strong style='color:#3C8FDC;'>clicando aqui</strong>";
    $mail = new PHPMailer;
    $mail->CharSet = "UTF-8";
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->Host = "$host_config";
    $mail->Port = "$port_config";
    $mail->SMTPSecure = "tls";
    $mail->Username = "$email_config";
    $mail->Password = "$senha_config";
    $mail->SetFrom ($email_config,$title_web);
    $mail->addReplyTo('[email protected]');
    if($title_web!=''){
        $mail->FromName = "$title";
    }                    
    $mail->Subject=$titulo;     
    $mail->WordWrap=50;
    $mail->IsHTML(true);
    $mail->msgHTML($body);  
    $mail->AddBCC($n_p['email']);  
    if($mail->Send()){           
        ...
    }else{
        ...    
    }
}
  • Hello tested your solution and it worked cool, before gave apply there I did a deep search on other topics and also arrived at another solution that worked! I think it is better this way according to this documentation: "https://www.greenarrowemail.com/docs/greenarrow-engine/Injecting-Mail/SimpleMH-Injection/PHPMailer-SimpleMH-Multiple-Recipient-Example" it is not necessary to restart the object, the loop is done in the same place, I think the link way avoids a memory overload in view of multiple registrations. Basically added clearAllRecipients(); and $mail->send(); inside the loop

  • Soon I will edit the post by adding the solution to the topic!

  • 1

    Exact! In fact, I did not look for the documentation of Phpmailer, but I only considered the code that was in the question, actually reading the posted documentation, it is more performative, memory few bytes around, since the object is being rewritten, and not creating a new instance of it.

  • Edited! Thank you.

Browser other questions tagged

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