简体   繁体   中英

PHPMailer Sends Email then Hangs (Windows, XAMPP)

When using PHPMailer on localhost (Windows, XAMPP), email sends ok but the script hangs forever -- no refresh.

php's own mail() function works fine, and PHPMailer works fine using sendmail, so this is only a problem in SMTP mode.

Strangely, when stepping through with Xdebug,

I get "Fatal error: Maximum execution time of 0 seconds exceeded" in the console when I reach __destruct()

though I can step through this, which gives me the refresh and the error reflected in the browser. Also, once I've done this, I can refresh the browser and new emails will be sent normally and with no error and no hanging. Exit out of debugging mode and I return to the hanging behaviour.

Note: in php.ini: max_execution_time=60 max_input_time=60

require_once "PHPMailerAutoload.php";
$to = "myemail@gmail.com";
$to_name = "Me";
$from_name = "fromName";
$from = "from@name.com";
$subject = "This is a test email from php " . strftime("%T", time());
$message = "phpmailer using smtp";

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Host     = "# censored #";
$mail->Port     = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Hostname = "myhost";
$mail->Username = '# censored #';
$mail->Password = '# censored #!';

$mail->FromName = $from_name;
$mail->From     = $from;
$mail->addAddress($to, $to_name);
$mail->Subject  = $subject;
$mail->Body     = $message;
$result = $mail->send();
echo $result ? 'Sent' : 'Error: ' . $mail->ErrorInfo;

I have had similar issues when working on a development localhost server with PHPMailer. After much research the way that I overcome this was to manually require both the PHPMailer class and the SMTP class like:

require('../vendor/phpmailer/phpmailer/class.phpmailer.php'); require("../vendor/phpmailer/phpmailer/class.smtp.php");

As using the PHPMailerAutoload.php didn't fully require all the class needed and threw exceptions. Do you have a live server to test as I found a lot of answers like "works fine when live and fully deployed", this is a risky choice though.

I managed to overcome the "maximum execution time" by requiring the files manually. But the hanging is intermittent whilst using windows and XAMPP where sometimes it send straight away and other times it may take a while for the script to process. Not sure if this will work but it's worth a shot.

Further to this I investigated the "class.smtp.php" file within PHPMailer and found this:

    $this->edebug('Connection: opened', self::DEBUG_CONNECTION);
    // SMTP server can take longer to respond, give longer timeout for first read
    // Windows does not have support for this timeout function
    if (substr(PHP_OS, 0, 3) != 'WIN') {
        $max = ini_get('max_execution_time');
        // Don't bother if unlimited
        if ($max != 0 && $timeout > $max) {
            @set_time_limit($timeout);
        }
        stream_set_timeout($this->smtp_conn, $timeout, 0);
    }

Which defined in the comments above it states that "Windows does not support the timeout function". Maybe from this understanding it could help you eliminate possibilities?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM