· 5 years ago · Mar 06, 2020, 08:48 AM
1case 'forgot_pass':
2
3 $mail = new PHPMailer(true);
4
5 $error = [];
6
7 $secretKey = "6LdEzKUUAAAAAEyy6kaBCBTnxNNYME-mAyb6w-R2";
8 $responseKey = $_POST['g-recaptcha-response'];
9 $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey";
10 $response = file_get_contents($url);
11 $res = json_decode($response);
12 $res1 = $res->success;
13
14 $email = $_POST["email"];
15
16 if (empty($email)) {
17 $err = 'Email is Required';
18 array_push($error, $err);
19 }
20
21 if ($res1 != "1") {
22 $err = 'Captcha is Required';
23 array_push($error, $err);
24 }
25
26 if (empty($error)) {
27
28 try {
29
30 $pdo->beginTransaction();
31
32 $sql = "SELECT * FROM user WHERE email = :email";
33 $stmt = $pdo->prepare($sql);
34
35 if ($stmt->execute(['email' => $email])) {
36 if ($stmt->rowCount() > 0) {
37 $token = md5(time() . $email);
38 $query1 = "UPDATE user SET token = :token WHERE email = :email LIMIT 1";
39 $qstmt = $pdo->prepare($query1);
40
41 if ($qstmt->execute(['token' => $token, 'email' => $email])) {
42 #####################################
43 $mail->SMTPOptions = array(
44 'ssl' => array(
45 'verify_peer' => false,
46 'verify_peer_name' => false,
47 'allow_self_signed' => true,
48 ),
49 );
50
51 $mail->SMTPDebug = 2;
52 $mail->Mailer = "smtp";
53 $mail->Host = "tls://smtp.gmail.com:587";
54 $mail->SMTPAuth = true;
55 $mail->Username = "renielgames@gmail.com";
56 $mail->Password = "Vaynelord1";
57 $mail->SMTPSecure = "tls";
58 $mail->Port = 587;
59
60 //Recipients
61 $mail->setFrom("habeasventuresinc@gmail.com");
62 $mail->addAddress($email);
63
64 // Content
65 $mail->isHTML(true);
66 $mail->Subject = "Change Email";
67 $mail->Body = "<p>to change your email</p><a href='http://localhost/habeas_final/web/user/verify.php?token=$token'>Click here!</a>";
68 $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
69
70 if ($mail->send()) {
71
72 $pdo->commit();
73
74 $_SESSION['msg'] = "Please check your email";
75 $_SESSION['alert'] = "alert alert-success";
76 header("location: ../view_forgot_password.php");
77 } else {
78 $_SESSION['msg'] = "Something went wrong";
79 $_SESSION['alert'] = "alert alert-warning";
80 header("location: ../view_forgot_password.php");
81 }
82 ####################################
83 } else {
84 $_SESSION['msg'] = "Something went wrong";
85 $_SESSION['alert'] = "alert alert-warning";
86 header("location: ../view_forgot_password.php");
87 }
88 } else {
89 $_SESSION['msg'] = "Please check your email";
90 $_SESSION['alert'] = "alert alert-warning";
91 header("location: ../view_forgot_password.php");
92 }
93 } else {
94 $_SESSION['msg'] = "Something went wrong";
95 $_SESSION['alert'] = "alert alert-warning";
96 header("location: ../view_forgot_password.php");
97 }
98 } catch (\PDOException $e) {
99 $pdo->rollBack();
100
101 $_SESSION['msg'] = $e->getMessage();
102 $_SESSION['alert'] = "alert alert-danger";
103 header("location: ../view_forgot_password.php");
104 }
105 } else {
106 $data = array(
107 'error' => $error,
108 );
109
110 $_SESSION['msg'] = $data['error'];
111 $_SESSION['alert'] = "alert alert-warning";
112 header("location: ../view_forgot_password.php");
113 }
114
115 break;