0

I have written a basic HTML form with few entries for the user to enter and register. Now the code needs to work in such a manner that after registration it has to send a verification mail on the email id entered with the verification link given by token. The code looks fine to me and I don't feel any error but I don't receive any emails with a verification link, neither in inbox nor do in spam.

Php Code for Register.php:

 <?php
$msg = "";
use PHPMailer\PHPMailer\PHPMailer;

if (isset($_POST['submit'])) {
    $con = new mysqli('localhost', 'root', '', 'research_phpEmailConfirmation');

    $name = $con->real_escape_string($_POST['name']);
    $email = $con->real_escape_string($_POST['email']);
    $password = $con->real_escape_string($_POST['password']);
    $cPassword = $con->real_escape_string($_POST['cPassword']);

    if ($name == "" || $email == "" || $password != $cPassword)
        $msg = "Please check your inputs!";
    else {
        $sql = $con->query("SELECT id FROM users WHERE email='$email'");
        if ($sql->num_rows > 0) {
            $msg = "Email already exists in the database!";
        } else {
            $token = 'qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM0123456789!$/()*';
            $token = str_shuffle($token);
            $token = substr($token, 0, 10);

            $hashedPassword = password_hash($password, PASSWORD_BCRYPT);

            $con->query("INSERT INTO users (name,email,password,isEmailConfirmed,token)
                VALUES ('$name', '$email', '$hashedPassword', '0', '$token');
            ");

            include_once "PHPMailer/PHPMailer.php";

            $mail = new PHPMailer();
            $mail->setFrom('sanchit****@.com');
            $mail->addAddress($email, $name);
            $mail->Subject = "Please verify email!";
            $mail->isHTML(true);
            $mail->Body = "
                Please click on the link below:<br><br>

                <a href='http://codingpassiveincome.com/PHPEmailConfirmation/confirm.php?email=$email&token=$token'>Click Here</a>
            ";

            if ($mail->send())
                $msg = "You have been registered! Please verify your email!";
            else
                $msg = "Something wrong happened! Please try again!";
        }
    }
}
?>

Html Code for Register.php:

<body>
<div class="container" style="margin-top: 100px;">
    <div class="row justify-content-center">
        <div class="col-md-6 col-md-offset-3" align="center">

            <?php if ($msg != "") echo $msg . "<br><br>" ?>

            <form method="post" action="register.php">
                <input class="form-control" name="name" placeholder="Name..."><br>
                <input class="form-control" name="email" type="email" placeholder="Email..."><br>
                <input class="form-control" name="password" type="password" placeholder="Password..."><br>
                <input class="form-control" name="cPassword" type="password" placeholder="Confirm Password..."><br>
                <input class="btn btn-primary" type="submit" name="submit" value="Register">
            </form>
sanchit
  • 41
  • 1
  • 8
  • When you add `echo $msg;` to the end of your PHP code, does it say "You have been registered!"? Or do you get an error? Is the user created in the database? What looks strange to me: You use an autoloader to load the `PHPMailer` class, but then you also include the file, maybe this is causing troubles. – masterfloda Jan 29 '18 at 18:35
  • @masterfloda If user enters correct details while registering is says "You have been registered! Please verify your email!" Also I tried removing autoloader and include file statement too, one each at a time, but both gives an error. – sanchit Jan 29 '18 at 19:08

0 Answers0