Hi team,
Can someone help with the code below? I am not getting to the OTP page. The email account is in godaddy cpanel. The app is hosted in godaddy also.
<?php
session_start();
include "connection.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
function send_verification($fname, $lname, $email, $token){
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPAuth = false;
$mail->Port = 25;
$mail->Host = "localhost";
$mail->Username = "username"; //SMTP username
$mail->Password = "pass";
$mail->IsSendMail();
$mail->From = "username";
$mail->FromName = "Name";
$mail->addAddress($email);
$mail->Subject = " OTP Verification!";
$mail->WordWrap = 80;
$email_template = "
<h5>Here's Your Security Code:</h5>
<h4 style='color: green;'>$token</h4><br/>
<h5>
Hello $fname $lname <br/><br/>
We've recieved a security request from your account. Please use the code above to login to your account.<br/><br/>
Regards,
</h5>
";
$mail->MsgHTML($email_template);
$mail->isHTML(true);
$mail->send();
if(!$mail->send()){
echo $mail->ErrorInfo;
}
}
if(isset($_POST['username']) && isset($_POST['password'])) {
function validate($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$username = validate($_POST['username']);
$password = validate($_POST['password']);
$otp_value = rand(1000, 9999);
if(empty($username)){
header ("Location: index.php?error=Username is required!");
exit();
}
else if(empty($password)) {
header ("Location: index.php?error=Password is required!");
exit();
}
$sql = "SELECT * FROM login_js WHERE email='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) === 1) {
$row = mysqli_fetch_assoc($result);
if($row['email'] === $username && $row['password'] === $password) {
$_SESSION['id'] = $row['id'];
$_SESSION['jsid'] = $row['jsid'];
$_SESSION['email'] = $row['email'];
$jsid = $row['jsid'];
$fname = $row['firstmiddlenames'];
$lname = $row['lastname'];
$email = $row['email'];
$sql2 = "UPDATE login_js SET loginOTP='$otp_value' WHERE email = '$username'";
$result2 = mysqli_query($conn, $sql2);
if($result2){
send_verification("$fname", "$lname", "$email", "$otp_value");
header ("Location: verification.php?email=".$email);
exit();
} else {
header ("Location: index.php?error=problem updating otp");
}
} else {
header ("Location: index.php?error=Incorrect Username or Password!");
exit();
}
}
} else {
header ("Location: index.php?error=Cannot Find the Username and Password");
exit();
}
?>