当我使用PHPmailer发送到一组电子邮件地址时,它会陷入循环并不断重复发送电子邮件(100次或更多次)。但是,它并不总是这样做。我可以发送到10或12个地址,它工作正常。但是更大的列表(25个地址)似乎有麻烦。我确实在我的列表中找到了一些不存在的电子邮件地址,这些电子邮件地址反弹回我的"来自"地址邮箱。所以我认为这可能是问题所在。但是,我只用3个地址(一个不存在)运行测试,它按预期工作(好的地址收到了邮件,坏的地址被反弹回发件人邮箱)。没有发送重复的电子邮件。
我确实使用BCC发送电子邮件,但我认为这不重要。我想知道是否可以为PHPmailer设置一些参数,告诉它不要重新发送任何电子邮件。我的网站托管网站说它不是他们的服务器或邮件系统发送这些电子邮件,所以我猜测它是PHPmailer中的东西。
当我对PHPmailer的实际使用并没有像我测试时那样工作时,这使得修复非常困难。任何人都有任何建议,为什么我的电子邮件多次被发送?
此外,我想知道在发出$ mail-> Send()之后是否有办法停止PHPmailer?我对我的电子邮件的垃圾邮件行为感到有点尴尬(我的用户也生气了)。
我的基本代码如下。但是,PHP程序代码在它工作但不起作用的情况下是相同的;它只是改变的输入数据。当它工作时,有2个较小的阵列:
emailistAry(1)= array(email1,email2,email3,... email10)和
emailistAry(2)= array(email1,email2,email3,email4,email5)。
当它保持循环时,有2个更大的数组:
emailistAry(1)= array(email1,email2,bademail1,email3,email4,bademail2,email5,... email25)和emailistAry(2)= array(mail26,email27,email28,bademail3,email29,... email50)。
我还没有找到可以成功处理的确切数字,因为这意味着运行的测试可能会通过无数多封电子邮件填充人们的邮箱。没有人想要那样。
这是我的代码:
<?php session_start();
// uses arrays of addresses to send emails to multiple people (with BCC)
require '../PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<link href="../root.css" rel="stylesheet" type="text/CSS"/>
<title> Website Notification </title>
<meta http-equiv= "content-type" content="text/html; charset=iso-8859-1"/>
</head>
<body>
<div style="top:0;padding:0;width:850px;margin-left:70px;">
<?php
//$emailistAry is gathered from addresses in a database.
// it contains 2 arrays of email arrays (multi-dimensional).
//$emailist_cnt is determined from building above array (currently a 2)
$fromaddr = "myaddress@mywebsite.org";
$toaddr = "groupaddress@mywebsite.com";
$rplyaddr = "my2address@gmail.com";
$subject = "--Special Notice--";
$messageH = "<p style='font-family:Arial,Helvetica,Georgia,\"Times New Roman\";font-size:90%;'>
<span style='font-size:120%;color:#336699;'>Greetings HLCA Member!</span> <br/><br/>".
"This is my message text....".
"</p>";
// Set mail environment variables
$mail->isSMTP();
$mail->Host = 'smtp.mywebsite.org';
$mail->SMTPAuth = true;
$mail->Username = $fromaddr;
$mail->Password = "mypasswd";
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Set mail details that remain constant for every recipient
$mail->SetFrom($fromaddr); // mailbox you want email to come from
$mail->AddAddress($toaddr); // set recipient group (empty mailbox)
$mail->AddReplyTo($rplyaddr); // where reject msgs are sent
$mail->Subject = make_goodemail($nly,'subj',$subject);
$mail->Body = $messageH;
$goodlist_cnt = $emailist_cnt;
$max_emails = 25; // max num emails per list
$bcc_out_total = 0; //number emails output with BCC
$badlist_cnt = 0;
$badlist = ''; // save emails that didn't get sent
$email_badlist = array(); // store email lists that didn't get sent
$duplist = "";
$stop_err = false;
$rcN = 0;
// Loop through email arrays and send one at a time (each contains an array of emails).
// (Note that we do not use the 0 array positions. First entries are in position 1.)
for ($i=1; $i <= $emailist_cnt; $i+=1) {
echo "<p style='margin-top:40px;font-size:120%;'><b> Processing list # ".$i."...</b></p>";
$duplist = "";
for ($j=1; $j <= $max_emails; $j+=1) {
// add addresses from this list array to BCC parameter,
if ($emailistAry[$i][$j] != '') {
// add current email to BCC for this list (save rc)
$BCCresult = $mail->AddBCC($emailistAry[$i][$j]);
if(!$BCCresult) {
$duplist = $duplist.$emailistAry[$i][$j].", ";
}
}
else {
$j = $max_emails; //set as end of list
}
} // for j loop
// Loop thru this BCC array and show list on screen.
$bccArray = $mail->GetBCCAddresses();
$bcc_out = 0;
echo "<br/>Mail BCC list: ";
for ($k=0;$k<=$max_emails;$k+=1) {
if ($bccArray[$k][0] != '') {
if ($k==0) { //first one
echo " ".$bccArray[$k][0]; } //no comma
else {
echo ", ".$bccArray[$k][0]; }
$bcc_out += 1;
}
else {
$k=$max_emails;
}
}
// $bcc_out_total = $bcc_out_total + $bcc_out;
echo "<br/>BCC count = ".$bcc_out;
if ($duplist != "") { //print duplicate list
echo "<br/> >>Duplicate or invalid emails not included: ".$duplist;
}
//echo "<br/><br/>would send email list here. webpass=".$webpass;
// ********** *********** ************* ************
// SEND THE MAIL with current BCC list of recipients.
// Output message indicating if mail for this group was successfully or not.
if (!$mail->Send()) { // Send mail, else save emailist.
// see if connection error, and process accordingly
$mail_err = $mail->ErrorInfo;
$pos = strpos($mail_err,'SMTP connect() failed');
if ($pos===true || $pos >= 0) {
$addmsg = "(Probably a password error. Try again.) <br/>";
if ($i == 1) { // connect error on first try, so
$stop_err = true; // stop processing cuz this error will continue
$rcN = 2;
}
}
else {
$addmsg = "";
$rcN = 1; //set general error for return
}
echo "<h4 style='color:red;margin-top:20px;margin-bottom:5px;text-align:center;'>
Problem sending emails. ".$addmsg." Mailer Error: ".$mail->ErrorInfo."</h4>";
echo "<h4 style='color:blue;margin-top:10px;margin-left:20px;margin-bottom:0px;'>Email List #".$i." not sent. </h4>";
if (!$stop_err) { //other error, so handle and keep going.
$goodlist_cnt = $goodlist_cnt - 1;
//save list in array (as one long string).
$badlist_cnt+=1;
$email_badlist[$badlist_cnt] = implode(",",$emailistAry[$i]); //save as a string
}
} //if !Mail-send
else { // Successful email
$bcc_out_total = $bcc_out_total + $bcc_out;
echo "<h4 style='color:blue;margin-top:20px;font-weight:bold;'>
Email group ".$i." sent successfully! </h4>";
}
// Clear cumulative recipient list for next loop
$mail->clearBCCs();
if ($stop_err) {
$i = $emailist_cnt; //set to last list so loop will stop
}
} // for i loop
if ($badlist_cnt > 0) { // error, but not stop_err, so lists are saved elsewhere.
$badlist_txt = "<span style='color:red;font-size:80%;'> (".$badlist_cnt." list(s) not sent)</span>";
//write badlists out to file for fixing and later processing
include ('includes/memNotifyErrSave.php');
}
else {
$badlist_txt = '';
}
if ($stop_err) {
echo "<br/> <h4 style='color:red;margin-top:50px;text-align:center;'> Processing stopped due to serious error! ".
"If password error, try again with correct password.<br/><br/>".
"<span style='font-size:120%;color:blue;font-weight:bold;'>* No emails were sent * </span></h4>";
}
else {
echo "<h4 style='color:blue;font-size:125%;font-weight:bold;margin-top:40px;'>
Sent this notice to ".$bcc_out_total." unique address(es), using ".$goodlist_cnt." email(s)".$badlist_txt.".</h4>";
}
// code for exit/return buttons
// Clear remaining cumulative lists
$mail->clearReplyTos();
$mail->clearAddresses();
?>
</div>
</body>
</html>