1
votes

I am using below code to send email to all users in the mysql database uing phpmailer. My problem is .. mail is not sending to all recipients but mail is sent to one or two recipients . Is there any problem in the code. Thanks

<?php 

        require_once('phpmailer/class.phpmailer.php');

    error_reporting( E_ALL & ~E_DEPRECATED & ~E_NOTICE & ~E_STRICT );
        $host="mysql.hostinger.in"; // Host name 
        $username="u831209167_user"; // Mysql username 
        $password="###"; // Mysql password 
        $db_name="u831209167_name"; // Database name 

        // Connect to server and select databse.
      $con=mysql_connect($host,$username,$password) or die("Can't connect to database!");
    mysql_select_db("u831209167_name");

    $str="SELECT email FROM `notification` ";
       $res=mysql_query($str,$con);
       while($row=mysql_fetch_array($res))
       {

        $mail = new PHPMailer();
        $mail->CharSet =  "utf-8";

        $mail->SMTPAuth = true;
        $mail->Username = "###@gmail.com";
        $mail->Password = "######";
        $mail->SMTPSecure = "ssl";  
        $mail->Host = "smtp.gmail.com";
        $mail->Port = "465";

        $mail->setFrom('#####@gmail.com', 'example');


        $mail->AddAddress($row[email]);

        $mail->Subject  =  '54450065';
        $mail->IsHTML(true);

    $hl = <<<EOL
    <h1>Welcome</h1>


    <p>Your username is {$row[email]} .</p>
    EOL;

        $mail->Body    = ($hl);






        if($mail->Send())
        {
            echo "Message was Successfully Send :)";
        }
        else
        {
            echo "Mail Error - >".$mail->ErrorInfo;
        }
        }   
    ?>
1
Possible duplicate of Problem with php mailerB001ᛦ
must take care of deprecated extensions, why are u using $mail = new PHPMailer(); inside the loop?devpro
what errors are u getting in Mail Error? and check two more points AddAddress and $mail->Body = ($hl);devpro
Did you forget the quotes: $row['email']dev0

1 Answers

0
votes

You need to call the AddAddress function once for each E-Mail address you want to send to. There are only two arguments for this function: recipient_email_address and recipient_name. The recipient name is optional and will not be used if not present.

$mail->AddAddress('[email protected]', 'First Name');
$mail->AddAddress('[email protected]', 'Second Name');
$mail->AddAddress('[email protected]', 'Third Name');

You could use an array to store the recipients and then use a for loop. I hope it helps.