Hi there,

I am trying to create a "refer a friend" form for a website I am launching soon but somewhere something broke...:( It worked when I only had 5 emails fields but then I realized I would need to capture the name of the referred people as well. After I added that I keep receiving the message "Please fill in the required fields". I can't seem to see why it is doing that.

To be honest I'm a complete PHP noob and sort of patched all of the code together from various scripts I found on the web.

I would really appreciate it if someone could point out the problem to me.

Here is the html code:

<table>
        	<tbody>
            	<tr>
                	<td></td>
                    <td><label for="userName" class="nameCref">Your Name: <span class="red">*</span></label></td>
                    <td><label for="userEmail" class="emailCref">Your Email Address: <span class="red">*</span></label></td>
                </tr>
                <tr>
                	<td></td>
                    <td><input id="userName" name="userName" class="text" /></td>
                    <td><input id="userEmail" name="userEmail" class="text" /></td>
                </tr>
                <tr>
                	<td></td>
                	<td><label>Your Friend's Name:</label></td>
                    <td><label>Your Friend's Email:</label></td>
                </tr>
            	<tr>
            		<td><label for="name1 email1" class="nameCref">#1 <span class="red">*</span></label></td>
            		<td><input id="name1" name="name1" class="text" /></td>
          			<td><input id="email1" name="email1" class="text" /></td>
                </tr>
				<tr>
                	<td><label for="name2 email2" class="nameCref">#2 <span class="red">*</span></label></td>
                    <td><input id="name2" name="name2" class="text" /></td>
          			<td><input id="email2" name="email2" class="text" /></td>
                </tr>
                <tr>
                	<td><label for="name3 email3" class="nameCref">#3</label></td>
                    <td><input id="name3" name="name3" class="text" /></td>
          			<td><input id="email3" name="email3" class="text" /></td>
                </tr>
                <tr>
                	<td><label for="name4 email4" class="nameCref">#4</label></td>
                    <td><input id="name4" name="name4" class="text" /></td>
          			<td><input id="email4" name="email4" class="text" /></td>
                </tr>
                <tr>
                	<td><label for="name5 email5" class="nameCref">#5</label></td>
                    <td><input id="name5" name="name5" class="text" /></td>
          			<td><input id="email5" name="email5" class="text" /></td>
                </tr>
            </tbody>
        </table>
		<table>
          	<tbody>
            	<tr class="submit">
                	<td></td>
                	<td><input type="submit" value="Click Here to Refer Your Friends!" id="refer_submit" /></td>
            		<td></td>
                </tr>
            </tbody>
        </table>

and the refer.php script:

<?php 

if(!$_POST) exit;
 
$values = array('userName','userEmail','name1','email1','name2','email2','name3','email3','name4','email4','name5','email5');
$required = array('userName','userEmail','name1','email1','name2','email2');

$userName = $_POST['userName'];
$userEmail = $_POST['userEmail'];
$name1 = $_POST['name1'];
$email1 = $_POST['email1'];
$name2 = $_POST['name2'];
$email2 = $_POST['email2'];
$name3 = $_POST['name3'];
$email3 = $_POST['email3'];
$name4 = $_POST['name4'];
$email4 = $_POST['email4'];
$name5 = $_POST['name5'];
$email5 = $_POST['email5'];
$emails = array($email1,$email2,$email3,$email4,$email5,$userEmail);

$your_email = "wikus@mymoot.co.za";
$email_subject2 = $userName." has referred his friends!";
$email_content2 = '
<html>
<head>
  <title>'.$userName.' has referred his friends!</title>
</head>
<body>
  <p>'.$userName.' has referred the following of his friends:</p>
	<ul>
		<li>'.$name1.' '.email1.'</li>
		<li>'.$name2.' '.email2.'</li>
		<li>'.$name3.' '.email3.'</li>
		<li>'.$name4.' '.email4.'</li>
		<li>'.$name5.' '.email5.'</li>
	</ul>	
	<p>Referred by:<br />'.$userName.'</p>
	<p>Referrer Email:<br />'.$userEmail.'</p>		
</body>
</html>
';

$referred  = $name1 . ', ';
$referred  .= $name2 . ', ';
$referred  .= $name3 . ', ';
$referred  .= $name4 . ', ';
$referred  .= $name5;
$email_subject2 = $userName." wants you to win a Nintendo Wii!";
$email_content2 = '
<html>
<head>
  <title>'.$userName.' wants you to win a Nintendo Wii!!</title>
</head>
<body>
<p>Your friend '.$userName.' thought you might be interested in winnning a Nintendo Wii!</p>
<p>
To stand a chance to win this prize simply go to <a href="http://www.mymoot.co.za">www.mymoot.co.za</a> and click on the Nintendo Wii competition link.</p>
<p>
If you are not interested in winnning this prize you can safely ignore this message
and no further emails will be sent to you.</p>
<p>
Festive Greetings!<br /><br />
<a href="http://www.mymoot.co.za">MYMOOT.co.za</a>
</p>
</body>
</html>
';

$email_subject3 = $userName.", thank you for referring your friends!";
$email_content3 = '
<html>
<head>
  <title>Your Friends have been Referred!</title>
</head>
<body>
  <p>'.$userName.', by referring your friends you have now been entered into the competition.</p>
  <p>These are the friends that you have referred:</p>
	<ul>
		<li>'.$name1.' '.email1.'</li>
		<li>'.$name2.' '.email2.'</li>
		<li>'.$name3.' '.email3.'</li>
		<li>'.$name4.' '.email4.'</li>
		<li>'.$name5.' '.email5.'</li>
	</ul>	
	<p>Remember, the more friends you refer the better your chances of winning!</p>	
	<p><b>Disclaimer:</b></p>
	<p>MYMOOT never sends unsolicited emails. Your friends will only receive a notification that they have been referred and no further emails will be sent should they not wish to enter the competition.</p>
	<p>Good Luck!</p>
	<p>The MYMOOT Team</p>
</body>
</html>
';
//Headers
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' ."MYMOOT.co.za". "\r\n";


function check_email_address($email) {
		foreach($email as $i=>$emails) {
			// Check of die email veld in gevul is en validate dan die email
			if (!empty($emails)) {	
				if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $emails)) {
					return false;
				}
				$email_array = explode("@", $emails);
				$local_array = explode(".", $email_array[0]);
				for ($i = 0; $i < sizeof($local_array); $i++) {
					if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
						return false;
					}
				}
				if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
					$domain_array = explode(".", $email_array[1]);
					if (sizeof($domain_array) < 2) {
						return false;
					}
					for ($i = 0; $i < sizeof($domain_array); $i++) {
						if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
							return false;
						}
					}
				}
			}
			// Gaan aan as dit nie in gevul is nie
			else {
				continue;
			}
	}
	return true;
}

