Send to multiple email accounts

Hi all,
I havent done a site in a while and I am a little stuck… I have a registration form that a user fills in and an email is sent to the user once they click on join… now I would like an email to be sent to me and another person when some one joins and not only the user as I will be doing validation manually.

Here is the register.php code (Please tell me what to put where):

<? 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']; $first_name = stripslashes($first_name); $last_name = stripslashes($last_name); $email_address = stripslashes($email_address); $username = stripslashes($username); $info = stripslashes($info); if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){ echo 'You did not submit the following required information!
'; if(!$first_name){ echo "First Name is a required field. Please enter it below.
"; } if(!$last_name){ echo "Last Name is a required field. Please enter it below.
"; } if(!$email_address){ echo "Email Address is a required field. Please enter it below.
"; } if(!$username){ echo "Desired Username is a required field. Please enter it below.
"; } include 'join_form.html'; exit(); // if the error checking has failed, we'll exit the script! } $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:
"; if($email_check > 0){ echo "Your email address has already been used by another member in our database. Please submit a different Email address!
"; 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!
"; unset($username); } include 'join_form.html'; // Show the form again! exit(); // exit the script so that we do not create this account! } 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); $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 Chantelle on 079xxxxxxx.'; } else { $userid = mysql_insert_id(); // Let's mail the user! $subject = "Your Membership at G's Bootcamp"; $message = "Dear $first_name $last_name, Thank you for registering at our website, http://www.gsbootcamp.co.za! Once we activate your memebership, you will be able to login with the following information: Username: $username Password: $random_password Thank you Gusmari This is an automated response, please do not reply!"; mail($email_address, $subject, $message, "From: Gusmari\nX-Mailer: PHP/" . phpversion()); echo 'Your membership information has been mailed to your email address! Please check it and follow the directions!'; } ?>

This is dangerous obsolete code that will not work at all in the current version of Php. The whole thing needs to be re-written from the ground up. This code cannot be “fixed”.

thanks Kevin… i downloaded it… I know its old… but i understand it… it is for a very basic simple site and it seems to be working fine… all i need is just that the code needs to send 2 extra emails once someone signs up… Just to let me know :slight_smile:

Well to be honest I say good luck getting someone to help you with obsolete code. Hackers like very basic websites. :wink: It’s really not that hard to learn new code and it would save you time. Oh well, to each its own. ::slight_smile:

Great so now i am back to square one… I just want a code to make a small members area… sends me an email when people join as well as them obviously…

This is the title of this forum section - Beginners - Learning PHP. Use this as an opportunity to learn how to do this, rather than to expect someone here to tell you how to change someone else’s out of date, bad code, to do what you want.

Some suggestions that would make the code you write vertually fool-proof (it will either work or it will tell you when and why it doesn’t) -

  1. Use php’s full opening tag - <?php

  2. Use ‘require’ instead of ‘include’ for things that your code requires for it to work.

  3. Detect that a post method form was submitted before referencing any of the form data.

  4. Don’t copy variables to other variable without a good reason. This just creates a bunch of variables and unnecessary lines of code that you have to keep track of and test. Programming isn’t a word processing exercise to see how many double-spaced pages you can produce (you won’t ever get paid by how many words your code contains.) You need to Keep It Simple (KISS) and use the least amount of logic that accomplishes a task.

  5. Validate the trimmed data before using it. Store any validation errors in a php array. The submitted email address needs to be validated that it is exactly and only one email address (so that hackers cannot submit multiple To: addresses.)

  6. If there are no validation errors, use the submitted data.

  7. Use the php PDO extension as your database api and use prepared queries when supplying data values to the sql query statement.

  8. Don’t Repeat Yourself (DRY.) The form processing code should be near the top of your file. The html form should be part of the html document and be near the end of your file. Any php code that gets or produces dynamic output for the page should come before the start of the html document (you don’t actually have any in this process.)

  9. Use exceptions to handle database statement errors and let php catch the exceptions and handle what happens with the error information. This will eliminate the need to have logic in your code to test the result from each database statement that can fail, which you don’t consistently have now, and since you should NOT output the raw database error messages to the visitor on a live site, letting php handle the errors will let php use it’s error_reporting, log_errors, and display_errors settings to control where the error information goes.

  10. There’s a race condition that exists with concurrent visitors using the same values, when you try to SELECT data first, in order to find if it’s already in the database. Since your database table should enforce unique values, you can still get a query error for this condition, that your code needs to handle. Since you have to have code to handle duplicate errors, you might as well eliminate the code trying to SELECT the data first and just try to INSERT the data (KISS.)

  11. Use php’s password_hash() and password_verify() for password hashing.

  12. htmlspecialchars() is an OUTPUT function. It is used when you OUTPUT data in a html context. If is NOT used on data when you input it into a database table.

  13. Your email message mentions activating the account. There is nothing in your database table structure related to account status. You need a status column that will hold values related to the state of the account, that’s set to an initial value that corresponds to a pending activation.

  14. The data values you put into the email message need to be passed through htmlentities() so that there’s no chance of an email client rendering any code in them.

  15. You need to test if the mail() function returned a true value before outputting a success message. Add an error message to the array holding the errors and output the errors in the html document. Set up a success message if the mail() call was successful and output it in the html document.

Once you get the code updated, then you can SIMPLY add other email addresses to send to. You may want to use a BCC: mail header for these, so that they won’t be included in the email to the visitor. The list of To: addresses will be shown in each email that gets sent.

Sponsor our Newsletter | Privacy Policy | Terms of Service