I have this script which works fine, and changes the password.

Admin is logged in and has a session id.

After admin has changed the password, i want to email the new changed password, but it doesnt send the email - can someone see why?

The form inputs are:
current password....
new password....
confirm new password....

This is the script:

<?php //error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING); ?>
<?php 
$password ='';
// Validate current pass
// only checks if theres a value
if(!empty($_POST['bekræftpass'])){
$currentPass = sha1(mysqli_real_escape_string($connection, $_POST['bekræftpass']));	
} else{ // Error msg.
$currentPassEmpty ='<br /><small class="error">Indtast en værdi i feltet!</small>';	
}
// Validate new pass
if( 
ctype_alnum($_POST['pass1']) // KUN tal og bogstaver 
&& strlen($_POST['pass1'])>5 // Mindst 6 karakterer lang 
&& strlen($_POST['pass1'])<21 // Højst 20 karakterer lang 
&& preg_match('`[A-Z]`',$_POST['pass1']) // Indeholder mindst et stort bogstav 
&& preg_match('`[a-z]`',$_POST['pass1']) // Indeholder mindst et lille bogstav 
&& preg_match('`[0-9]`',$_POST['pass1']) // Indeholder mindst et tal
){ 
// Valid!!
$new_password = mysqli_real_escape_string($connection, $_POST['pass1']);
$ValidPassword = '<br /><small class="error">Password er gyldigt!</small>';
}else{ 
// Not Valid!!
$InvalidPassword = '<br /><small class="error">Ugyldigt Password</small>';
}
// Checks if the two pass are alike
if($_POST['pass1']!=$_POST['pass2']){
	$passwordDoesntMatch =	'<br /><small class="error">De to password er ikke ens!</small>';
} else {
if($_POST['pass1']==$_POST['pass2']){
$password = sha1(mysqli_real_escape_string($connection, $_POST['pass1']));
}

// Validation done! *******************************

// Check if user or pass exists in DB
if(empty($currentPassEmpty) &&
empty($InvalidPassword) && 
empty($passwordDoesntMatch)){ // No errors..
	
$query = "SELECT id, email FROM users WHERE pass='$currentPass' AND id={$_SESSION['id']}";	
$result = mysqli_query($connection, $query);
while($row=mysqli_fetch_array($result)){
$email = $row['email'];	// The email that recieves the changed password
}

if(mysqli_num_rows($result)!=1){ // No users found!
$CurrentPassFindesIkke = 'Det indtastede password eksisterer ikke i systemet';	
}

if(mysqli_num_rows($result)==1) { // 1 user, update and email the changed password
$updateQuery = "UPDATE users SET pass='$password' WHERE id={$_SESSION['id']} LIMIT 1";
if($result = mysqli_query($connection, $updateQuery)){
$PasswordChanged = '<br />Dit Password er blevet ændret og sendt til din email';	

$to = $email; // The email adress from the above select - DOESNT GET SELECTED IT SEEMS?
$subject = "Dit ændrede password til admin!";
$message = 'Dit ændrede password til login er: <b>'.$new_password.'</b><br /><br />
Med Venlig Hilsen<br />
enkelt-webdesign.dk';
$headers = "Content-type: text/html\r\n";
$headers .= "<h3 style=\"color:#5581aa;\">Dit password er blevet ændret!</h3>";
$headers .= "<hr />";

mail($to,$subject,$message,$headers);
			}
		} 
	} 
} 
?>

That script work on live server or in your local server? If work in local server, sent email will not work. You must install email server in your localhost.

It is on a live server, not localhost :-)

I cant see why it is not sending?

When I try to echo out the email, I get nothing too? (the select statement from line:42-46)
But the select query looks fine, from here!??

And I tried changing the $to - to my own email adress, and the email still doesnt send, which I dont see why, because if theres something messed up in the query where i get the email adress from the DB - that shouldnt be a problem when i write a "hard coded" email adress in the $to variable?

This is i have,

$Name = "User Created | leakbali - Web Tutorials"; //senders name
$email = "w3@leakbali.com"; //senders e-mail adress
$recipient = $username; //recipient
$mail_body = "Your account has been registered,
			
	Username : $username
	Password : $password

______________________________________________
2006 - 2011 www.leakbali.com - Web Tutorials
"; //mail body

$mail_body = strip_tags($mail_body);
$subject = "User Created - Leakbali.com"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
mail($recipient, $subject, $mail_body, $header); //mail command

I think your code is work, do you mean the mail not work or your mail work but new password not sent?

The email doesnt work it seems.

And also for some reason, when I try to echo out the $email taken from the DB (42-46), nothing is being echo'ed out. But I think the query looks just like it should.

But then I tried to changed the recipient to my own emial, and still didnt recieve the mail.

And the email script works fine in other files.

I have to take a look at my variables maybe, to see if any gets overwritten somewhere, but I dont think it does.

hmm, mystery for me!

Be a part of the DaniWeb community

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