phpMailer not adding multiple addresses when calling addAddress() in a loop
03:21 26 Apr 2016

I got an issue with phpMailer and it is not sending to multiple addresses when using an if statement. If I hardcode the emails they work fine...

P.s. I have also checked the variables and the if statements and all are working correctly.

$mail = new PHPMailer;

$mail->setFrom('x@x.com', 'xxx');
$mail->addAddress($row['recipient']); 
if ( $row['recipient2'] != '' ) { $mail->addAddress($row['recipient2']); }
if ( $row['recipient3'] != '' ) { $mail->addAddress($row['recipient3']); }
$mail->addAddress('x@x.com'); 
$mail->addReplyTo('x@x.com', 'xxx');

EDIT - NEW examples - bounty set:

I tried the following script which should give me 7 addresses however I only get one. (The hardcoded one)

$mail = new PHPMailer;

$mail->setFrom($from_email);

$mail->addAddress('test@test.com'); 

if ( $rows != 0 ) {
    for ( $i = 0; $i < $rows; $i ++ ) {
        $row = clean_fetch_assoc($result);
        $mail->addAddress("test2@test.com"); 
    }
}

var_dump($rows);
var_dump($mail->getAllRecipientAddresses());
var_dump($mail->isError());

die();

Which returns...

int 6

array (size=1)
  'test@test.com' => boolean true

boolean false

As you can see $rows returns 6 so there is nothing wrong with the loop.

Any ideas?

php loops email phpmailer