James AraSmith
James AraSmith

Reputation: 1

I am having problems sending mySQL information in an email message

I want to send an email with a user's email and password to their email account. This is the code I have:

    if ($ct2 == 1){
        while ($row = mysql_fetch_array($sql)){
            $emusername = $row2["username"];
            $empassword = $row2["password"];
            $firstname = $row2["first_name"];
            $to = $row["email"];
            $subject = "User Name And Password";
            $message =  $firstname 
                . "Here is your username and password,\n Username" 
                . $emusername . "\n Password" 
                . $empassword . "\n\n";
            $headers = "From: [email protected]\r\nReply-To: Forgot username and password.";

            mail($to,$subject,$message,$headers);

//The email is sent but none of the $... information in the $message is sent. I can't figure out how to get it to send the info.

Upvotes: 0

Views: 98

Answers (3)

Brighid McDonnell
Brighid McDonnell

Reputation: 4343

The real answer, as @SLacks points out in the comments, is that you should

  • Never, ever, ever store passwords in your database
    • Store the hashes of passwords instead - use bcrypt, scrypt, or similar.
  • Never, ever, ever email passwords to users
    • Email users a unique token instead that they can use to reset their password.

As for your primary question of "why don't my variables show up in the concatenated string," you probably want to use heredoc syntax for your big string, and to double-check that your SQL query is giving you back the values that you expect.

Upvotes: 0

Higgsy
Higgsy

Reputation: 324

Your array variable is $row not $row2. And use single quotes in your index. .... And listen to SLaks. Haha! Bad idea to keep passwords that aren't encrypted.

Upvotes: 2

pbond
pbond

Reputation: 1927

Not so surprising, you're fetching your rows into $row, and you're trying to retrieve the data out of $row2. Furthermore, as stated by @SLaks, never send password by email. And on a sidenote, since you're retrieving the password out of the database, never store passwords unencrypted. And never store personal information of users unencrypted either, if someone hacks into your database, he'll have all personal information directly.

Upvotes: 0

Related Questions