1

EDIT:

I thought it was related to URL Rewrites, but even with those disabled, it still sends two emails. I have the subject just a random number using rand(), and the number is different in each email, which would seem to indicate that the script is actually being run twice, but this makes no sense to me.

I am running this code, that is as simplified as I can get it, there are no redirects or anything and yet the script seems to be run more than once.

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$toEmail        = '######@#####.com';
$toName         = 'To';
$fromName       = 'From';
$fromEmail      = '#####@#####.com';
$subject        = rand();
$message        = 'Test Message';

$mail           = new PHPMailer(TRUE);
$mail->isSMTP();
$mail->Host         = 'smtp.office365.com';
$mail->SMTPAuth     = TRUE;
$mail->SMTPSecure   = 'tls';
$mail->Username     = '#####@######.com';
$mail->Password     = '#####';
$mail->Port         = 587;
$mail->setFrom($fromEmail, $fromName);
$mail->addAddress($toEmail, $toName);
$mail->Subject      = $subject;
$mail->Body         = $message;
$mail->send();

?>

###############################################################################

ORIGINAL POST:

I have a PHP script that is sending an email with PHPMailer that is running twice and sending an email more than once. I know it is the PHP script running twice because I add a random number in PHP to the email subject, and it is different in each email.

Now, I am also using Web.Config to do URL Rewrites, and I see a 404 in the error log for the favicon. I was reading that could be the reason the rewrite rule is causing the file to load more than once, but even placing a favicon.ico in my includes directory doesn't solve the issue, and I haven't come across any other solutions.

This is the full rewrite rule...

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Remove PHP" stopProcessing="false">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />
                    </conditions>
                    <action type="Rewrite" url="{R:1}.php" />
                </rule>
                <rule name="Remove trailing slash" stopProcessing="true">
                    <match url="(.*)/$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Redirect" redirectType="Permanent" url="{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

I don't think this could cause this issue on its own, but at this point I am really at a loss.

Anyone experienced a similar issue and come up with a solution. I would greatly appreciate it.

  • The usual explanation for this is browser plugins doing repeat posts. Try hitting it with curl to confirm. – Synchro Jan 05 '21 at 22:04
  • cURL also sends to emails. – Fred Langemark Jan 05 '21 at 22:09
  • 2
    When you **remove** that rewrite rule and request your PHP script directly, does it send the email twice also ? – Accountant م Jan 05 '21 at 22:09
  • Yes, it does. So that rules that out. – Fred Langemark Jan 05 '21 at 22:13
  • But that just makes me more confused. I know I am not calling the script twice. – Fred Langemark Jan 05 '21 at 22:17
  • 1
    I'm sorry I didn't use `phpmailer` before I can't help much here, but I don't think the provided code sends 2 emails!. **Try to reduce your actual code to the minimal reproducible code to narrow down the possibilities**. Also echo something in the script(does it echo twice ?). Check your network requests in the browser(with persist logs activated). Check if this code is included twice in another script. – Accountant م Jan 06 '21 at 00:58
  • Next thing is to check logs to see if the web server is receiving two requests. Might be interesting to enable PHPMailer debug output so you can see what’s happening at a lower level. Separately, I observe you have no error checking at all – see PHPMailer example code for how to do that. – Synchro Jan 06 '21 at 06:46
  • Will this problem occur if the application is not deployed in IIS? We have to confirm whether this is an IIS issue or a PHP application issue. I found a similar problem on so, you can also refer to it: https://stackoverflow.com/questions/14966984/php-seems-to-execute-script-twice/40953958 – Ding Peng Jan 06 '21 at 06:56

0 Answers0