Jump to content

phpmailer sending duplicates


devofash

Recommended Posts

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!";
    }
?> 

Link to comment
Share on other sites

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...
?>

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.