Page 1 of 1

Sending Email to admin once user Registers - HELP!

Posted: Sat Jul 16, 2011 12:21 pm
by jjfletcher90
Hello all... Need your help please!

Is it possible to have the website administrator receive an email that a customer has registered for my Members Area? Is this accomplished with the Register.php file or is this only done through the MYSQL Database? The customer is sent an email of the event - yet the admin has to go to the database manually to see if someone has registered ... not very efficient...

Here is my Register.PHP file ...

Code: Select all

<?

include 'db.php';

// Define post fields into simple variables
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$username = $_POST['username'];
$info = $_POST['info'];

/* Let's strip some slashes in case the user entered
any escaped characters. */

$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$email_address = stripslashes($email_address);
$username = stripslashes($username);
$info = stripslashes($info);


/* Do some error checking on the form posted fields */

if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){
	echo 'You did not submit the following required information! <br />';
	if(!$first_name){
		echo "First Name is a required field. Please enter it below.<br />";
	}
	if(!$last_name){
		echo "Last Name is a required field. Please enter it below.<br />";
	}
	if(!$email_address){
		echo "Email Address is a required field. Please enter it below.<br />";
	}
	if(!$username){
		echo "Desired Username is a required field. Please enter it below.<br />";
	}
	include 'join_form.html'; // Show the form again!
	/* End the error checking and if everything is ok, we'll move on to
	 creating the user account */
	exit(); // if the error checking has failed, we'll exit the script!
}

/* Let's do some checking and ensure that the user's email address or username
 does not exist in the database */

 $sql_email_check = mysql_query("SELECT email_address FROM users WHERE email_address='$email_address'");
 $sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'");

 $email_check = mysql_num_rows($sql_email_check);
 $username_check = mysql_num_rows($sql_username_check);

 if(($email_check > 0) || ($username_check > 0)){
 	echo "Please fix the following errors: <br />";
 	if($email_check > 0){
 		echo "<strong>Your email address has already been used by another member in our database. Please submit a different Email address!<br />";
 		unset($email_address);
 	}
 	if($username_check > 0){
 		echo "The username you have selected has already been used by another member in our database. Please choose a different Username!<br />";
 		unset($username);
 	}
 	include 'join_form.html'; // Show the form again!
 	exit();  // exit the script so that we do not create this account!
 }

/* Everything has passed both error checks that we have done.
It's time to create the account! */

/* Random Password generator.

We'll generate a random password for the
user and encrypt it, email it and then enter it into the db.
*/

function makeRandomPassword() {
  $salt = "abchefghjkmnpqrstuvwxyz0123456789";
  srand((double)microtime()*1000000);
  	$i = 0;
  	while ($i <= 7) {
    		$num = rand() % 33;
    		$tmp = substr($salt, $num, 1);
    		$pass = $pass . $tmp;
    		$i++;
  	}
  	return $pass;
}

$random_password = makeRandomPassword();

$db_password = md5($random_password);

// Enter info into the Database.
$info2 = htmlspecialchars($info);
$sql = mysql_query("INSERT INTO users (first_name, last_name, email_address, username, password, info, signup_date)
		VALUES('$first_name', '$last_name', '$email_address', '$username', '$db_password', '$info2', now())") or die (mysql_error());

if(!$sql){
	echo 'There has been an error creating your account. Please contact the webmaster.';
} else {
	$userid = mysql_insert_id();
	// Let's mail the user!
	$subject = "Membership Area";
	$message = "Dear $first_name $last_name,
	Thank you for registering at our website, http://www.website.com!

	You are two steps away from logging in and accessing our exclusive members area.

	To activate your membership, please click here: http://www.website.com/members/Members_Area/activate.php?id=$userid&code=$db_password

	Once you activate your memebership, you will be able to login with the following information:
	Username: $username
	Password: $random_password

	Thanks!
	The Webmaster

	This is an automated response, please do not reply!";

	mail($email_address, $subject, $message, "From: Webmaster<admin@website.com>\nX-Mailer: PHP/" . phpversion());
	echo 'Your membership access is now being created and will be completed within minutes - information will be emailed to you with your activation instructions! If you have any additional questions please call XXX-XXX-XXXX';
}

?>
Thanks John

Re: Sending Email to admin once user Registers - HELP!

Posted: Sat Jul 16, 2011 2:24 pm
by social_experiment
jjfletcher90 wrote:Is it possible to have the website administrator receive an email that a customer has registered for my Members Area?
Yeah. Below the code where you email the user their registraion information simply add another mail function containing information relevant to the administrator and it should be sent after a successfull registration. When your script goes live, add the error control operator in front of the function (@mail()) as to minimize any information that will be displayed if the mail is not sent.

Re: Sending Email to admin once user Registers - HELP!

Posted: Sat Jul 16, 2011 2:28 pm
by twinedev
Just basically duplicate the section (minus the $userid = ... line and the last echo line) and replace $email_address with the administrators address. Then customize the $subject = and the $message= lines to be the message you want to send to them.

There is another way by putting the administrator as a BCC recipient in the headers, but you list the password in the e-mail, so I don't recommend that. (Speaking of the headers, you should be using \r\n instead of just \n in them). An example of adding this (if you decide to remove the passowrd from the message) with the correct use:

Code: Select all

mail($email_address, $subject, $message, "From: Webmaster<admin@website.com>\r\nBcc: admin@website.com\r\nX-Mailer: PHP/" . phpversion());
See http://www.php.net/manual/en/function.mail.php for more explanation/examples.

-Greg