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 Messages Sent #347

Closed
ken-jones1 opened this issue Jan 20, 2015 · 44 comments
Closed

Multiple Messages Sent #347

ken-jones1 opened this issue Jan 20, 2015 · 44 comments

Comments

@ken-jones1
Copy link

I'm a newbie here and just wondering if anyone could tell me why the mailer is sending me 3 instances of the same email to 1 recipient? Any assistance would be greatly appreciated

@drmad
Copy link

drmad commented Jan 21, 2015

You should give more information. The code you're using for sending, for instance.

@ken-jones1
Copy link
Author

Here's the code - pretty basic:

require '../pathtophpmailer/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtplocation';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'username';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->Port = 25;

$mail->From = '[email protected]';
$mail->FromName = 'John Smith';
$mail->addAddress('[email protected]');               // Name is optional

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

In the same directory as the autoload I have the class.smtp.php, class.pop3.php and class.phpmailer.php. Thanks for any assistance.

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

Are you positive that this code is only being run once?
Is it sending three separate emails, or three copies of the same email (i.e. are their headers absolutely identical or not)?
If you set $mail->SMTPDebug = 3;, does it show three messages being sent?
Do you have access to the mail server logs?

@ken-jones1
Copy link
Author

I haven't set the Debug on, its sending 3 of the same emails to the 1 address and I've looked at the server logs but didn't see much in them - just the file that I ran when I received the emails.

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

Well how about you do enable Debug, since it will show you what's happening?

Please read what I said - if you run this code three times, it will send three messages that wil look visually identical, but they will have different headers, particularly the message ID headers. If it's only running once, but you're receiving three messages, then they will be identical, including the headers.

You could also try doing this to make it more obvious:

$mail->Subject = 'Here is the subject '. md5(rand());

If it runs three times, the messages will have different subject lines; if it's run once, they will be the same.

You need to look in the mail server log, not your web server log.

@ken-jones1
Copy link
Author

Ok cool I appreciate you time & assistance here :) !! I ran it with the debug on and got this one thing that I'm wondering if it is the problem (oh and I'm putting the md5 stuff in now.

SERVER -> CLIENT: 354 enter mail, end with "." on a line by itself 2015-01-21 15:24:48

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

That's normal. After you send an SMTP DATA command, the server will accept everything else you send as part of the message until it receives a . on a line by itself. You're just seeing the SMTP conversation between client and server.

@ken-jones1
Copy link
Author

Ok cool with that added I just received only 1 email, but there was nothing in the body of the email and coding after the Subject. Which I take was the md5 random stuff.

@ken-jones1
Copy link
Author

Opps yes there was a body sorry

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

That doesn't sound right - you sure you didn't miss something off there? It should have just set the subject lines like: Here is the subject bc73b42cdaacc7d18235243fbf211915.

@ken-jones1
Copy link
Author

Yep your right - the subject just had the coding after the Subject line - awesome I appreciate you assistance in this matter greatly !! Just one more question is there anyway to get rid of that coding after the Subject or will I have to live with that.

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

Er, the code is only there temporarily to help diagnose your multiple message problem! If it's working, just change it back to whatever it was.

So are you saying it's all working now? If so, please close this ticket.

@ken-jones1
Copy link
Author

Oppps sorry - yes it seems to be all working now - I really appreciate your assistance in this matter. I will close this ticket ! Awesome !!

@ken-jones1
Copy link
Author

Well I just tried it again and it sent 3 emails - so I put the random code back in and it sent 4 emails with different Subjects??

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

OK, so we know your code is being run multiple times, and PHPMailer is doing nothing wrong. The code you posted is not your complete script, so it must be something else you've not shown us.

@ken-jones1
Copy link
Author

Yes that is the complete script

@ken-jones1
Copy link
Author

I just tried it again and it sent 2 mails with different Subjects?? Should I download another copy of the phpmailer?

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

How are you running it? Pointing a browser at it? Running from a command line?

@ken-jones1
Copy link
Author

I'm running it in a browser, from the url line

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

It sounds like your browser may be sending multiple requests - do you see that in your web server logs?

@ken-jones1 ken-jones1 reopened this Jan 21, 2015
@ken-jones1
Copy link
Author

Ya I just cleared the cache and all browser history etc. and it still is sending 2 distinct emails?

@ken-jones1
Copy link
Author

So do you think it would make a difference is I set that up as a link in a page - that's what my purpose is here to do a form to email

@Synchro
Copy link
Member

Synchro commented Jan 21, 2015

What's happening is that it's being loaded multiple times by your browser, and it sends one message each time. Your web server logs will show this.

There is an example form to mail script in the examples folder (have a look at the generator script).

@ken-jones1
Copy link
Author

