0

I'm trying to setup my php page so that when the user enters their username in the textbox it checks my database for that username, finds their email address and then sends an email to that address. The code I currently have which unfortunately doesn't seem to be working as when i submit the page it refreshes but no email is received at the expected address;

<?php 

require_once 'header.php';
require_once 'my_connect.php';

$user = $_POST['user'];

$my_query="SELECT * from table where user = '$user'";
$result= mysqli_query($connection, $my_query);

while ($myrow = mysqli_fetch_array($result)){

echo $to = $myrow["emailaddress"]; 
$password = $myrow["password"]; 

}

$to = $email;
$password = $password;
$subject = 'Password';
$sender = 'email@mail.com';

$message = <<< EMAIL

The Password registered with account {$user} is {$password}.

EMAIL;

$header = "From:" . $sender;


if ($result):
        mail($to, $subject, $message, $header);
        $feedback = 'Email Sent';
endif;

?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mccarthy19
  • 55
  • 6
  • 5
    "doesn't seem to be working is" is not a problem description – PeeHaa May 31 '16 at 17:11
  • 1
    you didn't fetch anything, you're using `$myrow` but doing nothing with it. Plus, that POST array is unknow and if it has a value or not. check for errors; you're not doing that. – Funk Forty Niner May 31 '16 at 17:12
  • Ok, well you've been given an answer below, since you're not responding to comments (or had to take off somewhere), maybe you will for that answer. Btw, like @PeeHaa said: *"doesn't seem to be working is" is not a problem description*. – Funk Forty Niner May 31 '16 at 17:28
  • [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard May 31 '16 at 17:30
  • **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure that you [don't escape passwords](http://stackoverflow.com/q/36628418/1011527) or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard May 31 '16 at 17:30
  • @PeeHaa "doesn't seem to be working" - sorry, when i click submit, the page says email sent but nothing is received at the expected emailaddress. not sure how else to put it – Mccarthy19 May 31 '16 at 17:36
  • @Fred-ii- i've updated my code with what I think you were trying to point me towards, not sure if i've got this entirely correct? also, not sure what you meant by answer is below, i'm unable to see any? – Mccarthy19 May 31 '16 at 17:49
  • @Mccarthy19 Slight change. Do this `while($myrow = mysqli_fetch_array($result)){ echo $to = $myrow["email"]; $password = $myrow["password"]; }` but without the `endwhile` - tested; working. - (the now-deleted) Answer below earlier has been deleted for a few reasons, that's why you can't see that one anymore. – Funk Forty Niner May 31 '16 at 17:56
  • @Mccarthy19 however, you changed that POST array to `$_GET['user']`, why? where is that coming from, a form? that's unclear and if it's a post or get method and if that input has a name attribute for it. – Funk Forty Niner May 31 '16 at 18:01
  • I've tested that code with no issues, so something is failing you. ping me when you get results. good luck – Funk Forty Niner May 31 '16 at 18:07
  • @Fred-ii- that change was accidental as I was playing around, trying different things – Mccarthy19 May 31 '16 at 18:08
  • I don't know what else I can say or do here then. My tests were conclusive; successful. – Funk Forty Niner May 31 '16 at 18:09
  • @Fred-ii- if i change the $to = 'myemailaddress'; the email comes through. but when I have $to = $email; nothing comes through – Mccarthy19 May 31 '16 at 18:19
  • `mail($email, $subject, $message, $header);` – Funk Forty Niner May 31 '16 at 18:20
  • @Fred-ii- mm unfortunately not, think i'll just give up for today - thanks for your help – Mccarthy19 May 31 '16 at 18:28
  • you're welcome. I'll post an answer below for what I used because it'll be too long for a comment. You can base yourself on that. Give me a minute. – Funk Forty Niner May 31 '16 at 18:28
  • @Fred-ii- ok thanks mate – Mccarthy19 May 31 '16 at 18:34
  • You're welcome. Take a look at what I posted below and good luck, *cheers* @Mccarthy19 – Funk Forty Niner May 31 '16 at 18:39

1 Answers1

4

It has been already established that you didn't (originally) fetch anything from your query.

I am submitting the following as an example (to help you out, because it is way too long for a comment) with what I used to test your code.

Make sure that all values exist in your database. You will need to adjust the table/column names accordingly.

If the following example fails you, then you will need to find out why.

Check for errors and make sure that mail is available for you to use. Check your spam also.

See comments embedded inside code also.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

$DB_HOST = 'xxx';
$DB_USER = 'xxx';
$DB_PASS = 'xxx';
$DB_NAME = 'xxx';

$Link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($Link->connect_errno > 0) {
  die('Connection failed [' . $Link->connect_error . ']');
}

$_POST['user'] = "john"; // this is a hard-code value. I did not use a form for it

$user = $_POST['user']; // If taken from a form
                        // make sure you have a post method
                        // and that the input has the same
                        // name attribute for it. I.e.: name="user"

$my_query="SELECT * from users where username = '$user'";
$result= mysqli_query($Link, $my_query) or die(mysqli_error($Link));

while($myrow = mysqli_fetch_array($result)){

echo $to = $myrow["email"];

$password = $myrow["password"];

}


$subject = 'Password';
$sender = 'email@example.com'; // replace this with your email address

$message = <<< EMAIL

The Password registered with account {$user} is {$password}.

EMAIL;

$header = "From:" . $sender;


if ($result):
        mail($to, $subject, $message, $header);
        echo $feedback = 'Email Sent';
endif;

Plus, the method you are using is insecure as in sending passwords in email and not using a prepared statement and storing passwords as plain text.

Use prepared statements, or PDO with prepared statements, they're much safer.


Passwords

I also noticed that you may be storing passwords in plain text. This is not recommended.

Use one of the following:

Important sidenote about column length:

If and when you do decide to use password_hash() or the compatibility pack (if PHP < 5.5) https://github.com/ircmaxell/password_compat/, it is important to note that if your present password column's length is anything lower than 60, it will need to be changed to that (or higher). The manual suggests a length of 255.

You will need to ALTER your column's length and start over with a new hash in order for it to take effect. Otherwise, MySQL will fail silently.

Other links of interest:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Frankly if he's going to be exposing himself to SQL injection he should be using salted hashes. – zfj3ub94rf576hc4eegm May 31 '16 at 19:06
  • @LukeSalamone There's no need to salt, as `password_hash()` takes care of all that, which is less of a hassle really. And in PHP 7.0, salting has been removed. They also warn/caution about it. http://php.net/manual/en/function.password-hash.php – Funk Forty Niner May 31 '16 at 19:09
  • Ah, I didn't know that, thanks! But I don't understand how `password_verify()` could possibly work if `password_hash()` is just generating a salt. It seems like there isn't enough info to verify? EDIT nevermind, I figured it out. – zfj3ub94rf576hc4eegm May 31 '16 at 19:19
  • @LukeSalamone You're welcome Luke. `password_hash()` doesn't generate a salt on its own, it generates a hash with the salt embedded inside it (by default). – Funk Forty Niner May 31 '16 at 19:21
  • 1
    My lord, always with the quality answers! Nice! – Darren Jun 01 '16 at 00:45
  • 1
    Thanks Darren - @Darren *Cheers* – Funk Forty Niner Jun 01 '16 at 00:46