Untitled

mail@pastecode.io avatar
unknown
php
a year ago
2.7 kB
4
Indexable
Never
<?php
session_start();
$servername = "localhost";
$username = "admin1";
$password = "angelo123";
$dbname = "taskydb";


// Create a connection to the database
$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if(isset($_POST["submit"])){
    $email = $_POST['email'];

}
else{
    header("Location:../index.php");
}


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require '../PHPMailer/Exception.php';
require '../PHPMailer/PHPMailer.php';
require '../PHPMailer/SMTP.php';


//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'tasky.reset@gmail.com';                     //SMTP username
    $mail->Password   = '&&&&&&&&&&&&&&&&';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('tasky.reset@gmail.com', 'Reset');
    $mail->addAddress($email);     //Add a recipient
    $mail->addAddress('ellen@example.com');               //Name is optional
    
    $code = substr(str_shuffle('1234567890QWERTYUIOPASDFGHJKLZXCVBNM'),0,10);

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Password Reset';
    $mail->Body    = 'To reset your password click on <a href="http://localhost/finals/resetform.php?code='.$code.'">this link</a>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $verifyq = $conn->query("SELECT * FROM users WHERE email ='$email'");

    if($verifyq->num_rows){
        $codeq = $conn->query("UPDATE users SET code='$code' WHERE email = '$email'");

        $mail->send();
        echo "<script>alert('An Email has been Sent to you for reseting your password!');</script>";
        echo "<script>setTimeout(function(){window.location.href='../index.php';}, 3000);</script>";
    }
    $conn->close();
    
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}