Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2010, 03:41 AM
Registered User
 
Join Date: Jun 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default PHP mail() problem!

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Send mail and attachments with PHP mail function Lofa Beginning PHP 1 June 2nd, 2008 03:24 PM
Problem with sending mail in php Paula222 Beginning PHP 4 July 16th, 2006 03:38 PM
mail problem in php yertanian PHP How-To 5 January 18th, 2005 10:54 PM
PHP mail() problem... mljones Beginning PHP 2 October 6th, 2004 04:49 AM
php mail() problem saicon Beginning PHP 2 August 18th, 2003 12:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.