Skip to content
Advertisement

send email to multiple recipient don’t work

I try to send email to multiple recipient but doesn’t work. It work when I only send it to one recipient. I put the email address in an array variable.

I put the email I get from database in array variable $dt. Here is the $dt contain

  array(3) { [0]=> string(21) "a@gmail.com" [1]=> string(21) "b@gmail.com" [2]=> string(24) "c@gmail.com" }

I’ve try this one

   //get email from database
   $mail = $this->Hire_model->get_email_recruiter();
      $dt = array();
      for ($i=0; $i < (count($mail)-1) ; $i++) { 
        if ($mail[$i+1]['Email'] == '') {
          $dt[$i] = 'hr.movement@gmail.com';
        }else{
          $dt[$i] = $mail[$i+1]['Email'];
        }       
      }

    //Load PHPMailer Library
     $this->load->library('phpmailer_lib');

     //PHPMailer object
     $mail = $this->phpmailer_lib->load();

     //SMTP configuration
     $mail->isSMTP();
     $mail->Host = 'smtp.gmail.com';
     $mail->SMTPAuth = true;
     $mail->Username = 'hire.movement@gmail.com';
     $mail->Password = '********';
     $mail->SMTPSecure = 'tls';
     $mail->Port = 587;

     $mail->setFrom('info@codexworld.com', 'KompasGramedia');
     $mail->addReplyTo('info@example.com', 'KompasGramedia');

     //Add a recipient
     $mail->addAddress($dt);

     // Email Subject
     $requestor = $this->session->userdata('FullName');
           $mail->Subject = 'Request Promotion by '.$requestor;

     // Set email format to HTML
     $mail->isHTML(true);

     $mailContent = 'Messages';
     $mail->Body = $mailContent;
      // Send email
     if(!$mail->send()){
       echo 'Message could not be sent.';
       echo 'Mailer Error: ' . $mail->ErrorInfo;
     }else{
       echo 'Message has been sent';
     }

I don’t know why its not work when I using more than 1 email recipient. Can anyone help me??

Advertisement

Answer

Try to add each mail recipient inside a loop, like this :

   //get email from database
   $mail = $this->Hire_model->get_email_recruiter();
      $dt = array();
      for ($i=0; $i < (count($mail)-1) ; $i++) { 
        if ($mail[$i+1]['Email'] == '') {
          $dt[$i] = 'hr.movement@gmail.com';
        }else{
          $dt[$i] = $mail[$i+1]['Email'];
        }       
      }

    //Load PHPMailer Library
     $this->load->library('phpmailer_lib');

     //PHPMailer object
     $mail = $this->phpmailer_lib->load();

     //SMTP configuration
     $mail->isSMTP();
     $mail->Host = 'smtp.gmail.com';
     $mail->SMTPAuth = true;
     $mail->Username = 'hire.movement@gmail.com';
     $mail->Password = '********';
     $mail->SMTPSecure = 'tls';
     $mail->Port = 587;

     $mail->setFrom('info@codexworld.com', 'KompasGramedia');
     $mail->addReplyTo('info@example.com', 'KompasGramedia');

     //Add a recipient(s)
     foreach($dt as $recipient) {
         $mail->addAddress($recipient);
     }

     // Email Subject
     $requestor = $this->session->userdata('FullName');
           $mail->Subject = 'Request Promotion by '.$requestor;

     // Set email format to HTML
     $mail->isHTML(true);

     $mailContent = 'Messages';
     $mail->Body = $mailContent;
      // Send email
     if(!$mail->send()){
       echo 'Message could not be sent.';
       echo 'Mailer Error: ' . $mail->ErrorInfo;
     }else{
       echo 'Message has been sent';
     }
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement