php - Sending email to registered members without repetation -


i trying send emails registered users in website, script works fine, there 1 problem, repeat sending emails.

for example: have 3 registered users in database (mysql), php script, first, selects email address, sends message it.

the problem comes here: 1- selects email 1 , send message (create!). 2- selects email 2 beside email 1 and, then, sends message both of them. 3- selects email 3 , send message 3 emails (1,2 , 3).

here code:

require 'mail/phpmailerautoload.php'; $mail = new phpmailer; $limit=3; $active="yes"; $stmt = $conn->prepare('select email,name users active=? limit ?'); $stmt->bind_param('si', $active,$limit); $stmt->execute(); $i=0; $stmt->bind_result($email,$name); $arr = array(); while($stmt->fetch()) { $row = array(); array_push($arr, $row); include "compose_mail.php";} 

the compose_mail.php

$mail->issmtp();   $mail->charset = 'utf-8';        // set mailer      use smtp $mail->host = 'the host';  // specify main , backup smtp servers $mail->smtpauth = true;                               // enable smtp      authentication $mail->username = 'username';       // smtp username $mail->password = 'password';       // smtp password $mail->smtpsecure = 'ssl';         // enable tls encryption, `ssl` accepted $mail->port = 465;            // tcp port connect   $mail->from = 'email'; $mail->fromname = 'websit name'; $mail->addaddress($email);     // add recipient $mail->ishtml(true);        // set email format  html  $mail->subject = $title; $mail->body    = ' html message';  if(!$mail->send()) {} else {} 

your while loop doing unwanted things:

$stmt->bind_result($email,$name); $arr = array(); while($stmt->fetch()) { $row = array(); array_push($arr, $row); include "compose_mail.php";} 

the include "compose_mail.php"; gets included every loop, after 3 loops have 3 includes of compose_mail.php.

include compose_mail.php before loop , change compose_mail.php have function call in it:

function sendmail(your parameters) {    mail code; } 

then call function in while loop. has clean parameters every time , repeated send should stop.

the total structure become like:

require 'mail/phpmailerautoload.php'; include "compose_mail.php";  $mail = new phpmailer; $limit=3; $active="yes"; $stmt = $conn->prepare('select email,name users active=? limit ?'); $stmt->bind_param('si', $active,$limit); $stmt->execute(); $i=0; $stmt->bind_result($email,$name);  while($stmt->fetch()) {   $mail = new phpmailer;   $arr = array();   $row = array();   array_push($arr, $row);   sendmail($mail,$arr,$row); } 

that way parameters reset every loop, multiple mail should impossible.


Comments