foreach($values as $value){
  if(in_array($value,$required)){
    if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
    $email_content .= $value.': '.$_POST[$value]."\n";
  }
}

# Anti-header-injection - Use before mail()
# By Victor Benincasa <vbenincasa(AT)gmail.com>

foreach($_REQUEST as $fields => $value) if(eregi("TO:", $value) || eregi("CC:", $value) || eregi("CCO:", $value) || eregi("Content-Type", $value)) exit("ERROR: Code injection attempt denied! Please don't use the following sequences in your message: 'TO:', 'CC:', 'CCO:' or 'Content-Type'.");

if(check_email_address($emails) == FALSE) {
   		echo 'PLEASE CHECK THAT YOU HAVE ENTERED VALID EMAIL ADRESSES.'; exit;
	} 
	elseif (!mail($your_email,$email_subject1,$email_content1)) {
		echo 'ERROR!';
	} 
else {
	echo 'Message Sent!';
	mail($your_email,$email_subject1,$email_content1,$headers);
	mail($referred,$email_subject2,$email_content2,$headers);
	mail($userEmail,$email_subject3,$email_content3,$headers);
}

?>

As you will probably see the script is supposed to capture all the information and then send three emails. One to me with the referrals. One to each of the referred people. And one to the referrer.

Please let me know if you need more info.

Recommended Answers

All 8 Replies

Hi there,

I am trying to create a "refer a friend" form for a website I am launching soon but somewhere something broke...:( It worked when I only had 5 emails fields but then I realized I would need to capture the name of the referred people as well. After I added that I keep receiving the message "Please fill in the required fields". I can't seem to see why it is doing that.

To be honest I'm a complete PHP noob and sort of patched all of the code together from various scripts I found on the web.

I would really appreciate it if someone could point out the problem to me.

Here is the html code:

<table>
        	<tbody>
            	<tr>
                	<td></td>
                    <td><label for="userName" class="nameCref">Your Name: <span class="red">*</span></label></td>
                    <td><label for="userEmail" class="emailCref">Your Email Address: <span class="red">*</span></label></td>
                </tr>
                <tr>
                	<td></td>
                    <td><input id="userName" name="userName" class="text" /></td>
                    <td><input id="userEmail" name="userEmail" class="text" /></td>
                </tr>
                <tr>
                	<td></td>
                	<td><label>Your Friend's Name:</label></td>
                    <td><label>Your Friend's Email:</label></td>
                </tr>
            	<tr>
            		<td><label for="name1 email1" class="nameCref">#1 <span class="red">*</span></label></td>
            		<td><input id="name1" name="name1" class="text" /></td>
          			<td><input id="email1" name="email1" class="text" /></td>
                </tr>
				<tr>
                	<td><label for="name2 email2" class="nameCref">#2 <span class="red">*</span></label></td>
                    <td><input id="name2" name="name2" class="text" /></td>
          			<td><input id="email2" name="email2" class="text" /></td>
                </tr>
                <tr>
                	<td><label for="name3 email3" class="nameCref">#3</label></td>
                    <td><input id="name3" name="name3" class="text" /></td>
          			<td><input id="email3" name="email3" class="text" /></td>
                </tr>
                <tr>
                	<td><label for="name4 email4" class="nameCref">#4</label></td>
                    <td><input id="name4" name="name4" class="text" /></td>
          			<td><input id="email4" name="email4" class="text" /></td>
                </tr>
                <tr>
                	<td><label for="name5 email5" class="nameCref">#5</label></td>
                    <td><input id="name5" name="name5" class="text" /></td>
          			<td><input id="email5" name="email5" class="text" /></td>
                </tr>
            </tbody>
        </table>
		<table>
          	<tbody>
            	<tr class="submit">
                	<td></td>
                	<td><input type="submit" value="Click Here to Refer Your Friends!" id="refer_submit" /></td>
            		<td></td>
                </tr>
            </tbody>
        </table>

and the refer.php script:

<?php 

if(!$_POST) exit;
 
$values = array('userName','userEmail','name1','email1','name2','email2','name3','email3','name4','email4','name5','email5');
$required = array('userName','userEmail','name1','email1','name2','email2');

$userName = $_POST['userName'];
$userEmail = $_POST['userEmail'];
$name1 = $_POST['name1'];
$email1 = $_POST['email1'];
$name2 = $_POST['name2'];
$email2 = $_POST['email2'];
$name3 = $_POST['name3'];
$email3 = $_POST['email3'];
$name4 = $_POST['name4'];
$email4 = $_POST['email4'];
$name5 = $_POST['name5'];
$email5 = $_POST['email5'];
$emails = array($email1,$email2,$email3,$email4,$email5,$userEmail);

$your_email = "wikus@mymoot.co.za";
$email_subject2 = $userName." has referred his friends!";
$email_content2 = '
<html>
<head>
  <title>'.$userName.' has referred his friends!</title>
</head>
<body>
  <p>'.$userName.' has referred the following of his friends:</p>
	<ul>
		<li>'.$name1.' '.email1.'</li>
		<li>'.$name2.' '.email2.'</li>
		<li>'.$name3.' '.email3.'</li>
		<li>'.$name4.' '.email4.'</li>
		<li>'.$name5.' '.email5.'</li>
	</ul>	
	<p>Referred by:<br />'.$userName.'</p>
	<p>Referrer Email:<br />'.$userEmail.'</p>		
</body>
</html>
';

$referred  = $name1 . ', ';
$referred  .= $name2 . ', ';
$referred  .= $name3 . ', ';
$referred  .= $name4 . ', ';
$referred  .= $name5;
$email_subject2 = $userName." wants you to win a Nintendo Wii!";
$email_content2 = '
<html>
<head>
  <title>'.$userName.' wants you to win a Nintendo Wii!!</title>
</head>
<body>
<p>Your friend '.$userName.' thought you might be interested in winnning a Nintendo Wii!</p>
<p>
To stand a chance to win this prize simply go to <a href="http://www.mymoot.co.za">www.mymoot.co.za</a> and click on the Nintendo Wii competition link.</p>
<p>
If you are not interested in winnning this prize you can safely ignore this message
and no further emails will be sent to you.</p>
<p>
Festive Greetings!<br /><br />
<a href="http://www.mymoot.co.za">MYMOOT.co.za</a>
</p>
</body>
</html>
';

$email_subject3 = $userName.", thank you for referring your friends!";
$email_content3 = '
<html>
<head>
  <title>Your Friends have been Referred!</title>
</head>
<body>
  <p>'.$userName.', by referring your friends you have now been entered into the competition.</p>
  <p>These are the friends that you have referred:</p>
	<ul>
		<li>'.$name1.' '.email1.'</li>
		<li>'.$name2.' '.email2.'</li>
		<li>'.$name3.' '.email3.'</li>
		<li>'.$name4.' '.email4.'</li>
		<li>'.$name5.' '.email5.'</li>
	</ul>	
	<p>Remember, the more friends you refer the better your chances of winning!</p>	
	<p><b>Disclaimer:</b></p>
	<p>MYMOOT never sends unsolicited emails. Your friends will only receive a notification that they have been referred and no further emails will be sent should they not wish to enter the competition.</p>
	<p>Good Luck!</p>
	<p>The MYMOOT Team</p>
</body>
</html>
';
//Headers
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' ."MYMOOT.co.za". "\r\n";


function check_email_address($email) {
		foreach($email as $i=>$emails) {
			// Check of die email veld in gevul is en validate dan die email
			if (!empty($emails)) {	
				if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $emails)) {
					return false;
				}
				$email_array = explode("@", $emails);
				$local_array = explode(".", $email_array[0]);
				for ($i = 0; $i < sizeof($local_array); $i++) {
					if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
						return false;
					}
				}
				if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
					$domain_array = explode(".", $email_array[1]);
					if (sizeof($domain_array) < 2) {
						return false;
					}
					for ($i = 0; $i < sizeof($domain_array); $i++) {
						if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
							return false;
						}
					}
				}
			}
			// Gaan aan as dit nie in gevul is nie
			else {
				continue;
			}
	}
	return true;
}

foreach($values as $value){
  if(in_array($value,$required)){
    if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
    $email_content .= $value.': '.$_POST[$value]."\n";
  }
}

# Anti-header-injection - Use before mail()
# By Victor Benincasa <vbenincasa(AT)gmail.com>

foreach($_REQUEST as $fields => $value) if(eregi("TO:", $value) || eregi("CC:", $value) || eregi("CCO:", $value) || eregi("Content-Type", $value)) exit("ERROR: Code injection attempt denied! Please don't use the following sequences in your message: 'TO:', 'CC:', 'CCO:' or 'Content-Type'.");

if(check_email_address($emails) == FALSE) {
   		echo 'PLEASE CHECK THAT YOU HAVE ENTERED VALID EMAIL ADRESSES.'; exit;
	} 
	elseif (!mail($your_email,$email_subject1,$email_content1)) {
		echo 'ERROR!';
	} 
else {
	echo 'Message Sent!';
	mail($your_email,$email_subject1,$email_content1,$headers);
	mail($referred,$email_subject2,$email_content2,$headers);
	mail($userEmail,$email_subject3,$email_content3,$headers);
}

?>

As you will probably see the script is supposed to capture all the information and then send three emails. One to me with the referrals. One to each of the referred people. And one to the referrer.

Please let me know if you need more info.

are you provide validation for your textfields (form validation).
"Please fill in the required fields" do you write this message in validation. check it once

Hi,

Not really sure what you mean but yes, I do have a validation check in the script.

The following snippet checks whether the field is required and then outputs an error:

foreach($values as $value){
if(in_array($value,$required)){
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
$email_content .= $value.': '.$_POST[$value]."\n";
}
}

The values are retrieved from the following snippet:

