Author Topic: phpmailer sending duplicates  (Read 2386 times)

0 Members and 1 Guest are viewing this topic.

Offline devofashTopic starter

  • Enthusiast
  • Posts: 127
    • View Profile
phpmailer sending duplicates
« on: February 26, 2010, 04:39:49 AM »
Hi Guys,

Need some help. I'm using phpmailer (for php4) i'm reading a textfile with about 200 email addresses and i'm sending 20 emails every 5 seconds. Works perfectly. But got a major problem, a lot of clients have comeback to me and said that they've received the same copy multiple times.

I can't figure out why its doing this can someone please help me resolve this ??

Many thanks.

<?php
    error_reporting
(0);             
    
set_time_limit(0); 
    include_once(
'phpmailer/class.phpmailer.php');
        
    
$body 'This is a test';

    
$mail             = new PHPMailer();
    
$mail->IsSMTP(); 
    
$mail->Host       "";
    
$mail->From       "Me";
    
$mail->FromName   "FromName";
    
$mail->Subject    "Subject";
    
$mail->AltBody    "To view the message, please use an HTML compatible email viewer!";
    
$mail->MsgHTML($body);
   
    
$myFile "listmanagement/uploads/34_250210_1267104673.txt";
    
$data file("$myFile");
    foreach(
$data as $value) { 
        
$v trim($value);        
        
$emails .= "$v,"
    }

    
$email_addrs explode(","$emails);

    
$throttle 20;
    
$i 0;
          
    
// Now, run a loop through all the email addresses in the mailing list.
    
foreach($email_addrs as $email_addr)
    {
        if (
$i $throttle == 0)
        {
            
sleep(5);  
        }
        
$mail->AddBCC($email_addr);
        
$mail->Send();
        
$mail->ClearBCCs();
        
$i++;
    }
    
    
$mail->AddAddress("me@me.com""Company");  
    if(!
$mail->Send()) 
    {
        echo 
"Mailer Error: " $mail->ErrorInfo;
    } 
    else 
    {
        echo 
"Message sent!";
    }
?> 

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: phpmailer sending duplicates
« Reply #1 on: February 26, 2010, 07:55:13 AM »
Are you only receiving one copy at the "me@me.com" address? Have you examined the list stored in the file to make sure it does not contain duplicates for those members that have indicated they are receiving duplicates?
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline devofashTopic starter

  • Enthusiast
  • Posts: 127
    • View Profile
Re: phpmailer sending duplicates
« Reply #2 on: February 26, 2010, 08:35:10 AM »
Hi Thanks you for the reply.

The list is clean, no duplicates
Yes I'm receiving a few copies too.

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: phpmailer sending duplicates
« Reply #3 on: February 26, 2010, 08:47:03 AM »
Quote
Yes I'm receiving a few copies too.

Then it is likely your browser is requesting the page twice. Different browsers do  this for different reasons and you can also have some url rewriting that is causing it. FF is the biggest offender because some of the add-on debugging tools cause a page to be requested twice and it used to (I think this bug has been fixed) request a page twice when the default character encoding set in the browser does not match the page encoding.

The best general purpose solution is to use a session variable to allow the page to only be processed once per 'browser session', something like this -

<?php
session_start
();
if(isset(
$_SESSION['oneshot'])){
	
die(
'The page has already been requested this browser session.');
}
$_SESSION['oneshot'] = TRUE;

// the remainder of your code on the page here...
?>
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline devofashTopic starter

  • Enthusiast
  • Posts: 127
    • View Profile
Re: phpmailer sending duplicates
« Reply #4 on: February 26, 2010, 10:06:45 AM »
Hi thanks for that.

Here's the thing, whilst in testing i'm using about 4/5 emails addresses (don't want to spam the clients) and the script works perfectly with my code. But as soon as I try it with live data its screwes up.

Is there anything wrong with my foreach loop?

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: phpmailer sending duplicates
« Reply #5 on: February 26, 2010, 10:07:49 AM »
Also, the code you posted is not checking in any way how or who requested the page, so, anytime the page is requested, by a search engine or some other bot script, it will send emails.

You would need to add login/authentication logic to insure that the code on the page only executes when YOU submit or browse to the page.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline devofashTopic starter

  • Enthusiast
  • Posts: 127
    • View Profile
Re: phpmailer sending duplicates
« Reply #6 on: February 26, 2010, 10:51:04 AM »
Its got all that, I just removed all that stuff just to keep the focus on the main script.

I still don't understand why it sends out duplicate emails when I use it with 200 emails. I don't want to spam my clients again.

Is there a feature in PHPMailer where I test it with my list ... without actually sending it out to my clients?

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: phpmailer sending duplicates
« Reply #7 on: February 26, 2010, 11:18:43 AM »
Quote
Its got all that ...
Are you sure it is working, because a lot of people's security code does not actually prevent the 'protected' code on the page from being executed while the browser is requesting the new URL due to a redirect.

Quote
Is there a feature in PHPMailer where ...
Nope.

You would need to log the actual information being generated for the emails (edit: and comment out the line that is actually sending the email) - http://us.php.net/manual/en/function.error-log.php

If you include a date/time and the IP address where the page request came from as part of the information you log, you should be able to determine if this is due to your browser making multiple page requests.

If these people have supplied their email address and they are receiving duplicates, that would not be considered spamming, unless the email is spam.
« Last Edit: February 26, 2010, 11:20:15 AM by PFMaBiSmAd »
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.