I am using the code below to send a link to the user email to reset the password, the code works fine and sends the link. The problem is after sending the link the page will show many lines of information and a success message at the end.
What is wrong with the code below to avoid these lines? Here is the code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'database.php';
if(isset($_POST["email"])){
$emailTo = $_POST["email"];
$code = uniqid(true);
$sql = "INSERT INTO reset(code, email) VALUES('$code', '$emailTo');";
$run = mysqli_query($conn, $sql);
if(!$run){
exit("Error");
}
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myEmail.com';
$mail->Password = 'xxxxxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//Recipients
$mail->setFrom('myqmial.com', 'Hello');
$mail->addAddress($emailTo);
$mail->addReplyTo('no-reply@gmail.com', 'No reply');
// Content
$url = "http://" .$_SERVER["HTTP_HOST"] . dirname($_SERVER["PHP_SELF"]) . "/resetPassword.php?code=$code";
$mail->isHTML(true);
$mail->Subject = 'Reset Password';
$mail->Body = "<h1>You asked to reset your password، </h1>
Click <a href='$url'>this link</a> Please click on the link";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'The link has been successfully sent to your email';
}
catch (Exception $e) {
echo "Message could not be sent. Mail Error: {$mail->ErrorInfo}";
}
exit();
}
?>
<form method='POST'>
<input type='text' name='email' placeholder='Email'>
<input type='submit' name='submit' value='Reset password'>
</form>
I am attaching an image with the results:
I do not know what mistake I am doing, so how can I just show the message (The link has been successfully sent to your email) without all these lines?