-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_availability.php
More file actions
63 lines (55 loc) · 2.44 KB
/
check_availability.php
File metadata and controls
63 lines (55 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php'; // Ensure you have installed PHPMailer via Composer
require_once("includes/config.php");
session_start();
// Email availability check and OTP sending
if (!empty($_POST["emailid"])) {
$email = $_POST["emailid"];
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo "error : You did not enter a valid email.";
} else {
$sql = "SELECT EmailId FROM tblusers WHERE EmailId=:email";
$query = $dbh->prepare($sql);
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->execute();
if ($query->rowCount() > 0) {
echo "<span style='color:red'> Email already exists.</span>";
echo "<script>$('#submit').prop('disabled',true);</script>";
} else {
// Generate OTP
$otp = rand(100000, 999999);
$_SESSION['otp'] = $otp;
$_SESSION['email'] = $email;
// Store OTP in database
$sql = "INSERT INTO otp_verification (EmailId, OTP, Expiry) VALUES (:email, :otp, DATE_ADD(NOW(), INTERVAL 10 MINUTE))";
$query = $dbh->prepare($sql);
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->bindParam(':otp', $otp, PDO::PARAM_INT);
$query->execute();
// Send OTP via email
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Set your SMTP host
$mail->SMTPAuth = true;
$mail->Username = 'hostelatib@gmail.com'; // SMTP username
$mail->Password = 'kwpc qhay eydk bqse'; // SMTP password
$mail->SMTPSecure = 'tls'; // Use TLS
$mail->Port = 587; // SMTP port
$mail->setFrom('hostelatib@gmail.com', 'DriveMate');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = 'Your OTP Code';
$mail->Body = "Your OTP for email verification is: <b>$otp</b>";
$mail->send();
echo "<span style='color:green'> Email available OTP sent, Please verify.</span>";
echo "<script>$('#submit').prop('disabled',false);</script>";
} catch (Exception $e) {
echo "<script>alert('Mailer Error: {$mail->ErrorInfo}');</script>";
}
}
}
}
?>