0

I am trying to send emails to multiple recipients.

So far, I was able to retrieve the email addresses in an array. But the mail function somehow does not accept the email addresses. Instead, it gives an error that it needs at least one email address to send an email, and it displays the emails inside the array.

Have a look at my code below.

$email = "select email from student"; 


if ($p_address=mysql_query($email))
{
$address = array();

while($row = mysql_fetch_array($p_address))
{     
$address[] = $row;
}  

for ($i = 0; $i < count($address); $i++) {
$all_address[] = implode(';', $address[$i]);
}

foreach ($all_address as $aa) {

$mail->AddAddress($aa);
}

Hope that you could help. Thanks.

2 Answers 2

1

Not sure what all that other code is for but something like this should be fine:

if ($p_address=mysql_query($email))
{ 
  while($row = mysql_fetch_array($p_address))
  {     
    $mail->AddAddress($row[0]);
  }  
}
4
  • I tried your code and it gives an error: Notice: Undefined index: email in ... $mail->AddAddress($row['email']);
    – banana
    May 6, 2013 at 7:28
  • I tried by sending 2 emails, and it works. But when I checked on the emails, both of the 2 addresses received 2 emails.
    – banana
    May 6, 2013 at 7:41
  • 2 rows, as I have queried 2 addresses.
    – banana
    May 6, 2013 at 8:21
0

it seems you have one redundant array - not sure why you need to implode addresses ...

$email = "select email from student"; 


if ($p_address=mysql_query($email))
{
$address = array();

while($row = mysql_fetch_array($p_address))
{     
$address[] = $row;
}  

foreach ($address as $aa) {

$mail->AddAddress($aa);
}
2
  • Thanks. I tried you code but it gives me this error: Warning: trim() expects parameter 1 to be string, array given in ....\phpmailer\class.phpmailer.php
    – banana
    May 6, 2013 at 7:23
  • 2
    @user2352236 try to check what is in the address[] please
    – user2262738
    May 6, 2013 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.