Problem with Phpmailer sending emails

Asked

Viewed 89 times

-1

I am having problems in a project that uses Phpmailer, the same worked normally, but now is giving problem of Connection Timeout on the site, however when I run the script via terminal the same works perfectly.

Items I’ve already checked:

  • Access credentials;
  • Openssl from the server;
  • Version of php.ini loaded by browser x terminal;
  • I debugged the code and the apparent problem is in phpmailer’s Send class all the rest of the script works perfectly.

Someone with the same problem?

    <?php
try{

   require 'vendors/phpmailer/src/PHPMailer.php';
   require 'vendors/phpmailer/src/SMTP.php';
   require 'vendors/phpmailer/src/Exception.php';

$layout = "teste servidor antigo";

$mail = new PHPMailer\PHPMailer\PHPMailer();// create a new object

$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "mail.swoosh.com.br";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->CharSet = "UTF-8";
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->SetFrom("[email protected]");
$mail->Subject = "Assunto";
$mail->Body = $layout ;
 $mail->AddAddress("[email protected]");
$mail->AddAddress("[email protected]");
$mail->AddAddress("[email protected]");
 if(!$mail->Send()) {  
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }
}catch(Exception $e){
   print_r($e);
}

1 answer

0

You need to check the time required to send the email. By default PHP sets (in PHP.ini) 30 seconds, if you spend that time, the server cuts the connection to the client, freeing the server for new connections.

You can change this time via the command below:

set_time_limit($seconds);

Thus remaining:

<?php
try{

   require 'vendors/phpmailer/src/PHPMailer.php';
   require 'vendors/phpmailer/src/SMTP.php';
   require 'vendors/phpmailer/src/Exception.php';

$layout = "teste servidor antigo";
set_time_limit(50);
$mail = new PHPMailer\PHPMailer\PHPMailer();// create a new object

$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "mail.swoosh.com.br";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->CharSet = "UTF-8";
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->SetFrom("[email protected]");
$mail->Subject = "Assunto";
$mail->Body = $layout ;
 $mail->AddAddress("[email protected]");
$mail->AddAddress("[email protected]");
$mail->AddAddress("[email protected]");
 if(!$mail->Send()) {  
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }
}catch(Exception $e){
   print_r($e);
}

Or you can make a definitive configuration for the whole site through the parameter max_execution_time in php.ini.

The problem of the second alternative is that it is valid for the entire site, which can generate a bottleneck in the fulfillment of new requests.

  • Francisco the problem is that when I run this same script on the terminal, it runs quickly to fire the email

  • There may be two problems, one is DNS as described [here] (https://techleader.pro/a/372-Fixing-slow-performance-of-sending-mail-from-PHP-via-Sendmail), but on this site [here] (https://github.com/PHPMailer/PHPMailer/wiki/Sending-to-lists#maximising-performance) recommends sending to localhost and checking the DNS cache. Maybe the command line performance is higher, due to not using php.ini (from what I understand vc tested with the same apache php.ini). Another thing we can observe is the performance in windows environments be much lower than the linux environment.

  • 1

    Thanks guys, in fact I ended up using a solution via terminal, I stored the data in a table of the database and following a script that captures this data and assembles the email. The environment is actually Linux is a very intriguing thing

Browser other questions tagged

You are not signed in. Login or sign up in order to post.