php - 通过 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("@", "@", $row["recipient"]) . ') ' . $mail->ErrorInfo . '<br />';
break; //Abandon sending
} else {
$now = strtotime("now");
//echo "Messaggio inviato a :" . ' (' . str_replace("@", "@", $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("@", "@", $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");
}
?>
解决方案
在脚本结束时,您的else-if
条件总是会导致重定向。如果行数大于 0,则将其重定向到相同的脚本,没有结束。它会导致永无止境的重定向循环。
在这种情况下不要重定向,或者重定向到另一个不会再次重定向到另一个(或相同)脚本的脚本。
伪代码:
if ($count > 0) {
// do something here instead of redirect
} else {
header('Location:' . $USER_LOCATION . "index.php?emails=success");
}
推荐阅读
- postgresql - 与 UPDATE 规则相比,postgres INSERT 规则中生成的 UUID 行为
- javascript - 即使哈希相同,有没有办法触发“哈希更改”事件?
- python - 计算距离,但遇到类型错误
- gatsby - 如何通过 gatsby-source-sanity 使用 sanity.io 和 gatsby 中的内部链接?
- python - 如何继续使用“MORE”这个词到达命令的末尾
- r - R:如何使用多个过滤器绘制折线图。(R-闪亮 / flexdashboard)
- html - 从 Google Chrome 到 Microsoft Edge,HTML 表格上方和下方的空间差异很大
- sql - 如何在 SQL 中使用 CASE 和 LIKE 进行 INNER JOIN
- java - 是否可以在不换行的情况下为 Java 中的每个 pcollection 编写代码?
- python - 使用 Python 遍历嵌套的 JSON 数据