Ok cool thanks - appreciate your assistance - use to be a programmer in the early days lol trying to get back up to date - have a few sites I'm working on for myself. Cheers

@Synchro Synchro closed this as completed Mar 5, 2015
@cybergenio
Copy link

About: "Synchro: It sounds like your browser may be sending multiple requests - do you see that in your web server logs?"

I had a similar error, When the script runs, three consecutive emails were sent, I thought the code was failing.
Then I saw who were following sending while you are watching the page in the browser.

I found the following: The "Realplayer Downloader" plugin reviews the page in the background, the plugin ran the page several times in order to find video links. I removed the plugin and corrected, only happened in Chrome!

@Synchro
Copy link
Member

Synchro commented Apr 14, 2015

Good thought, sounds like a likely explanation - things like dev tools might do something similar.

@jakewhiteley
Copy link

@Synchro I can confirm that Chrome extensions have a habit of doing this. I just encountered the same issue where multiple emails were being sent.

I have a page which creates a password, saves it, then emails it. The email I was getting sent was different than the stored value.

After disabling all my Chrome extensions the script worked as expected.

@Synchro
Copy link
Member

Synchro commented Jun 8, 2015

You could protect against this on the server side by storing a hash of recently-submitted values for say, the last hour, and ignoring the request if it's a repeat. This would be a good use of something like memcached.

@sebamen
Copy link

sebamen commented Mar 2, 2016

I have a similar problem. But, when i ran the script on localhost (WAMP) it works fine, but when I did it on the web hosting page it repeats the emails twice or three times, some of them don't repeat it at all. I am using the same browser (Chrome) on the same PC. Any ideas? TIA

@Synchro
Copy link
Member

Synchro commented Mar 2, 2016

Check your logs - multiple requests will show up there.

@sebamen
Copy link

sebamen commented Mar 2, 2016

I have no access to the web server or mail server log, it's a hosting website of a client of mine.

@ken-jones1
Copy link
Author

I finally did get it to stop doing that but its been 1 year ago and I can't remember what I did - I think it was a corrupt Mailer php file

