How to send emails?

I have all these code:

use PHPMailer\PHPMailer\PHPMailer;

require __DIR__ . './vendor/autoload.php';
require './classes/Config.php';

if (!ifItIsMethod('get') && !$_GET['forgot']) {

  redirect('index');
}

if (ifItIsMethod('post')) {

  if (isset($_POST['email'])) {
    $email = $_POST['email'];
    $length = 50;
    $token = bin2hex(openssl_random_pseudo_bytes($length));


    if (email_exists($email)) {

      if ($stmt = mysqli_prepare($connection, "UPDATE users SET token='{$token}' WHERE user_email = ?")) {
        mysqli_stmt_bind_param($stmt, "s", $email);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_close($stmt);


        /**
         * 
         * configure PHPMailer
         * 
         */
        $mail = new PHPMailer();
        // $ssl_reqd;
        // $url = 'smtp://live.smtp.mailtrap.io:587';
        // $user = 'api:********45e8';
        // $mail_from = 'hello@demomailtrap.com';
        // $mailrcpt = 'nasratullahkhadim00@gmail.com';
        // $upload - 'file';
        //$mail->SMTPDebug = SMTP::DEBUG_SERVER;
        $mail->isSMTP();
        // $mail->Host = Config::SMTP_HOST;
        // $mail->Username = Config::SMTP_USER;
        // $mail->Password = Config::SMTP_PASSWORD;
        // $mail->Port       = Config::SMTP_PORT;
        $phpmailer = new PHPMailer();
        $phpmailer->isSMTP();
        $phpmailer->Host =Config::SMTP_HOST;
        $phpmailer->SMTPAuth = true;
        $phpmailer->Port = Config::SMTP_PORT;
        $phpmailer->Username =Config::SMTP_USER;
        $phpmailer->Password =  Config::SMTP_PASSWORD;
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->SMTPAuth   = 'PLAIN';
        $mail->isHTML(true);
        $mail->SMTPDebug = 3;


        //Enable implicit TLS encryption
        // $mail->Port = 465; 
        //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`


        $mail->setFrom('nasratullahkhadim00@gmail.com', 'Nasratullah Khadim');
        $mail->addAddress($email);
        $mail->Subject = 'This is a test email';
        $mail->Body = 'Email body';


        if ($mail->send()) {
          echo "IT WAS SENT";
        } else {
          echo "NOT SENT";
        }
      }
    }
  }
}

and this code: class Config

{
  const SMTP_HOST = 'sandbox.smtp.mailtrap.io';
  const SMTP_PORT =  587;
  const SMTP_USER = '94c23eb94ca034';
  const SMTP_PASSWORD = '********2966';
}

in my visual studio code but when I run it in googl chrome it gives me this error:

2024-12-02 09:42:54 Connection: opening to ssl://localhost:25, timeout=300, options=array()
2024-12-02 09:42:54 Connection failed. Error #2: stream_socket_client(): Unable to connect to ssl://localhostlive.smtp.mailtrap.io25 (Failed to parse address "localhostlive.smtp.mailtrap.io25") [C:\xampp\htdocs\cms\vendor\phpmailer\phpmailer\src\SMTP.php line 420]
2024-12-02 09:42:54 SMTP ERROR: Failed to connect to server: Failed to parse address "localhostlive.smtp.mailtrap.io25" (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
NOT SENT

now how to solve this problem could you help me please

Closing thread as duplicate of About sending emails

1 Like