JDaveth
JDaveth

Reputation: 37

Password recovery not sending email

Password recovery not sending email. But it always say that email was sent. Please help me.

<?php
session_start();
include_once 'dbconnect.php';

if (isset($_POST['submit'])) {

    $email = $_POST['email'];
    $query = mysql_query("SELECT * FROM users WHERE email='" . $email . "' ") or die(mysql_error());
    $run = mysql_affected_rows();
    $password = substr(hash('sha512', uniqid(rand(), 1)), 3, 10);
    $pass = hash('sha512', $password);
    if ($run != 0) {
        $res = mysql_fetch_array($query);
        $to = "$email";
        $subject = "Obnovení hesla";
        $body = "Dobrý den $res->username, vaše nové heslo je $password";
        $additionalheader = "From: <[email protected]>";
        mail($to, $subject, $body, $additionalheader);
        if ($email) {

            echo"<script>alert('Email byl úspěšně odeslán.')</script>";

        } else {

            echo"<script>alert('Emailová adresa $email neexistuje.')</script>";
        }
    }
    $sql = mysql_query("UPDATE users SET password='$pass' WHERE email = '$email'")or die(mysql_error());
}
?>

Upvotes: 0

Views: 85

Answers (3)

1ns4n3
1ns4n3

Reputation: 169

For checking response properly try below code:

if(mail($to, $subject, $body, $additionalheader)) {
    echo "mail sent";
}
else {
    echo "mail not sent";
}

You can also print response of $res to know the exact error. Also, read following post. How to test if PHP mail() has successfully delivered mail

Upvotes: 1

Aamir Sarwar
Aamir Sarwar

Reputation: 271

Is mail function working on your server?

Also check your spam folder.

As Jon Stirling said, replace

$run = mysql_affected_rows();

wtih

$run = mysql_num_rows($query);

Upvotes: 0

Jonnix
Jonnix

Reputation: 4187

You're using the wrong function to check whether there were any returned rows.

mysql_affected_rows() returns the number of rows affected by an INSERT / UPDATE / DELETE.

mysql_num_rows() will return the number of rows returned by a query, which is what you're actually wanting to check.

Upvotes: 0

Related Questions