Date: Wed, 2 Mar 2016 09:06:29 -0800
From: [email protected]
To: [email protected]
CC: [email protected]
Subject: Re: [PHPMailer] Multiple Messages Sent (#347)

I have no access to the web server or mail server log, it's a hosting website of a client of mine.


Reply to this email directly or view it on GitHub.

@Synchro
Copy link
Member

Synchro commented Mar 2, 2016

Well without access to the server you can't fix it anyway. If the messages are turning up with different message-id headers, it will be because your browser is making multiple requests, as mentioned earlier in this thread.

@sebamen
Copy link

sebamen commented Mar 3, 2016

I use md5() in the subject of the email and returned all different codes (20 emails 20 different subjects), so it looks like something is wrong in the code or in anyway it is recharging the page or something, I disabled all th extensions of the browser but still happening. Anyway I use the same browser in the localhost and it's working ok. I don't know how to solve this....

@ghost
Copy link

ghost commented Mar 6, 2016

I'm having this same problem. Basically -- did all the advice from above-- and when I added this code:

$mail->Subject = 'Here is the subject '. md5(rand());
I get 3 different emails with 3 different subjects.

I'm capturing an image from an html5 canvas, saving it then sending it via email. Here's some of my code (the mail portion) -- maybe we can help each other.

` $mail = new PHPMailer();
$mail->Sender = "[email protected]";
$mail->From = "[email protected]";
$mail->FromName = "My Web Admin";
$mail->AddReplyTo( "[email protected]", "DO NOT REPLY" );
$mail->AddAddress("[email protected]");

    // $mail->Subject  = "Latest Sketch Image";
$mail->Subject = 'Here is the subject '. md5(rand());
$mail->Body     = "Attached! \n\n Thanks.";
$mail->AddAttachment('uploads/latestimage.png');


if(!$mail->Send()) {
    echo 'Message was not sent.';
    echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
    echo '<script language="javascript">';
    echo 'alert("Thanks! Message Sent")';
    echo '</script>';
}

`

@ken-jones1
Copy link
Author

Ok what I did to track this down I put a random number generator code into each place to see where the code was sending from

Date: Sun, 6 Mar 2016 05:23:13 -0800
From: [email protected]
To: [email protected]
CC: [email protected]
Subject: Re: [PHPMailer] Multiple Messages Sent (#347)

I'm having this same problem. Basically -- did all the advice from above. with the addition of this code:

$mail->Subject = 'Here is the subject '. md5(rand());

I get 3 different emails with 3 different subjects.

I'm capturing an image from an html5 canvas, saving it then sending it via email. Here's some of my code -- maybe we can help each other.

` $mail = new PHPMailer();

$mail->Sender = "[email protected]";

$mail->From = "[email protected]";

$mail->FromName = "My Web Admin";

$mail->AddReplyTo( "[email protected]", "DO NOT REPLY" );

$mail->AddAddress("[email protected]");


// $mail->Subject  = "Latest Sketch Image";

$mail->Subject = 'Here is the subject '. md5(rand());
$mail->Body = "Attached! \n\n Thanks.";
$mail->AddAttachment('uploads/latestimage.png');

if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo '<script language="javascript">';
echo 'alert("Thanks! Message Sent")';
echo '</script>';
}

`


Reply to this email directly or view it on GitHub.

@Synchro
Copy link
Member

Synchro commented Mar 6, 2016

Right, so what this tells you is that your script is run multiple times, and PHPMailer is sending once each time it does so, as it should. PHPMailer is not the problem here - it's just a symptom - so you need to look elsewhere.

@ghost
Copy link

ghost commented Mar 6, 2016

That makes sense. Here's my entire "send.php". I even updated to the latest of PHPMailer. If I go directly to the "send.php" file -- I get the following error. However, it also prompts that the image was sent 2 times. I'm really trying to figure out where in my script am I saying.. hey, send this, send this again, send this 3 times. I just don't see it.... tuff.

Notice: Undefined index: dataURL in /save2.php on line 15
Unable to save the file.latestimage.png
<?php
    // DEFINE ERRORS 
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);


    // RECIEVE CAPTURED IMAGE FROM .JS FILE -- STORE IT IN UPLOADS FOLDER.
    define('UPLOAD_DIR', '/pathtomyuploadsfolder/uploads/');

        // ***** THIS IS WHERE THE ERROR FROM ABOVE IS ****** 
    $img = $_POST['dataURL'];
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);

    $data = base64_decode($img);

    $file = UPLOAD_DIR . uniqid() . '.png';
    $file = UPLOAD_DIR . 'latestimage.png';
    $success = file_put_contents($file, $data);
    print $success ? 'Latest File:   ' . $file : 'Unable to save the file.';

    // GET LATEST UPLOAD -- AND HAVE IT STORED IN DOM (WHEN NEEDED) 
    $path = "/pathtomyuploadsfolder/uploads/"; 

    $latest_ctime = 1;
    $latest_filename = '';    

    $d = dir($path);
    while (false !== ($entry = $d->read())) {
      $filepath = "{$path}/{$entry}";
      // could do also other checks than just checking whether the entry is a file
      if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
        $latest_ctime = filectime($filepath);
        $latest_filename = $entry;
        print $latest_filename;
      }

    // SEND EMAIL W/ ATTACHMENT OF LATEST IMAGE (OVERRIDING same file EVERYTIME)
    if (!class_exists("phpmailer")) {
        require_once('PHPMailer-master2/class.phpmailer.php');
        require_once 'PHPMailer-master2/PHPMailerAutoload.php';
    }

    $mail = new PHPMailer();

    $mail->Sender = "[email protected]";

    $mail->Sender = "[email protected]";
        $mail->From = "[email protected]";
        $mail->FromName = "My Web Admin";
        $mail->AddReplyTo( "[email protected]", "DO NOT REPLY" );
        $mail->AddAddress("[email protected]");

        // $mail->Subject  = "Latest Sketch Image";
    $mail->Subject = 'Here is the subject '. md5(rand());
    $mail->Body     = "Attached! \n\n Thanks.";
    $mail->AddAttachment('/uploads/latestimage.png');

    if(!$mail->Send()) {
        echo 'Message was not sent.';
        echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
        echo '<script language="javascript">';
        echo 'alert("Thanks! Message Sent")';
        echo '</script>';
    }
}

@Synchro
Copy link
Member

Synchro commented Mar 7, 2016

Your script does no checking to see if there is a form submission, it just runs every time, and if you visit the bare URL (e.g. there is a redirect or a separate page load), $_POST will be empty and so you will get that undefined index error. There is nothing wrong with your script; there is a problem with the way that you are running it, and how you're managing your URLs, which is not a question for here.

@Atmiya735
Copy link

Hello Synchro,
I m facing same issue in this. It is sending 2 email to every email please suggest me the solution.

@Synchro
Copy link
Member

Synchro commented Sep 4, 2018

To start with, I'd try reading this thread.

@0743566638
Copy link

i have an issued that is the same as that on i am trying to loop through emails which are in mysqli database and send single copy of email to each user using phpmailer is working but its sending 6 emails of the same copy..any feedback will be much appreciated.
i wanted it to send only one single email thanks

@Synchro
Copy link
Member

Synchro commented Nov 28, 2020

Please read this thread, base your code on the mailing list example, make sure you're calling clearAddresses() in your sending loop.

@Haroldgomez777
Copy link

I had a similar issue calling clearAddresses() in the sending loop fixed it.

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

9 participants