PHPMailer 在发送群发邮件时挂起

标签 php email centos phpmailer html-email

我已经成功地在 centos 服务器上使用 PHP、PHPmailer 设置了一个网络应用程序,用于向客户发送批量电子邮件。客户电子邮件从 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 秒 sleep sleep(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);
}

关于PHPMailer 在发送群发邮件时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49401487/

相关文章:

JavaMail、IMAP、大量文件夹的性能

php - 尝试用 php 发送 mysql 信息

PHP - 需要澄清 is_int() 和 while 循环

php - WordPress 元标记挑战

php - MySQL选择查询内部连接表,有2个针对不同表的正则表达式

css - HTML 电子邮件中的文本垂直居中

PHP json_decode 不起作用

.htaccess - 无法在 htaccess 或 robots.txt 中阻止 AspiegelBot

python - CentOS上用python模块安装OpenCV出错

php - 如何让PHP能够读取系统环境变量