首页 > 解决方案 > 通过 phpmailer 发送电子邮件的重定向过多

问题描述

我在数据库中有一个表,它对通过将数据插入该表的脚本发送的所有邮件进行排队和存储。

考虑到大量电子邮件,php 中的另一个脚本管理发送,以避免垃圾邮件,它以 50 封为一组发送。

它重复循环,直到所有元组都已发送。(每个带有排队或发送集的元组都有一列)

问题如下:

确切地说,鉴于提交的数量很大,脚本在某个点崩溃(chrome)并出现错误“ERR_TOO_MANY_REDIRECTS”。

我该如何解决?

<?php
    require_once '../dbh.inc.php';
    include_once '../functions.php';




    error_reporting(E_STRICT | E_ALL);

    date_default_timezone_set('Etc/UTC');

    require '../PHPMailer/PHPMailerAutoload.php';

    $mail = new PHPMailer;

    $mail-> isSMTP();
    $mail->Host = 'smtps.*****.it';
    $mail->SMTPDebug = 0;
    $mail->Port = 465;
    $mail->SMTPAuth = true;
    $mail->SMTPAutoTLS = false;
    $mail->SMTPSecure = 'ssl';
    $mail->Username = 'intranet@*****.it';
    $mail->Password = '*****';
    $mail->Priority = '1';
    $mail->setFrom('intranet@*****.it', '*****');




    $process_count = 50;

    $sql = "SELECT * FROM  `email_queue` WHERE `status` = 'queued' AND `do_not_send_before` < '" .strtotime("now") ."'
        ORDER BY `submission_date`, `priority` ASC LIMIT $process_count ";
    $result = mysqli_query($conn, $sql);

        foreach ($result as $row) {

        $mail->addAddress($row['recipient']);
            $mail->addReplyTo($row['reply_to']);
            $mail->IsHTML(true);
            $mail->CharSet = "UTF-8";
            $mail->Subject = $row['subject'];
            $mail->msgHTML($row['message']);
            $mail->AltBody = "";

            $file1 = $row['file1'];
            $file2 = $row['file2'];
            $file3 = $row['file3'];
            $file4 = $row['file4'];
            $file5 = $row['file5'];

            $filepath = '/web/htdocs/www.*****.it/home/it/intranet/uploads/mails/';

            if (!empty($file1)) {
            $file1 = $filepath.$file1;
            $mail->AddAttachment($file1);
        }
        if (!empty($file2)) {
            $file2 = $filepath.$file2;
            $mail->AddAttachment($file2);
        }
        if (!empty($file3)) {
            $file3 = $filepath.$file3;
            $mail->AddAttachment($file3);
        }
        if (!empty($file4)) {
            $file4 = $filepath.$file4;
            $mail->AddAttachment($file4);
        }

                if (!empty($file5)) {
            $file5 = $filepath.$file5;
            $mail->AddAttachment($file5);
        }

        if (!$mail->send()) {
            echo "Mailer Error (" . str_replace("@", "&#64;", $row["recipient"]) . ') ' . $mail->ErrorInfo . '<br />';
            break; //Abandon sending
        } else {
                $now = strtotime("now");
            //echo "Messaggio inviato a :" . ' (' . str_replace("@", "&#64;", $row['recipient']) . ')<br />';
            //Mark it as sent in the DB

                    $sql1 = "UPDATE `email_queue` SET `status` =  'sent', `sent_date` = '$now' WHERE `id` = '" . $row['id'] . "'";
                    $result = mysqli_query($conn, $sql1);
                    if ($result) {
                        echo "Mail Inviata correttamente a:  (" . str_replace("@", "&#64;", $row["recipient"]) . ') <br />';
                    } else {
                        echo "error $sql1 </br>";
                    }
        }
        // Clear all addresses and attachments for next loop
        $mail->clearAddresses();
        $mail->clearAttachments();

    }



    $sql2 = "SELECT COUNT(*) AS count FROM  `email_queue` WHERE `status` = 'queued';";
    $result2 = mysqli_query($conn, $sql2);
    $row2 = mysqli_fetch_assoc($result2);
    $count = $row2['count'];
    echo "$count";
    if ($count > 0) {
        header('Location: '.$_SERVER['PHP_SELF']);
    } else {
      header('Location:' . $USER_LOCATION . "index.php?emails=success");

    }




    ?>

标签: phpmysqlphpmailer

解决方案


在脚本结束时,您的else-if条件总是会导致重定向。如果行数大于 0,则将其重定向到相同的脚本,没有结束。它会导致永无止境的重定向循环。

在这种情况下不要重定向,或者重定向到另一个不会再次重定向到另一个(或相同)脚本的脚本。

伪代码:

if ($count > 0) {
  // do something here instead of redirect
} else {
  header('Location:' . $USER_LOCATION . "index.php?emails=success");
}

推荐阅读