Page 1 of 1

Register user and send a confirmation email

Posted: Mon May 11, 2009 12:51 pm
by kuzermedia
I have created a page that allows users to sign up to my site, it connects to a mysql database and enters in username, password and email to my table.
I was wondering if anyone knows how to send an automatic email confirming the user name and password they have just chosen.
Thanks in advance

Heres the php code i have so far

Code: Select all

<?php
// execute script only if form has been submitted
if (array_key_exists('register', $_POST)) {
  // remove backslashes from the $_POST array
  include('../includes/corefuncs.php');
  include('../includes/conn_mysql.inc.php');
  nukeMagicQuotes();
  // check length of username and password
  $username = trim($_POST['username']);
  $pwd = trim($_POST['pwd']);
  $email = trim($_POST['email']);
  // initialize error array
  $message = array();
  // check length of username
  if (strlen($username) < 6 || strlen($username) > 15) {
    $message[] = 'Username must be between 6 and 15 characters';
    }
  // validate username
  if (!ctype_alnum($username)) {
    $message[] = 'Username must consist of alphanumeric characters with no spaces';
    }
  // check password
  if (strlen($pwd) < 6 || preg_match('/\s/', $pwd)) {
    $message[] = 'Password must be at least 6 characters with no spaces';
    }
  // check that the passwords match
  if ($pwd != $_POST['conf_pwd']) {
    $message[] = 'Your passwords don\'t match';
    }
  // if no errors so far, check for duplicate username
  if (!$message) {
    // connect to database as administrator
    $conn = dbConnect('admin');
    // check for duplicate username
    $checkDuplicate = "SELECT user_id FROM users
                       WHERE username = '$username'";
    $result = mysql_query($checkDuplicate) or die(mysql_error());
    $numRows = mysql_num_rows($result);
    // if $numRows is positive, the username is already in use
    if ($numRows) {
      $message[] = "$username is already in use. Please choose another username.";
      }
    // otherwise, it's OK to insert the details in the database
    else {
      // create a salt using the current timestamp
      $salt = time();
    
      // insert details into database
      $insert = "INSERT INTO users (username, salt, pwd, email)
                 VALUES ('$username', $salt, '$pwd', '$email')";
      $result = mysql_query($insert) or die(mysql_error());
      if ($result) {
        $message[] = "Account created for $username";
        }
      else {
        $message[] = "There was a problem creating an account for $username";
        }
      }
    }
  }
 
require_once ("connection.php");
 
// catch field data
$userid = $_POST['userid'];
$password = $_POST['password'];
$email = $_POST['email'];
$submitted = $_POST['submitted'];
 
if ($userid && $password){
 
$query = sprintf("SELECT * FROM users WHERE username='$userid' AND pwd='$password'");
$result = @mysql_query ($query);
$rowAccount = @mysql_fetch_array($result);
}
 
if ($rowAccount){
 
$_SESSION['id'] = $rowAccount['user_id'];
 
header("location:members.php");
exit;
 
}elseif($submitted){
 
echo "The Username / Password does not exist";
}
 
?>

Re: Register user and send a confirmation email

Posted: Mon May 11, 2009 12:55 pm
by Defiline

Re: Register user and send a confirmation email

Posted: Mon May 11, 2009 1:04 pm
by ldougherty
Simple example of sending mail via PHP

Code: Select all

 
<?php
$to = 'user@domain.com';
$subject = 'the subject';
$from = 'email@domain.com';
$message = 'hello';
 
if(mail($to, $subject, $message, "From: $from"))
  echo "Mail sent";
else
  echo "Mail send failure - message not sent"; ?>