Skip to main content
Known Participant
January 10, 2011
Question

Reset forgotten password glitch

  • January 10, 2011
  • 9 replies
  • 1379 views

Working through the Powers' latest book. Everything seemed to be working fine in local testing environment recently. When I uploaded the files tonight to the remote server and did a test of the forgotten password feature, I'm getting a message at the top of the Request Received screen (p. 293) that says "Connection refused" and not seeing the reset email come in. The request_reset.php code is below:

<?php
$errors = FALSE;
$result = FALSE;
if ($_POST) {
  require_once('library.php');
  require_once('mail_connector.php');
  try {
     $val = new Zend_Validate_EmailAddress();
     if (!$val->isValid($_POST['email'])) {
       $errors = TRUE;
     }
     if (!$errors) {
       $sql = $dbRead->quoteInto('SELECT user_id, first_name, last_name, email FROM users WHERE email = ?', $_POST['email']);
       $result = $dbRead->fetchRow($sql);
       if (!$result) {
          $errors = TRUE;
       } else {
        // update database and send mail
          $token = md5(uniqid(mt_rand(), TRUE));
          $data = array('token' => $token);
          $where = $dbWrite->quoteInto('email = ?', $_POST['email']);
          $dbWrite->update('users', $data, "user_id = {$result['user_id']}");     
       }
       $mail = new Zend_Mail('UTF-8');
       $mail->addTo($result['email'], "{$result['first_name']} {$result['last_name']}");
       $mail->setSubject('Instructions for resetting your password');
       $mail->setFrom('peter_mcdonald@comcast.net', 'Wild Felid Association');
       $link = "http://www.conmolbry.com/WFRMA/reset.php?id={$result['user_id']}&token=$token";
       $message = "Use the following link to reset your password. This link can be used once only. $link";
       $mail->setBodyText($message, 'UTF-8');
       $mail->send();
     }
  } catch (Exception $e) {
     echo $e->getMessage();
  }
}

Peter

This topic is closed to new replies. Start a new post to keep the conversation going.

9 replies

David_Powers
Inspiring
January 11, 2011

conmolbry wrote:

The request_reset.php code is below:

Peter

Somehow, I don't thing the code is "Peter". Looks like something is missing from your post.

bmmotionAuthor
Known Participant
January 11, 2011

Indeed. Even my post had a glitch. Try again:

<?php
$errors = FALSE;
$result = FALSE;
if ($_POST) {
  require_once('library.php');
  require_once('mail_connector.php');
  try {
     $val = new Zend_Validate_EmailAddress();
     if (!$val->isValid($_POST['email'])) {
       $errors = TRUE;
     }
     if (!$errors) {
       $sql = $dbRead->quoteInto('SELECT user_id, first_name, last_name, email FROM users WHERE email = ?', $_POST['email']);
       $result = $dbRead->fetchRow($sql);
       if (!$result) {
          $errors = TRUE;
       } else {
        // update database and send mail
          $token = md5(uniqid(mt_rand(), TRUE));
          $data = array('token' => $token);
          $where = $dbWrite->quoteInto('email = ?', $_POST['email']);
          $dbWrite->update('users', $data, "user_id = {$result['user_id']}");     
       }
       $mail = new Zend_Mail('UTF-8');
       $mail->addTo($result['email'], "{$result['first_name']} {$result['last_name']}");
       $mail->setSubject('Instructions for resetting your password');
       $mail->setFrom('peter_mcdonald@comcast.net', 'Wild Felid Association');
       $link = "http://www.conmolbry.com/WFRMA/reset.php?id={$result['user_id']}&token=$token";
       $message = "Use the following link to reset your password. This link can be used once only. $link";
       $mail->setBodyText($message, 'UTF-8');
       $mail->send();
     }
  } catch (Exception $e) {
     echo $e->getMessage();
  }
}

David_Powers
Inspiring
January 11, 2011

The "Connection refused" message is coming from the catch block. It's telling you that the connection was refused by the mail server.

Looking at your script, I see that you're including mail_connector.php. I presume that is still pointing to the SMTP server you were using for local testing. I'm surprised that it's rejecting a connection from your remote server, but it might be something to do with relay policies.

If you re-read page 261, you'll see that by default Zend_Mail hands the message and headers directly to the server's MTA. You don't need to create a transport object unless you need to use SMTP authentication or supply the fifth argument to mail().

I suggest you comment out the line that includes mail_connector.php.

// require_once('mail_connector.php');

If that works, you're in business. If it doesn't, remove the double slash, and change the content of mail_connector.php like this:

<?php
$transport = new Zend_Mail_Transport_Sendmail('-fyou@example.com');
Zend_Mail::setDefaultTransport($transport);

Replace "you@example.com" with your own email address.

The script should then send the email using the local mail transport agent.