?
Solved

email random password php

Posted on 2008-10-25
4
Medium Priority
?
410 Views
Last Modified: 2013-12-13
I am just wanting to send the user a random password to their email.

This is for a simple email verification script.

I had thought that maybe the random number is not being generated...
so I put it into an "if" statement.

It does get fired off in the right order.
I have tried send other vars into the email.

Just that random password is the only thing not being sent.
Everything else is.

Any suggestions?
<?php
 
function sendEmail(){  
 
$senderName = 'myCompany'; 
$senderEmail = 'info@myCompany.com'; 
$toEmail = 'me@myCompany.com'; 
$mail_body =  $newPass;
 
 
$subject = 'Your password'; 
$header = 'From: '. $senderName . ' <' . $senderEmail . '>\r\n';  
 
mail($toEmail, $subject, $mail_body, $header); 
 
};
	
 
 
 
 
function createRandomString($length = 7) {    
      $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789';    
      srand((double)microtime()*1000000);    
      $i = 0;    $pass = '' ;    
      while ($i <= $length) {        
            $num = rand() % 33;        
            $tmp = substr($chars, $num, 1);        
            $pass = $pass . $tmp;        $i++;    
      }    
      return $pass;
}
$newPass =  createRandomString();
$newEncPass = md5($newPass);
 
$newPass;
 
if($newPass != '') {
		
		
			echo $newPass;
						
			sendEmail();	
 
	
	}
 
 
?>

Open in new window

0
Comment
Question by:cntmedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:nick_bal
ID: 22806010

$senderName = 'myCompany'; 
$senderEmail = 'info@myCompany.com'; 
$toEmail = 'me@myCompany.com'; 
 
$chars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
 
    while ($i <= 7) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
 
$mail_body = $pass;
 
 
$subject = 'Your password'; 
$header = 'From: '. $senderName . ' <' . $senderEmail . '>\r\n';  
 
mail($toEmail, $subject, $mail_body, $header); 

Open in new window

0
 

Author Comment

by:cntmedia
ID: 22806106
That does work... but what about inside of a function?
I understand your trying to keep the code simple...

But this is being put back into several things.
Just starting to build my own classes and includes to cut down on some things.

Not really understanding why it didn't work inside the function.

I am guessing that I forgot to set it to return the actual value vs. just having it spit out vars at runtime.
0
 
LVL 3

Accepted Solution

by:
raminhos earned 200 total points
ID: 22806157
You waren't passing the $newpass var to the function sendmail...
<?php
 
function sendEmail($newPass){
  $senderName = 'myCompany';
  $senderEmail = 'info@myCompany.com';
  $toEmail = 'me@myCompany.com';
  $mail_body =  $newPass;
  $subject = 'Your password';
  $header = 'From: '. $senderName . ' <' . $senderEmail . '>\r\n';
  mail($toEmail, $subject, $mail_body, $header);
}
 
function createRandomString($length = 7) {
  $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789';
  srand((double)microtime()*1000000);
  $i = 0;    $pass = '' ;
  while ($i <= $length) {
    $num = rand() % 33;
    $tmp = substr($chars, $num, 1);
    $pass = $pass . $tmp;        $i++;
  }
  return $pass;
}
 
$newPass =  createRandomString();
$newEncPass = md5($newPass);
 
$newPass;
 
if($newPass != '') {
 
  //echo $newPass;
 
  sendEmail($newPass);
 
}
 
 
?>

Open in new window

0
 

Author Closing Comment

by:cntmedia
ID: 31510042
Thanks.  I am switching over from Actionscript.
Have some background in OOP, but still fresh to PHP on somethings. -- Small problem, but thanks again!

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question