$values = array('userName','userEmail','name1','email1','name2','email2','name3','email3','name4','email4','name5','email5');
$required = array('userName','userEmail','name1','email1','name2','email2');

I have checked the html form and everything seems to be correct.

Err... I see now that I failed to mention that this happens even if the required fields are filled in.

The problem is that the error displays EVEN IF the required fields are filled in.

Hope that clarifies things a bit. :)

Err... I see now that I failed to mention that this happens even if the required fields are filled in.

The problem is that the error displays EVEN IF the required fields are filled in.

Hope that clarifies things a bit. :)

In email_contents,you forgot to put '$' before emails name

<li>'.$name1.' '.$email1.'</li>
<li>'.$name2.' '.$email2.'</li>
<li>'.$name3.' '.$email3.'</li>
<li>'.$name4.' '.$email4.'</li>
<li>'.$name5.' '.$email5.'</li>

put '$' before emailsname. and try it. and where is
$email_content1, email_subject1 check it .

put '$' before emailsname. and try it. and where is
$email_content1, email_subject1 check it .

Thank you rajabhaskar525.

I have fixed what you said but I still receive the message "please fill in required fields" even though they are filled in.... very strange.. :?:

Thank you rajabhaskar525.

I have fixed what you said but I still receive the message "please fill in required fields" even though they are filled in.... very strange.. :?:

but your code works fine in my system. did you fix '$' in all Email_contents. can you post your code once again.

Hello rajabhaskar,

I really appreciate you going through all this trouble to help me :)

here is the code:

<?php 

if(!$_POST) exit;
 
$values = array('userName','userEmail','name1','email1','name2','email2','name3','email3','name4','email4','name5','email5');
$required = array('userName','userEmail','name1','email1','name2','email2');

$userName = $_POST['userName'];
$userEmail = $_POST['userEmail'];
$name1 = $_POST['name1'];
$email1 = $_POST['email1'];
$name2 = $_POST['name2'];
$email2 = $_POST['email2'];
$name3 = $_POST['name3'];
$email3 = $_POST['email3'];
$name4 = $_POST['name4'];
$email4 = $_POST['email4'];
$name5 = $_POST['name5'];
$email5 = $_POST['email5'];
$emails = array($email1,$email2,$email3,$email4,$email5,$userEmail);

$your_email = "wikus@mymoot.co.za";
$email_subject1 = $userName." has referred his friends!";
$email_content1 = '
<html>
<head>
  <title>'.$userName.' has referred his friends!</title>
</head>
<body>
  <p>'.$userName.' has referred the following of his friends:</p>
	<ul>
		<li>'.$name1.' '.$email1.'</li>
		<li>'.$name2.' '.$email2.'</li>
		<li>'.$name3.' '.$email3.'</li>
		<li>'.$name4.' '.$email4.'</li>
		<li>'.$name5.' '.$email5.'</li>
	</ul>	
	<p>Referred by:<br />'.$userName.'</p>
	<p>Referrer Email:<br />'.$userEmail.'</p>		
</body>
</html>
';

$referred  = $name1 . ', ';
$referred  .= $name2 . ', ';
$referred  .= $name3 . ', ';
$referred  .= $name4 . ', ';
$referred  .= $name5;
$email_subject2 = $userName." wants you to win a Nintendo Wii!";
$email_content2 = '
<html>
<head>
  <title>'.$userName.' wants you to win a Nintendo Wii!!</title>
</head>
<body>
<p>Your friend '.$userName.' thought you might be interested in winnning a Nintendo Wii!</p>
<p>
To stand a chance to win this prize simply go to <a href="http://www.mymoot.co.za">www.mymoot.co.za</a> and click on the Nintendo Wii competition link.</p>
<p>
If you are not interested in winnning this prize you can safely ignore this message
and no further emails will be sent to you.</p>
<p>
Festive Greetings!<br /><br />
<a href="http://www.mymoot.co.za">MYMOOT.co.za</a>
</p>
</body>
</html>
';

$email_subject3 = $userName.", thank you for referring your friends!";
$email_content3 = '
<html>
<head>
  <title>Your Friends have been Referred!</title>
