我在centos服务器上使用PHP,PHPmailer成功设置了一个Web应用程序,用于向客户发送批量电子邮件。客户电子邮件来自SQL数据库。它成功运行并发送一封电子邮件并休眠20秒,然后将此过程作为循环执行。该数据库有6000个电子邮件地址。在此过程中,服务器通过发送大约100封电子邮件来挂起。所以我必须再次运行这个程序。 这为何挂起? 我没有得到PHP错误或PHP超时。
这是我的代码:`
<?php
require 'PHPMailerAutoload.php';
$con = mysql_connect("localhost", "root", "test");
mysql_select_db("user", $con);
$query = "select email from client_detail";
$result = mysql_query($query, $con);
$email = array();
while ($row = mysql_fetch_assoc($result)) {
$email[] = $row['email'];
}
foreach ($email as $to) {
$mail = new PHPMailer;
$mail->setFrom('bestweb@nic.lk');
$mail->addAddress($to);
$mail->Subject = 'Bestweb2018';
$mail->isHTML(true);
$mail->Body = '<html>
<head>
<title>BestWeb.lk 2018</title>
</head>
<body>
<table style="width: 760px;" >
<tr>
<td>
<img src="cid:banner" alt="bestweb.lk 2018" width="760px" height="167px" />
</td>
</tr>
</table>
</body>
</html>
';
$mail->AddEmbeddedImage('images/bannergold.gif', 'banner');
if (!$mail->send()) {
echo 'Message was not sent ' . $to;
echo "<br>";
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent ' . $to;
echo "<br>";
}
sleep(20);
}
?>
答案 0 :(得分:1)
由于每次迭代的睡眠时间为20秒sleep(20)
,服务器可能会过载。
将睡眠时间减少到几秒钟,如2或3。
sleep(3)
启用显示错误,添加脚本的顶部
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
在日志文件上记录电子邮件完成状态,因为您正在运行循环,所有回显数据都会转到缓冲区,因此在循环结束之前不会有任何内容打印。
if (!$mail->send()) {
@file_put_contents('email-logs.txt', 'Message was not sent ' . $to ." - error :" . var_export( $mail->ErrorInfo, true) . "\n", FILE_APPEND);
} else {
@file_put_contents('email-logs.txt', 'Message has been sent ' . $to . "\n", FILE_APPEND);
}