0

I wrote this:

  require 'PHPMailer/Exception.php';
  require 'PHPMailer/PHPMailer.php';
  require 'PHPMailer/SMTP.php';
  
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;
  
  $mail = new PHPMailer(true);
  
  try {
      $mail->SMTPDebug = 2;                                      
      $mail->isSMTP();                                           
      $mail->Host       = 'smtp.google.com;';                   
      $mail->SMTPAuth   = true;                            
      $mail->Username   = 'myemail@gmail.com';                
      $mail->Password   = 'mypassword';                       
      $mail->SMTPSecure = 'ssl';
      $mail->Port       = 465; 
  
      $mail->setFrom('myemail@gmail.com', 'Sender');          
      $mail->addAddress('hisemail@gmail.com');
      $mail->addAddress('hisemail@gmail.com', 'Reciever');
      $mail->isHTML(true);                                 
      $mail->Subject = 'Subject';
      $mail->Body    = 'HTML message body in <b>bold</b> ';
      $mail->AltBody = 'Body in plain text for non-HTML mail clients';
      $mail->send();
      echo "Mail has been sent successfully!";
  } catch (Exception $e) {
      echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  }

And The error was:

023-07-21 15:54:54 SERVER -> CLIENT:
Invalid hostentry:
SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_client(): SSL: An existing connection was forcibly closed by the remote host
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_client(): SSL: An existing connection was forcibly closed by the remote hostSMTP server error: Connection failed. Detail: stream_socket_client(): SSL: An existing connection was forcibly closed by the remote host SMTP code: 2

I tried a lot changing tls and ssl and ttl and different ports. Additionally, when I used nodemailer in nodejs. The same error occured, but it was solved by setting tls unhandledRejection to false. Is there something similar to that in php ?

Please :)

Codeleek
  • 1
  • 1
  • You have used direct password or app password for accesing to gmail server? – Ariful Islam Jul 21 '23 at 17:40
  • @codeleek: Editing does not work on Stackoverflow, just in case you're not aware, the earlier revision remains. You need to change the credentials as well. Just FYI. – hakre Jul 22 '23 at 15:08

0 Answers0