</head>
<body>
  <p>'.$userName.', by referring your friends you have now been entered into the competition.</p>
  <p>These are the friends that you have referred:</p>
	<ul>
		<li>'.$name1.' '.$email1.'</li>
		<li>'.$name2.' '.$email2.'</li>
		<li>'.$name3.' '.$email3.'</li>
		<li>'.$name4.' '.$email4.'</li>
		<li>'.$name5.' '.$email5.'</li>
	</ul>	
	<p>Remember, the more friends you refer the better your chances of winning!</p>	
	<p><b>Disclaimer:</b></p>
	<p>MYMOOT never sends unsolicited emails. Your friends will only receive a notification that they have been referred and no further emails will be sent should they not wish to enter the competition.</p>
	<p>Good Luck!</p>
	<p>The MYMOOT Team</p>
</body>
</html>
';
//Headers
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' ."MYMOOT.co.za". "\r\n";


function check_email_address($email) {
		foreach($email as $i=>$emails) {
			// Check of die email veld in gevul is en validate dan die email
			if (!empty($emails)) {	
				if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $emails)) {
					return false;
				}
				$email_array = explode("@", $emails);
				$local_array = explode(".", $email_array[0]);
				for ($i = 0; $i < sizeof($local_array); $i++) {
					if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
						return false;
					}
				}
				if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
					$domain_array = explode(".", $email_array[1]);
					if (sizeof($domain_array) < 2) {
						return false;
					}
					for ($i = 0; $i < sizeof($domain_array); $i++) {
						if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
							return false;
						}
					}
				}
			}
			// Gaan aan as dit nie in gevul is nie
			else {
				continue;
			}
	}
	return true;
}

foreach($values as $value){
  if(in_array($value,$required)){
    if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
    $email_content .= $value.': '.$_POST[$value]."\n";
  }
}

# Anti-header-injection - Use before mail()
# By Victor Benincasa <vbenincasa(AT)gmail.com>

foreach($_REQUEST as $fields => $value) if(eregi("TO:", $value) || eregi("CC:", $value) || eregi("CCO:", $value) || eregi("Content-Type", $value)) exit("ERROR: Code injection attempt denied! Please don't use the following sequences in your message: 'TO:', 'CC:', 'CCO:' or 'Content-Type'.");

if(check_email_address($emails) == FALSE) {
   		echo 'PLEASE CHECK THAT YOU HAVE ENTERED VALID EMAIL ADRESSES.'; exit;
	} 
	elseif (!mail($your_email,$email_subject1,$email_content1)) {
		echo 'ERROR!';
	} 
else {
	echo 'Message Sent!';
	mail($your_email,$email_subject1,$email_content1,$headers);
	mail($referred,$email_subject2,$email_content2,$headers);
	mail($userEmail,$email_subject3,$email_content3,$headers);
}

