我在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);
}
?>
解决方法:
由于每次迭代20秒睡眠睡眠(20),服务器可能会过载.
Reduce sleep time to few seconds like 2 or 3.
sleep(3)
Enable display errors, add this top of the script
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Log email completion status on log file because you are running a loop all echo data goes to buffer so nothing would get print until loop gets finished.
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);
}