views:

40

answers:

1

i'm using drupal 6 and tries to send an email to all users by using following code . The problem is sometimes the mail sends to users multiple times but sent folder holds 1 entry for each user. Specially yahoo ids got more than 2 mails(3 to 4) and sent shows 2 emails sent to each yahoo id . is anyone knows it ..

<?php
 require("/var/www/drupal/sites/all/modules/smtp/phpmailer/class.phpmailer.php");
 $con = mysql_connect("***","***","*****");
 $select_db=mysql_select_db("drupaldb", $con);
 $result = mysql_query("select uid,name,mail,data from users ");
 while($row = mysql_fetch_array($result))
 {
   mailit($row['name'],$row['mail']);
   flush();
   sleep(3);
 }
 print "<br>*******************************************<br>";
 function mailit($name,$rec)
 {
   $content="<div> Test mail </div>";

   $mail = new PHPMailer();
   $mail->IsSMTP(); 
   $mail->Host = "ssl://smtp.gmail.com"; 
   $mail->Port = 465; 
   $mail->SMTPAuth = true; 
   $mail->Username = "**********"; 
   $mail->Password = "*********";
   $mail->From = "*********";
   $mail->FromName = "****";
   $mail->AddAddress($rec,$name);
   $mail->Subject = "Test mail";
   $mail->IsHTML(true);
   $mail->Body = $content;
   $mail->AltBody ='Test Mail'; 
   if(!$mail->Send())
   {
     error_log("Mass Mail: ".$mail->ErrorInfo, 0);
     echo "Mailer Error: " . $mail->ErrorInfo."<br>";
   }
   else
   {
     echo "Message has been sent to <b>".$name."</b> through <i>".$rec."</i><br>";
   }
 }
?>*
+1  A: 

Your code seems fine. Are you sure that different users don't share some email addresses? The mail column should be normalized and UNIQUE. Also, is the script being executed multiple times?

Alix Axel
No problem with email address and its executed only once.
Paniyar