?>
<?php
 //if(!$_POST) exit;
 if($_POST['submit'])
 {
 $values = array('userName','userEmail','name1','email1','name2','email2','name3','email3','name4','email4','name5','email5');
 $required = array('userName','userEmail','name1','email1','name2','email2');
  
 $userName = $_POST['userName'];
 $userEmail = $_POST['userEmail'];
  $name1 = $_POST['name1'];
 $email1 = $_POST['email1'];
  $name2 = $_POST['name2'];
  $email2 = $_POST['email2'];
 $name3 = $_POST['name3'];
 $email3 = $_POST['email3'];
   $name4 = $_POST['name4'];
  $email4 = $_POST['email4'];
  $name5 = $_POST['name5'];
  $email5 = $_POST['email5'];
$emails = array($email1,$email2,$email3,$email4,$email5,$userEmail);
 $your_email = "wikus@mymoot.co.za";
  $email_subject1 = $userName." has referred his friends!";
$email_content1 = '
 <html>
 <head>
 <title>'.$userName.' has referred his friends!</title>
 </head>
 <body>
 <p>'.$userName.' has referred the following of his friends:</p>
 <ul>
 <li>'.$name1.' '.$email1.'</li>
  <li>'.$name2.' '.$email2.'</li>
<li>'.$name3.' '.$email3.'</li>
 <li>'.$name4.' '.$email4.'</li>
 <li>'.$name5.' '.$email5.'</li>
 </ul>
  <p>Referred by:<br />'.$userName.'</p>
  <p>Referrer Email:<br />'.$userEmail.'</p>
  </body>
 </html>
  ';
   
 $referred = $name1 . ', ';
  $referred .= $name2 . ', ';
  $referred .= $name3 . ', ';
  $referred .= $name4 . ', ';
  $referred .= $name5;
 $email_subject2 = $userName." wants you to win a Nintendo Wii!";
  $email_content2 = '
  <html>
 <head>
 <title>'.$userName.' wants you to win a Nintendo Wii!!</title>
 </head>
 <body>
 <p>Your friend '.$userName.' thought you might be interested in winnning a Nintendo Wii!</p>
 <p>
 To stand a chance to win this prize simply go to <a href="http://www.mymoot.co.za">www.mymoot.co.za</a> and click on the Nintendo Wii competition link.</p>
 <p>
 If you are not interested in winnning this prize you can safely ignore this message
 and no further emails will be sent to you.</p>
 <p>
 Festive Greetings!<br /><br />
 <a href="http://www.mymoot.co.za">MYMOOT.co.za</a>
 </p>
  </body>
  </html>
 ';
  $email_subject3 = $userName.", thank you for referring your friends!";
  $email_content3 = '
  <html>
 <head>
 <title>Your Friends have been Referred!</title>
 </head>
  <body>
 <p>'.$userName.', by referring your friends you have now been entered into the competition.</p>
 <p>These are the friends that you have referred:</p>
 <ul>
  <li>'.$name1.' '.$email1.'</li>
 <li>'.$name3.' '.$email3.'</li>
  <li>'.$name4.' '.$email4.'</li>
  <li>'.$name5.' '.$email5.'</li>
</ul>
 <p>Remember, the more friends you refer the better your chances of winning!</p>
 <p><b>Disclaimer:</b></p>
  <p>MYMOOT never sends unsolicited emails. Your friends will only receive a notification that they have been referred and no further emails will be sent should they not wish to enter the competition.</p>
 <p>Good Luck!</p>
  <p>The MYMOOT Team</p>
  </body>
  </html>
  ';
 //Headers
  $headers = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= 'From: ' ."MYMOOT.co.za". "\r\n";
 function check_email_address($email) {
   foreach($email as $i=>$emails) {
  // Check of die email veld in gevul is en validate dan die email
 if (!empty($emails)) {
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $emails)) {
 return false;
  }
 $email_array = explode("@", $emails);
  $local_array = explode(".", $email_array[0]);
 for ($i = 0; $i < sizeof($local_array); $i++) {
 if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
 return false;
  }
  }
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
  $domain_array = explode(".", $email_array[1]);
 if (sizeof($domain_array) < 2) {
  return false;
  }
  for ($i = 0; $i < sizeof($domain_array); $i++) {
 if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
  return false;
  }
 }
 }
  }
  // Gaan aan as dit nie in gevul is nie
  else {
 continue;
  }
 }
 return true;
  }
  
foreach($values as $value){
 if(in_array($value,$required)){
 if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
 $email_content .= $value.': '.$_POST[$value]."\n";
 }
 }
 # By Victor Benincasa <vbenincasa(AT)gmail.com>
 foreach($_REQUEST as $fields => $value) if(eregi("TO:", $value) || eregi("CC:", $value) || eregi("CCO:", $value) || eregi("Content-Type", $value)) exit("ERROR: Code injection attempt denied! Please don't use the following sequences in your message: 'TO:', 'CC:', 'CCO:' or 'Content-Type'.");
 //echo $email_content3;exit;
if(check_email_address($emails) == FALSE) {
  echo 'PLEASE CHECK THAT YOU HAVE ENTERED VALID EMAIL ADRESSES.'; exit;
  }
 elseif (!mail($your_email,$email_subject1,$email_content1)) {
echo 'ERROR!';
 }
  else {
  echo 'Message Sent!';
 mail($your_email,$email_subject1,$email_content1,$headers);
 mail($referred,$email_subject2,$email_content2,$headers);
 mail($userEmail,$email_subject3,$email_content3,$headers);
  }
  }
 ?>

it works fine. try once.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.