PHPMailer + Cron =重复的电子邮件

When I use the PHPMailer library, my script runs fine from CLI, but when I run it using cron, multiple emails get sent out. I have a list of addresses to send to, then for every next mail recipient, excluding the first one, it sends duplicates.

Example:

I send a message to 3 mail recipients every ten minutes

1st recipient received same email once.

2nd recipient received same email twice.

3rd recipient received same email three times.

4th recip. four times... etc..

The Code:
http://pastebin.com/XjtgEN8u

The crontab :

0,10,20,30,40,50 * * * * /etc/webmin/cluster-cron/cron.pl 1353486136-28420

The problem is that you're not creating a new PHPMailer object for each email you're sending. The result is that you're storing all the email addresses already sent to (in previous loops), and resending to them on all subsequent loops. That's why you get incremental duplication.

To fix the problem, put this line inside your while loop:

$mail = new PHPMailer(true);

That will instantiate a new mail object for each email address, and reset your email list to 0 before adding the new one.

In your script on line 38, you need to change the adding of recipient to setting the recipient... in each iteration you ADD new one while the previous remains set.

You can also remove the previous email addresses before send to a new one by using:

$mail->ClearAllRecipients();

then add another address.