-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?
– Will Knippelberg
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!
– Caio Lourençon
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
– Caio Lourençon