-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsend-password-reset.php
50 lines (29 loc) · 969 Bytes
/
send-password-reset.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
<?php
$email = $_POST["email"];
$token = bin2hex(random_bytes(16));
$token_hash = hash("sha256", $token);
$expiry = date("Y-m-d H:i:s", time() + 60 * 30);
$mysqli = require __DIR__ . "/database.php";
$sql = "UPDATE user
SET reset_token_hash = ?,
reset_token_expires_at = ?
WHERE email = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("sss", $token_hash, $expiry, $email);
$stmt->execute();
if ($mysqli->affected_rows) {
$mail = require __DIR__ . "/mailer.php";
$mail->addAddress($email);
$mail->Subject = "Password Reset";
$mail->Body = <<<END
Click <a href="http://example.com/reset-password.php?token=$token">here</a>
to reset your password.
END;
try {
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer error: {$mail->ErrorInfo}";
}
}
echo "Message sent, please check your inbox.";