-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp.php
68 lines (45 loc) · 1.43 KB
/
otp.php
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
64
65
66
67
68
<?php
session_start();
require 'vendor/autoload.php';
header('Content-Type: application/json');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "signup";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
echo json_encode(['success' => false, 'error' => 'Database connection failed']);
exit();
}
if (!isset($_SESSION['email'])) {
echo json_encode(['success' => false, 'error' => 'User not logged in']);
exit();
}
$email = $_SESSION['email'];
$otp = mt_rand(100000, 999999);
$_SESSION['otp'] = $otp;
$subject = "OTP for SyncSpace";
$message = "Your OTP is: $otp";
$mail = new PHPMailer\PHPMailer\PHPMailer();
try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your gmail';
$mail->Password = 'your app pw';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('your gmail', 'SyncSpace');
$mail->addAddress($email);
$mail->Subject = $subject;
$mail->Body = $message;
if ($mail->send()) {
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => 'Failed to send OTP']);
}
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => 'Mailer Error: ' . $mail->ErrorInfo]);
}
$conn->close();
?>