Search code examples
phpphpmailer

Send Bulk Mail Using PHPMailer


Need to send mail for 4 to 5 emails one after another so used for loop and I'm trying to trigger email using PHPMAILER. However only for first email address mail is sent and for remaining its showing error.

NOTE: EVEN TRIED $mail->clearAddresses()

Below is my mail code

      <?php
      require 'Send_Mail.php';
      $var = array("[email protected]","[email protected]","[email protected]");

      for($i=0;$i<$var;$i++)
      {
       $to = $var[$i];
       $subject = "Bulk Mail";
       $body = "dummy loop";
       $op = Send_Mail($to,$subject,$body);
      }
    ?>

Below is my PHPMAILER CODE

        function Send_Mail($to,$subject,$body)
        {
         require 'class.phpmailer.php';
         $from = "[email protected]";
         $mail = new PHPMailer();
         $mail->ClearAllRecipients();
         $mail->IsSMTP(true); // SMTP
         $mail->SMTPAuth   = true;  // SMTP authentication
         $mail->Mailer = "smtp";
         $mail->Host       = "tls://smtp.gmail.com"; // Amazon SES server, note "tls://" protocol tls://email-smtp.us-west-2.amazonaws.com
         $mail->Port       = 465;                    // set the SMTP port
         $mail->Username   = "[email protected]";  // SES SMTP  username
         $mail->Password   = "XYZ";  // SES SMTP password
         $mail->SetFrom($from, '[email protected]');
         $mail->AddReplyTo($from,'[email protected]');
         $mail->Subject = $subject;
         $mail->MsgHTML($body);
         $mail->clearAllRecipients();
         $address = $to;
         $mail->AddAddress($address, $to);
         if(!$mail->send()) {
            $mail->clearAddresses();
            return false;    
         }
         else
        {
          $mail->clearAddresses();
          return true;
        }
     }

what is error in my code..??


Solution

  • try changing

     for($i=0;$i<$var;$i++)
    

    as it is a endless loop to

    for($i=0;$i<count($var);$i++)
    

    or do a foreach

    foreach($var as $to)