0

My request form is taking to much time to submit. Also sometimes the emails are repeated and in error log I can see this PHP Warning: mail() [<a href='function.mail'>function.mail</a>]: Could not execute mail delivery program '/usr/sbin/sendmail -t -i'
Here is my code:

    $emails = mysql_query('select * from owners where status ="active"');

    while($row = mysql_fetch_assoc($emails)){

        $to = $row['s_email'];
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: mysitename  <noreply@mysitename.com' . "\r\n";
        mail($to, $subject, $message, $headers);
    }
Sergey Weiss
  • 5,944
  • 8
  • 31
  • 40

1 Answers1

1

To send an email to multiple recipients, separate each email address by a comma. Instead of a loop, you can do this in one line with the implode() function.

To answer your question, it could be a memory leak. Try my solution replacing your code with mine:

$emails = $pdo->prepare("SELECT * FROM owners WHERE status = 'active'");
$emails->execute()->fetchAll();

mail(implode(', ', $emails), $subject, $message, $headers);

Also, please avoid using mysql_* functions. They're deprecated. Try PDO instead. More information can be found here.


You may also want to look into cron jobs. Emails sent with the mail() function are often automatically placed in the spam folder.

Community
  • 1
  • 1
Terry Harvey
  • 840
  • 6
  • 12