加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHPMailer挂起发送邮件

发布时间:2020-12-13 16:10:26 所属栏目:PHP教程 来源:网络整理
导读:我在centos服务器上使用 PHP,PHPmailer成功建立了一个Web应用程序,用于向客户发送批量电子邮件.客户电子邮件来自SQL数据库.它成功运行并发送一封电子邮件并休眠20秒,然后将此过程作为循环执行.该数据库有6000个电子邮件地址.在此过程中,服务器通过发送大约10
我在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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读