Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpmailer timeout not working

Tags:

php

phpmailer

i use phpmailer to send mail,and i want to get the result,because send mail is very frequently. so i want to use phpmailer "timeout" .but is not working. my code

        $mail             = new PHPMailer();
    $mail->IsSMTP();
    $mail->Timeout  =   10;
    $mail->SMTPAuth   = true;
    $mail->SMTPKeepAlive = true;
    $mail->SMTPSecure = "ssl";
    $mail->Host       = "smtp.gmail.com";
    $mail->Port       = 465;
    $mail->Username   = "[email protected]";
    $mail->Password   = "xxxx";
    $mail->From       = "[email protected]";
    $mail->Subject    = "This is the subject";
    $mail->AltBody    = "test";
    //$mail->WordWrap   = 50; // set word wrap
    $mail->MsgHTML("test233");
    //$mail->AddReplyTo("[email protected]");
    $mail->AddAddress("[email protected]");

    $mail->IsHTML(true);

    echo "<br/>".time()."<br/>";
    echo "time out is ".$mail->Timeout."<br/>";
    if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message has been";
    }
    echo "<br/>".time()."<br/>";

and echo is : 1383181520 time out is 10 Message has been 1383181534

can you help me

like image 338
user2939638 Avatar asked Jan 13 '23 00:01

user2939638


1 Answers

The documentation states that "var $Timeout = 10 Sets the SMTP server timeout in seconds" and "this function will not work with the win32 version."

If you want a longer timeout value, simply set it to a minute (60 seconds) or so. If you are sending multiple emails through the same SMTP server, it may be beneficial to keep the connection open, but then remember to close it at the end.

If you do extend the timeout, also make sure that you increase the time limit on the script, or remove it all together:

<?php
    set_time_limit(0); // remove a time limit if not in safe mode
    // OR
    set_time_limit(120); // set the time limit to 120 seconds

    $mail                = new PHPMailer();
    $mail->IsSMTP();
    $mail->Timeout       =   60; // set the timeout (seconds)
    $mail->SMTPKeepAlive = true; // don't close the connection between messages
    // ...
    // Send email(s)
    $mail->SmtpClose(); // close the connection since it was left open.
?>
like image 155
Westy92 Avatar answered Jan 16 '23 20:01

Westy92