PHPMailer在发送大约175封电子邮件后随机停止发送邮件

时间:2012-05-04 20:18:18

标签: php email phpmailer

我正在使用PHPMailer从数据库中的电子邮件列表中发送新闻稿。该脚本查询数据库,将数据放入带有while循环的数组中,然后在while循环中创建并发送电子邮件。

在发送150封左右的电子邮件之后,它会正常工作,然后随机停止。例如,电子邮件爆炸#1在161封电子邮件发送后失败,电子邮件爆炸#2在165封电子邮件发送后失败,电子邮件爆炸#3在182封电子邮件发送后失败。

我一直在研究和研究,但找不到原因。现在我将每封电子邮件发送到To:消息,并且不要使用BCC:route发送它们。它可能是我服务器上的设置吗?如果是这样,我应该寻找什么?

我内置了一些调试。最后的sql脚本将电子邮件添加到数据库,以及发送的具体时间,以便我可以确切地告知发送了多少电子邮件,哪些电子邮件停止这个过程,需要多长时间。

它停止的电子邮件显示没有模式(意味着它们是不同的电子邮件提供商),只需要大约10到12分钟即可发送出那么多。我在服务器上的脚本超时远高于此,因此不是原因。

我的代码如下:

$mail = new PHPMailer();
$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->Host = "*server ip here*";  // specify main and backup server
$mail->SMTPKeepAlive = true;                  // SMTP connection will not close after each email sent
$mail->SMTPAuth = false;     // turn on SMTP authentication
$mail->Username = "*user*";  // SMTP username
$mail->Password = "*pass*"; // SMTP password

while($select2 = sqlsrv_fetch_array($select)) {
$email = $select2['email'];
$unid    = $select2['id'];
$unemail = $select2['email'];
$to = $email;
$from = "newsletter@site.com";
$new_date = date("n/j/y - g:i:s a");
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message2 = "<html><body>";
$message2 .= $message;
$message2 .= "</body></html>";

$mail->From = "newsletter@site.com";
$mail->FromName = "Newsletter";
$mail->AddAddress($to);
$mail->AddReplyTo("newsletter@site.com", "Website.com");

$mail->IsHTML(true);                                  // set email format to HTML

$mail->Subject = $subject;
$mail->Body    = $message2;

if(!$mail->Send()) {
    die("mail() Failed to send email to $to.");
    echo "<br><br>Mailer Error: " . $mail->ErrorInfo;
    exit;
} else {
    $make_sent_query = "INSERT INTO emailssent (email_signup_id, email, datesent) VALUES (?, ?, ?)";
    $params_sent = array($unid, $to, $new_date);
    $add_to_sent = sqlsrv_query($conn, $make_sent_query, $params_sent);
    if($add_to_sent) {
        echo "Message sent for email $to<br>";
    } else {
        die("Mail sent, but failed to insert into Sent Emails database.");
    }
}

$mail->ClearAddresses();  //clear addresses for next loop
}

3 个答案:

答案 0 :(得分:2)

这可能是由您的本地邮件服务器引起的吗?如果您在很短的时间内发送了150封电子邮件,可能会假设它是垃圾邮件,并在您达到一定金额后停止。

您可以在发送每封邮件后尝试使用sleep()减慢速度吗?如果这是原因,那么只需要一两秒即可......

您是否有理由避免使用BCC?您可以向多个收件人发送电子邮件,并将您发送的电子邮件数量减少到现在发送的一小部分,但这样做的代价是无法对每封电子邮件进行个性化。

答案 1 :(得分:0)

我认为这是超时问题。并且crontab可以解决您的问题http://en.wikipedia.org/wiki/Cron。您可以编写脚本来发送电子邮件,并设置crontab以每分钟运行此脚本。那你就没有超时。

答案 2 :(得分:0)

其中一个潜在原因可能是PHP超时脚本。每个脚本都运行有限的时间。如果你的脚本需要更多的时间,那么PHP将简单地杀死脚本。通常,这会导致错误日志中报告错误。检查您的apache错误日志消息 - 它们可能包含提示。