Hi all,
I'm a relative newbie to PHP - only been using it properly for a few months. I've been working on a new site for a client and used the mail() function to create a feedback form. This works perfectly and the client is receiving feedback emails without any problems.
I've also got a login area on the site, and am using md5 to encrypt the user passwords. Because of this I'm building a password reset function for those users who have forgotten their password, which should email them their new password once it's been reset. The PHP and SQL code for generating a random password and updating the database works perfectly, but I'm having problems with the mail() function.
With the feedback form I've used the following code:
Code:
$email_to = "[email protected]";
$name = $_POST["name"];
$email_from = $_POST["fromemail"];
if ($_POST["sendcopy"]) { $email_to .= ";" . $email_from; }
$message = $_POST["message"];
if (!$_POST["subject"]) {
$email_subject = "Feedback from your website";
} else {
$email_subject = $_POST["subject"];
}
$headers =
"From: $email_from .\n";
"Reply-To: $email_from .\n";
$message = "You have received a contact from your website.\r\nName: ". $name . "\r\nMessage: " . $message;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
if ($sent)
{
header("Location: contact.php?msg=sentok");
} else {
header("Location: contact.php?msg=+error");
}
So, based on that working code I created the following for the new password notification:
Code:
...SQL CODE TO GET USER DETAILS - IF THEY EXIST THE FOLLOWING IS EXECUTED...
$email_to = $row["emailfield"];
$name = $row["namefield"];
$username = $row["usernamefield"];
$password = createRandomPassword();
....SQL CODE TO UPDATE PASSWORD...
$email_from = "[email protected]";
$email_subject = "Website Login";
$headers =
"From: $email_from .\n";
"Reply-To: $email_from .\n";
$message = "Your password has been reset to ". $password . ".\r\nYour username is ". $username;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
if ($sent)
{
echo "email sent";
//header("Location: download.php?msg=+registered");
} else {
echo "email not sent";
//header("Location: forgotpwd.php?msg=error");
}
Although the password is reset in the database I don't receive an email, and the page displays the "email not sent" message, which indicates that there's a problem in the mail creation. The site is hosted with Fasthosts, and according to their documentation the $email_from OR $email_to has to be an email address that exists, which it does.
Can anyone help me to get this working? It's been driving me mad for a few days!
Thanks in advance!