Home > OS >  PHPMailer keeps loading page
PHPMailer keeps loading page

Time:01-07

I have been trying to send mail using PHPMailer from my localhost(XAMPP) but the page keeps loading. After some time it shows

Fatal error: Maximum execution time of 120 seconds exceeded in C:\xampp\htdocs\test\vendor\phpmailer\phpmailer\src\SMTP.php on line 1227

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

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->SMTPDebug = 2;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'my_email';                     //SMTP username
    $mail->Password   = 'my_password';                               //SMTP password
    // $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->SMTPSecure = 'tls';            //Enable implicit TLS encryption
    $mail->Port       = 587;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
    // $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('my_email', 'Elexis');
    $mail->addAddress('recipient email.com');     //Add a recipient
    // $mail->addAddress('[email protected]');               //Name is optional
    // $mail->addReplyTo('[email protected]', 'Information');
    // $mail->addCC('[email protected]');
    // $mail->addBCC('[email protected]');

    //Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

    //Content
    $body= '<strong>This is my first email with php mailer</strong>';
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'This Is My First Email';
    $mail->Body    = $body;
    $mail->AltBody = strip_tags($body);

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

how do i solve this?

CodePudding user response:

Try increasing max_execution_time in php.ini to 300 (5 minutes).

I believe that the 2 minutes execution time is not enough for the SMTP to respond.

CodePudding user response:

Generally speaking, SMTP is not a good protocol for interactive use (i.e. during web page submission processing) because it has some very long timeouts which you may run into when talking to external mail servers.

The best way to avoid that is to run a local mail server as a relay, and have that deal with onward delivery for you.

You can install something like postfix and configure it to relay through your gmail account.

It will be much, much faster than submitting to an external server.

  •  Tags:  
  • Related