一个简单的 PHP For Each 循环可以处理向 10,000 人发送电子邮件吗?

Can a simple PHP For Each loop handle sending email to 10,000?

我正在为每个循环使用 PHP 向 10,000 封电子邮件发送一封电子邮件。为什么这不是处理 10,000 封电子邮件的最佳做法?这个执行可以挂起吗?我想确保这会发送到所有电子邮件。

 foreach($thecustomer as $key => $value){
      $to = $customeremail; 
      $fromemail = $shopowner . " <" . $email . ">"; 
      $subject = $subject;
      $message = $body;
      $headers = "From:" . $email . "\n";
      $headers .= "MIME-Version: 1.0\n";
      $headers .= "Content-Type: text/html; charset=ISO-8859-1\n";
      mail($to,$subject,$message,$headers);
 }

是 php 这样做没有问题,但是请注意 mail() 将创建一个到邮件服务器的新连接,发送邮件,然后再次关闭连接,这意味着您还将打开和关闭连接一万次!而 PEAR::Mail and PEAR::Mail_Queue 软件包将在一个连接中完成所有操作,因此速度要快得多

引用 mail() 文档:

Note:
It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.
For the sending of large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.

还要确保你有足够的执行时间,你可以使用 set_time_limit(0); 到 "never timeout"