0

Result Required : send mail to individual student. Result getting : It send 3 mail to test1 user. In first mail test1 get his mail id in "TO". In second mail test1 get his mail id & test2 mail id in "TO" In third mail test1 get his mail id & test2 & test3 mail id in "TO"

$d=date('Y-m-d');
$qu="select * from student";
$res=mysqli_query($con,$qu);
if($res && mysqli_num_rows($res)>0)
{
while($row=mysqli_fetch_assoc($res))
    {
            $e1=$row["stu_name"];
            $e2=$row["mailid"];
            $e3=$row["d_date"];
            if($e3 == $d)
                {
                        try 
                             {
                                    //Server settings
                                    $mail->SMTPDebug = 2;                                 
                                    $mail->isSMTP();                                     
                                    $mail->Host = 'xys.xyz.com';  
                                    $mail->SMTPAuth = true;                               
                                    $mail->Username = 'xyz@xyz.com';                 
                                    $mail->Password = 'abc';                         
                                    $mail->Port = 25;                                   
                                    
                                    $mail->setFrom('xyz@xyz.com', 'test');
                                    $mail->addAddress($e2);  
                                    $mail->isHTML(true);                                  
                                    $mail->Subject = "Test Mail";
                                    $mail->Body    = "Dear ".$e1.",</br>hello"; 
                                    $mail->send();
                                    echo 'Message has been sent';
                            } 
                       catch (Exception $e) 
                            {
                                    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
                            }   
             }
          
    }
 }


 DATABASE :
 S_no  stu_name   mailid              d_date
 1    test1      test1@gmail.com      2021-02-02    
 2    Test2      test2@gmail.com      2021-02-02    
 3    Test3      test3@gmail.com      2021-02-02    

 
                        
Shalini
  • 131
  • 1
  • 10
  • Please please please Do NOT use variables like $e1, $e2 etc as they are meaningless... The rule is - Name them what they are... i.e. $e1 should be $student_name or $studentName etc – TimBrownlaw Feb 02 '21 at 06:44
  • https://stackoverflow.com/a/59618700/2943403 , https://stackoverflow.com/a/39398916/2943403 – mickmackusa Feb 02 '21 at 06:47

1 Answers1

0

You may need to use setTo instead of addAddress

$mail->setTo([$e2]);

OR clear addresses before adding the address!

$mail->clearAddresses();
$mail->addAddress($e2);  

Also, you may move out some of the settings that don't need to be inside the loop! You should only keep the dynamic settings that change in the loop!

Lahar Shah
  • 7,032
  • 4
  • 31
  • 39
  • Actually any code that does not change should never be inside a loop. It's just very naughty and causes frowns upon those who view it. – TimBrownlaw Feb 02 '21 at 06:45
  • My page isn't working when I use setTo instead of addAddress. – Shalini Feb 02 '21 at 06:47
  • 1
    This page is sufficiently resolved by my 5 duplicates in the page closure. This mega-duplicate question should have been closed instead of answered. – mickmackusa Feb 02 '21 at 06:52