<?php

require_once 'lib/swift_required.php';

$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$headers = "From: Sebastian@Thorn.se" . "\r\n";

$conn = new mysqli('localhost', 'root', 'rushndie', 'bookmarks');
$username = $_POST['username']
$result2 = $conn->query("select * from user
where username='".$username."'
and passwd = sha1('".$password."')");

$row = $result->fetch_object();

$email = $row->email;
$message = Swift_Message::newInstance()
->setFrom(array('sebastian@thorn.se' => 'SEBASTIAN JOHANSSON THORN'))
->setSubject('Password')
->setTo(array('$email'))
->setBody('"Ditt lösenord är: ".$password."\r\n"
."Tips: Ändra ditt lösenord när du loggat in.\r\n";');
$result = $mailer->send($message);

print_r($result);

?>

Where have I done this wrong?

I want to collect the password from the database
and then send it to the user.

Recommended Answers

Please format your code block correctly using the code or inline code

Jump to Post

All 4 Replies

Please format your code block correctly using the code or inline code

forgot_passw2.php

    <form class="cygnaform" action="forgot_passwd.php" method="post">
        <table width="188" cellpadding="2" cellspacing="0">
          <tr>
            <td><h4><br>
              Återställ lösenord</h4>
              <span class="text1">Skriv ditt användarnamn:</span><br><input class="cygnaTextbox textarea" type="text" name="username" size="16" maxlength="16"/><br><br>      <input type="submit" value="Skicka nytt!"/>  </td></tr>
        </table></form>

forgot_passw.php

    <?php

    require_once 'lib/swift_required.php';

    $transport = Swift_MailTransport::newInstance();
    $mailer = Swift_Mailer::newInstance($transport);
    $headers = "From: Sebastian@Thorn.se" . "\r\n";

    $conn = new mysqli('localhost', 'root', 'password', 'bookmarks');
    $username = $_POST['username']
    $result2 = $conn->query("select * from user
    where username='".$username."'
    and passwd = sha1('".$password."')");

    $row = $result->fetch_object();

    $email = $row->email;
    $message = Swift_Message::newInstance()
    ->setFrom(array('sebastian@thorn.se' => 'SEBASTIAN JOHANSSON THORN'))
    ->setSubject('Password')
    ->setTo(array('$email'))
    ->setBody('"Ditt lösenord är: ".$password."\r\n"
    ."Tips: Ändra ditt lösenord när du loggat in.\r\n";');
    $result = $mailer->send($message);

    print_r($result);

    ?>

Line 11 => $result2, but you then use $result in line 15 & 24
Line 13 => You check where against both username and passwd, but you only get POST data for username.

Other than that i have not used "Swift_Mailer" before, so maybe someone can shed some light, or have you reviewed the HOWTO where the class was downloaded from?

  $result2 = $conn->query("select * from user
     where username='".$username."'
     and passwd = sha1('".$password."')");//forgot password doesn't contain password

Firstly person has forgot his password,how will he enter password.

It must be

$result2 = $conn->query("select * from user
    where username='".$username."'");

Secondly,
$password is undefined.
after line 17 add this line,

$password=$row->passwd;//assuming passwd is field containing password.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.