Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple emails sent #427

Closed
varmad opened this issue May 28, 2015 · 15 comments
Closed

Multiple emails sent #427

varmad opened this issue May 28, 2015 · 15 comments

Comments

@varmad
Copy link

varmad commented May 28, 2015

I am using this script for my email campaigns, users are complaining that they are receiving duplicate mails at the same point of time. I am running this script from the command line.

This is the code am using, please suggest me if there are any changes.

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = $config->smtp->host;  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = $config->smtp->username;                 // SMTP username
$mail->Password = $config->smtp->password;                           // SMTP password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->isHTML(false);

$replay_to = $config->smtp->replay_to;
$replay_name = $config->smtp->replay_name;

$mail->From = $replay_to;
$mail->FromName = $replay_name;

$total_emails = count($recipients);

if($total_emails > 0) {

  $i=0; $k=1; $interval = ($total_emails > 5000)?150:300; $nextInterval = 500;
  foreach($recipients AS $recipient) {
    //sleep time for every 500 emails
    if($i == $nextInterval ){
      sleep($interval);
      $nextInterval += 500; 
    }   

    $trac_url = ''; $unsub_url="";
    $to_email = $recipient['Email'];
    $to_name  = $recipient['Name'];

    //subject rotation for each mail
    $subject_index = ($i%count($subjects));

    //Getting subject
    $subject = $to_name.', '.$subjects[$subject_index];

    //Tracking url
    $trac_url = $tracking_url.'?user='.urlencode($to_email);

    //Unsubscribe url
    $unsub_url = $unsubscribe_url.'?user='.urlencode($to_email);

    //Place holder which are replaced in html template
    $place_holders = array(
      '{{user_name}}' => $to_name,
      "{{unsub_link}}"   => $unsub_url,
      "{{tracking_url}}"   => $trac_url,
      "{{from_name}}"   => $replay_name,
      "{{from_email}}"   => $replay_to,
      "{{city}}" => $campaign->city,
      "{{venue}}" => $campaign->venue,
    );

    //Get text template
    $message_txt = file_get_contents($campaign->product.".txt");
    $message_txt = $message_txt;

    //Replace place holders
    $body_txt = strtr($message_txt, $place_holders);

    $mail->addAddress($to_email, $to_name);     // Add a recipient
    $mail->addReplyTo($replay_to, $replay_name);
    $mail->Subject = $subject;
    $mail->Body    = $body_txt;

    if(!$mail->send()){
        echo "Message could not be sent to:".$to_email." \n";
        echo "Mailer Error: " . $mail->ErrorInfo." \n";
    } else {
        echo "Message has been sent to: ".$to_email." \n";
        $k++;
    }

    $mail->clearAddresses();

    $i++;
  }

}

Thanks,
Varma

@Synchro
Copy link
Member

Synchro commented May 28, 2015

It's most likely that the script is simply being run twice. You will be able to tell if the duplicate messages have different message ID headers. Try enabling debug output ($mail->SMTPDebug = 2), writing something to a log file, or append a random value to the end of the subject line - if they receive two messages with the same subject, they are true duplicates; if they receive different subjects, your script is being run twice.

You should probably not adding addReplyTo inside the loop if they are the same each time, and reply-to addresses are not cleared by clearAddresses; you should call clearReplyTos as well if you're going to add them every time.

@varmad
Copy link
Author

varmad commented May 29, 2015

Thanks for the replay @Synchro, this not happening for all users, we send 1k mails only two or three users are complaining.
This is the typical scenario, I don't even know how to debug this.

@Synchro
Copy link
Member

Synchro commented May 29, 2015

For that kind of problem you just need to do lots of logging, then you will be able to go back and see what happened when you get another report.

@varmad varmad closed this as completed May 31, 2015
@varmad varmad reopened this May 31, 2015
@varmad
Copy link
Author

varmad commented May 31, 2015

Ok, I will do that.

@varmad varmad closed this as completed May 31, 2015
@aeyachts99
Copy link

I'm also getting this issue even when sending to just one recipient. I've added a random number to the subject field and both received messages show the same random number so as mentioned earlier in this thread they are true duplicates.

@Synchro
Copy link
Member

Synchro commented Oct 26, 2015

Like I said, log everything. Run it in a debugger, record traces. All will be revealed.

@georgelouk
Copy link

georgelouk commented May 15, 2017

I have a similar issue, and i use the latest PHPmailer version.

@Synchro
Copy link
Member

Synchro commented May 15, 2017

So follow the advice in this ticket.

@georgelouk
Copy link

I've done a lot of checks and logging out apache logs...

Let's say that the code is at thanks.php
It seems that the thanks.php file that runs the email code, run sometimes more than 1 time without any specific reason after few seconds difference (around 2-8 seconds).

Of course the code of that php file, doesn't have any loop in order to do that, and also the strange thing that this this is not happening every time, but sometimes.

The result of it, is the same receiver to receive more than 2-3 copies of the same email...
I believe that the issue is coming out from the PHPMailer script...i've "updated" to the latest version, but the problem remains.

@Synchro
Copy link
Member

Synchro commented May 15, 2017

It's very common for browser extensions to repeat requests. PHPMailer just doesn't have a mechanism for sending multiple messages without being asked to. Did you try the random subject test described above? That will prove whether it's multiple requests. You will also see it in your web server logs.

@HemalBhalodia
Copy link

I am having the same issues. PHPMailer sends sometime two or more emails. But it's done randomly as we are sending 1000+ emails daily. So it's sending after we start to send more emails. It's working fine when we send 200-300 emails a day but if we start to send more it's sending 2-3 emails to some customers.

@Synchro
Copy link
Member

Synchro commented Oct 29, 2017

So do what it says above. Log details.

@HemalBhalodia
Copy link

Thanks Synchro for quick followup. $mail->clearAddresses fixed my issue. Thank you again.

@shishkin17
Copy link

I thought I had the same issue until I realised that if you want to send 2 (or more) separate emails to different addresses you need to clear addresses and attachments (and everything that has clear functions). The problem that it is not obvious. I found that you have those clear functions only in that issue after an hour of trying to figure out why is that happening. I am not sure that I have to read all the documentation and all the functions available just to do the basic action - send email.
You should write about that on the main page and in the example!

@Synchro
Copy link
Member

Synchro commented Mar 19, 2019

It is mentioned in the examples, in exactly the one you'd expect - the mailing list. There's also a clue in the name it's addAddress not setAddress, implying it's cumulative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants