Home > Software engineering >  PHP Mailer sending duplicate emails
PHP Mailer sending duplicate emails

Time:10-15

So as the title implies Im trying to send an email (one email) but the problem I'm facing is bizarre. Whenever debug is turned on, email is sent only once. But when debug is turned off, around 3 to 4 emails are sent at once.

NOTE: I'm using localhost not an actual server.

to diagnose the problem, I did the following: 1- used a md5 to generate random string in the "subject" to check whether email is sent with same contents or different. and the results were totally different. meaning, emails weren't duplicate but actually being sent couple of times. 2- opened the project in a browser with no extensions to make sure the problem wasn't in an extension loading my project page more than once. and the results were also similar to number 1, different "subject" also.

so, long story shot. i have no idea what causes this problem to happen. and why it only stop happening when debug is turned on.

NOTE: this is not my first time to use PHP mailer, but my first time to face this problem. I'm also using the latest version of PHP mailer (6.5.1)

Here is my entire code: in PHP mailer file:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require ABSPATH.'inc/phpmailer/script/src/PHPMailer.php';
require ABSPATH.'inc/phpmailer/script/src/Exception.php';
require ABSPATH.'inc/phpmailer/script/src/SMTP.php';



function SendEmail(){
    $mail = new PHPMailer(true);
    try {
        //Server settings
        $mail->SMTPDebug = SMTP::DEBUG_OFF;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';
        $mail->isSMTP();
        $mail->Host       = '*****';
        $mail->SMTPAuth   = true;
        $mail->Username   = '*****'; 
        $mail->Password   = '*****';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port       = 465;
        $mail->SMTPAutoTLS = false;
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $mail->ClearAllRecipients();
        $mail->clearAttachments();
        //Recipients
        $mail->setFrom('*****', '******');
        $mail->addAddress('*******', '*****');
        $mail->addReplyTo('******', '******');
    
        //Attachments
        $mail->addAttachment(ABSPATH.'upload/dog.jpg', 'new.jpg');
    
        //Content
        $mail->isHTML(true);
        $mail->Subject = md5(rand()); //This is how I'm checking whether it sends same email with same subject header or different ones. and it does send different ones
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>'. md5(rand());
        $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
        $mail->ContentType = 'text/html; charset=utf-8\r\n';
        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
}

and here is the file I'm using to call the function

<?PHP
// NO LOOP here
require_once(ABSPATH.'inc/phpmailer/phpmailer.php');
SendEmail();

CodePudding user response:

Try resetting the recipient so it can't send multiple emails.

Add this

$mail->clearAddresses();

For example

$mail->send();
$mail->clearAddresses();
echo 'Message has been sent';

CodePudding user response:

As always, the first place to check is the docs.

Since you've already tried the random hash in the subject trick (well done on your logic!), you know that the problem is not that your script is sending more than one message, but that your script is being called repeatedly, and you should see evidence of that in your web server logs too. The usual reason for this is browser extensions which have a nasty habit of doing multiple submissions. Test that by using a clean browser with no extensions (e.g. use a browser you don't normally use, like Opera, Brave, or Chrome Canary).

Any requests that your browser is making should appear in the network tab of the dev console. On the server side you can either set up remote debugging to trigger breakpoints (tricky), or var_dump every request to a log so you get full details of what's calling the script.

BTW, you really shouldn't be disabling TLS verification. Fix the problem properly instead of hiding it; the troubleshooting guide has lots on that subject.

  • Related