Stop blank emails being sent

Hi there.  is there a way using this form that if someone uses it, it won't send out the blank emails if people don't add more than one email address to send it to?  It allows the user to add up to 4 email addresses, but if they leave the other three blank, the code sends an email to the client's catchall email address for each email address that is left blank.
<?php
if(isset($_POST["_task"]) && $_POST["_task"]=="SEND"){
$sender = $_POST['sender'];
$comment = $_POST['comment'];
$subject = "Photos from Test";	
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'From: Test <test@test.com>' . "\r\n";

for($i = 1; $i <=4; $i++) {
$r_email = "email_".$i;
$email = $_POST[$r_email];

if($email != "") {


$message='<p style="width:450px; font-family:arial; font-size:14px;color:#717783;"></p><p style="width:450px; font-family:arial; font-size:14px;color:#717783;">These photos are being sent to you at the request of '.$sender.':</p><p style="width:450px; font-family:arial; font-size:14px;color:#717783;">'.$comment.'</p><table style="width:450px; font-family:arial; font-size:14px;color:#717783;" border="0" width="480" cellspacing="0" cellpadding="0"><tr>';$sql = mysql_query("select p.productid,pd.productname,pd.price,pd.description,pd.image from products p , products_description pd where pd.productid = p.productid and p.productid = $id");$row = mysql_fetch_row($sql); 
$productname=$row[1];
$thumb=$row[4];
$message .= '<tr><td valign="top"><br><br><div style="width:450px; font-family:arial; font-size:18px;color:#717783;">'.$productname.'</div><div style="width:450px; font-family:arial; font-size:14px;color:#717783;">'.$descriptions.'<br><br></div></td></tr></table>';}
$message .= '';
mail($email, $subject, $message, $headers);
}}
else{
	header("location:products.php");
	exit();
} 
?>
<?php include("include/header.php");?>
<body class="wishlist">
<table width="1000" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">
  <tr>
    <td width="43"></td>
    <td width="227"  height="62" background="images/sidebarbg.png" style="background-repeat:repeat">&nbsp;</td>
    <td width="728"><?php include("topaddress.php")?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td background="images/sidebarbg.png" style="background-repeat:repeat"><img src="images/logo.png" width="227" height="98" border="0" /></td>
    <td><div class="navigation">
        <?php include("topnavigation.php")?>
      </div></td>
  </tr>
  
  <td>&nbsp;</td>
  <td valign="top" background="images/sidebarbg.png" style="background-repeat:repeat"><div class="search">
      <?php include("searchsidebar.php")?>
    </div></td>
  <td>
   
    <div class="wishlist"><table width="613" border="0" cellspacing="0" cellpadding="0">
                           <tr><td width="613" height="20"></td></tr>
						   <tr>
							<td style="font-size:16px">Product Detail has been Sent Successfully.</td>
							</tr>
							<tr><td height="20"></td></tr>
							<tr>
							<td>
							</td>
							</tr>
                          </table></div>
    <div id="footer">
   
      </td>
  </tr>
  
</table>
<script language="javascript">
function validate() {
   
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var sender = document.getElementById('sender').value;
   var email1 = document.getElementById('email_1').value;
   var email2 = document.getElementById('email_2').value;
   var email3 = document.getElementById('email_3').value;
   var email4 = document.getElementById('email_4').value;
   
   if(sender == "" || sender == " ") {
      alert('Please Enter your Name');
      document.getElementById('sender').focus();
      return false;
   }
   else if(reg.test(email1) == false) {
      alert('Please Enter valid Email Address');
      document.getElementById('email_1').focus();
      return false;
   }
   else if(reg.test(email2) == false  && email2!='') {
      alert('Invalid Email Address');
      document.getElementById('email_2').focus();
      return false;
   }else if(reg.test(email3) == false && email3!='') {
      alert('Invalid Email Address');
      document.getElementById('email_3').focus();
      return false;
   }else if(reg.test(email4) == false && email4!='') {
      alert('Invalid Email Address');
      document.getElementById('email_4').focus();
      return false;
   }else return
}
</script>
</body>
</html>

Open in new window

kmurphychiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Usually a script can use trim() and empty() to test for blank fields.  Then it can avoid sending blank emails.
0
Ray PaseurCommented:
Now that I have read the code, it appears that what is missing is the <form> that sends the POST information to this script.  A good design might include the <form> and the action script together in the same script file.  I think we would need to see the complete picture to help you bring this up to professional standards.  This is in need of a complete refactoring.
0
Marco GasiFreelancerCommented:
Without complete code I can only show you what I would approximately do: see code attached and keep in mind my form is very poor. I only want show you how you can manage multiple email address using array notation in your form and using php. I also omitted javascript validation and I would suggest to always validate your form server-side using php: it's mutch more secure.

HTH

Cheers
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
if (isset($_POST['submit'])){
    $sender = $_POST['sender'];
    $comment = $_POST['comment'];
    $subject = "Photos from Test";	
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $headers .= 'From: Test <test@test.com>' . "\r\n";
    foreach ($_POST['email'] as $e){
        //put validation here
        $message='<p style="width:450px; font-family:arial; font-size:14px;color:#717783;"></p><p style="width:450px; font-family:arial; font-size:14px;color:#717783;">These photos are being sent to you at the request of '.$sender.':</p><p style="width:450px; font-family:arial; font-size:14px;color:#717783;">'.$comment.'</p><table style="width:450px; font-family:arial; font-size:14px;color:#717783;" border="0" width="480" cellspacing="0" cellpadding="0"><tr>';$sql = mysql_query("select p.productid,pd.productname,pd.price,pd.description,pd.image from products p , products_description pd where pd.productid = p.productid and p.productid = $id");$row = mysql_fetch_row($sql); 
        $productname=$row[1];
        $thumb=$row[4];
        $message .= '<tr><td valign="top"><br><br><div style="width:450px; font-family:arial; font-size:18px;color:#717783;">'.$productname.'</div><div style="width:450px; font-family:arial; font-size:14px;color:#717783;">'.$descriptions.'<br><br></div></td></tr></table>';
        $message .= '';
        mail($email, $subject, $message, $headers);        
    }
}

?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
    <table>
        <tr>
            <td>
                <input type="text" name="sender" value="" />
            </td>
        </tr>
                <tr>
            <td>
                <input type="text" name="comment" value="" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="email[]" value="" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="email[]" value="" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="email[]" value="" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="email[]" value="" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="submit" value="Send" />
            </td>
        </tr>
    </table>
</form>

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ray PaseurCommented:
I think marqusG's design pattern that uses an array for the email input controls is pretty good.  However I doubt that I would want to have a mysql query statement in the same line that generated an HTML message string.  I would change lines 12-20 as follows.
foreach ($_POST['email'] as $e)
{
    $email = trim($e);
    if (empty($email)) continue;
    /* CONSTRUCT MESSAGE HERE */
    mail($email, $subject, $message, $headers);        
}

Open in new window


In case you are interested in refactoring this, and I sincerely hope you will do that, you might want to add a little bit of email validation to the PHP script.  This code snippet shows how to do that.

Best of luck with it, ~Ray
<?php // RAY_email_validation.php
error_reporting(E_ALL);


// A FUNCTION TO TEST FOR A VALID EMAIL ADDRESS, RETURN TRUE OR FALSE
// SEE MAN PAGE: http://php.net/manual/en/intro.filter.php
function check_valid_email($email)
{
    // LIST OF BLOCKED DOMAINS
    $bogus = array
    ( '@unknown.com'
    , '@example.com'
    , '@gooseball.org'
    )
    ;

    // IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
    if (strnatcmp(phpversion(),'5.2') >= 0)
    {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) return FALSE;
    }

    // IF LOWER-LEVEL PHP, WE CAN CONSTRUCT A REGULAR EXPRESSION
    else
    {
        $regex
        = '/'                        // START REGEX DELIMITER
        . '^'                        // START STRING
        . '[A-Z0-9_-]'               // AN EMAIL - SOME CHARACTER(S)
        . '[A-Z0-9._-]*'             // AN EMAIL - SOME CHARACTER(S) PERMITS DOT
        . '@'                        // A SINGLE AT-SIGN
        . '([A-Z0-9][A-Z0-9-]*\.)+'  // A DOMAIN NAME PERMITS DOT, ENDS DOT
        . '[A-Z\.]'                  // A TOP-LEVEL DOMAIN PERMITS DOT
        . '{2,6}'                    // TLD LENGTH >= 2 AND =< 6
        . '$'                        // ENDOF STRING
        . '/'                        // ENDOF REGEX DELIMITER
        . 'i'                        // CASE INSENSITIVE
        ;
        // TEST THE STRING FORMAT
        if (!preg_match($regex, $email)) return FALSE;
    }

    // TEST TO SEE IF THE DOMAIN IS IN OUR BLOCKED LIST
    foreach ($bogus as $badguy)
    {
        if (stripos($email, $badguy)) return FALSE;
    }

    // FILTER_VAR OR PREG_MATCH DOES NOT TEST IF THE DOMAIN IS ROUTABLE
    $domain = explode('@', $email);

    // MAN PAGE: http://php.net/manual/en/function.checkdnsrr.php
    if ( checkdnsrr($domain[1], "MX") || checkdnsrr($domain[1], "A") ) return TRUE;

    // EMAIL IS NOT ROUTABLE
    return FALSE;
}



// DEMONSTRATE THE FUNCTION IN ACTION
$e = NULL;
if (!empty($_GET["e"]))
{
    $e = $_GET["e"];
    if (check_valid_email($e))
    {
        echo "<br/>VALID: $e \n";
    }
    else
    {
        echo "<br/>BOGUS: $e \n";
    }
}


// END OF PROCESSING - CREATE THE FORM USING HEREDOC NOTATION
$form = <<<ENDFORM
<form>
TEST A STRING FOR A VALID EMAIL ADDRESS:
<input name="e" value="$e" />
<input type="submit" />
</form>
ENDFORM;

echo $form;

Open in new window

0
kmurphychiAuthor Commented:
Thanks everyone for your help!  Below is the form code.  I can't believe I forgot to add that.  My PHP skills are not great and I have inherited this site, so feel free to let me know what I'm doing wrong!  I'm just not sure how to implement this, and am not sure if the above suggestions will work with the form code below.
<table width="475" border="0" cellspacing="0" cellpadding="0" class="box">
							<form name="form_id" method="post" action="email_product1.php" onSubmit="return validate();">
							 <tr>
                             <td class="cartheading " colspan="3">&nbsp;</td>
                          </tr>
                          <tr>
                             <td width="100" class="normal">Your Name: </td>
                             <td width="125" class="normal"><input type="text" size=26 name="sender" id="sender" value=""/></td>
							</tr>
							
							 <tr>
                             <td width="100" class="normal">Your Comment: </td>
                             <td width="125" class="normal"><textarea name="comment" id="comment" rows="5"></textarea></td>
							</tr>
											
                    <tr><td height="10"></td></tr>
 <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
							
                          <input type="hidden" name="_task" value="SEND" />
                           <tr>
                             <td class="cartheading " colspan="3">&nbsp;</td>
                          </tr>
                          <?php 
						  for($i = 1; $i <= 4; $i++) { ?>
						  <tr>
                             <!--<td width="143" class="normal">Recipent Detail #<?php echo $i ?></td>
                             <td width="250" class="normal">Name: <input type="text" name="name_<?php echo $i ?>" id="name_<?php echo $i ?>" value=""/></td>-->
							 <td width="100" class="normal">Recipient Email: </td><td><input type="text" size=26 name="email_<?php echo $i ?>" id="email_<?php echo $i ?>" value=""/>
							 </td>
                          </tr>
						  <?php } ?>
                          <tr>
                             <td class="cartheading1 " colspan="2"></td><td class="cartheading1" align="center"><input type="image" src="images/button_send_mail.gif"/></td>
                          </tr>
                           </form> 
                          </table>

Open in new window

0
xtermCommented:
Your for() loop checks $email, but it needs to check $r_email (which is basically $email_1->$email_4)

Change line 15 as below, and that should fix it.
if($r_email != "") {

Open in new window

0
Marco GasiFreelancerCommented:
Hi.
I attach two snippets, one for the form and one for the code which sends emails:
//FORM

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="MSSmartTagsPreventParsing" content="TRUE" />
<script language="javascript">
function validate() {
   
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var sender = document.getElementById('sender').value;
   var email1 = document.getElementById('email_1').value;
   var email2 = document.getElementById('email_2').value;
   var email3 = document.getElementById('email_3').value;
   var email4 = document.getElementById('email_4').value;
   
   if(sender == "" || sender == " ") {
      alert('Please Enter your Name');
      document.getElementById('sender').focus();
      return false;
   }
   else if(reg.test(email1) == false) {
      alert('Please Enter valid Email Address');
      document.getElementById('email_1').focus();
      return false;
   }else if(reg.test(email2) == false && email2!='') {
      alert('Invalid Email Address');
      document.getElementById('email_2').focus();
      return false;
   }else if(reg.test(email3) == false && email3!='') {
      alert('Invalid Email Address');
      document.getElementById('email_3').focus();
      return false;
   }else if(reg.test(email4) == false && email4!='') {
      alert('Invalid Email Address');
      document.getElementById('email_4').focus();
      return false;
   }else return
}
</script>
</head>
       
    <body>
<form name="form_id" method="post" action="email_product1.php" onSubmit="return validate();">
    <table width="475" border="0" cellspacing="0" cellpadding="0" class="box">
        <tr>
            <td class="cartheading " colspan="3">&nbsp;</td>
        </tr>
        <tr>
            <td width="100" class="normal">Your Name: </td>
            <td width="125" class="normal"><input type="text" size=26 name="sender" id="sender" value=""/></td>
        </tr>

        <tr>
            <td width="100" class="normal">Your Comment: </td>
            <td width="125" class="normal"><textarea name="comment" id="comment" rows="5"></textarea></td>
        </tr>

        <tr><td height="10">
            <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
            <input type="hidden" name="_task" value="SEND" />
        </td></tr>
        <tr>
            <td class="cartheading " colspan="3">&nbsp;</td>
        </tr>
        <?php for ($i = 1; $i <= 4; $i++) { ?>
            <tr>
    <!--<td width="143" class="normal">Recipent Detail #<?php echo $i ?></td>
    <td width="250" class="normal">Name: <input type="text" name="name_<?php echo $i ?>" id="name_<?php echo $i ?>" value=""/></td>-->
                <td width="100" class="normal">Recipient Email: </td><td><input type="text" size=26 name="email_<?php echo $i ?>" id="email_<?php echo $i ?>" value=""/>
                </td>
            </tr>
        <?php } ?>
        <tr>
            <td class="cartheading1 " colspan="2"></td><td class="cartheading1" align="center"><input type="image" src="images/button_send_mail.gif"/></td>
        </tr>
    </table>
</form>         
        
    </body>
</html>

Open in new window


<?php
include("include/header.php");
function check_valid_email($email)
{
    // LIST OF BLOCKED DOMAINS
    $bogus = array
    ( '@unknown.com'
    , '@example.com'
    , '@gooseball.org'
    )
    ;

    // IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
    if (strnatcmp(phpversion(),'5.2') >= 0)
    {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) return FALSE;
    }

    // IF LOWER-LEVEL PHP, WE CAN CONSTRUCT A REGULAR EXPRESSION
    else
    {
        $regex
        = '/'                        // START REGEX DELIMITER
        . '^'                        // START STRING
        . '[A-Z0-9_-]'               // AN EMAIL - SOME CHARACTER(S)
        . '[A-Z0-9._-]*'             // AN EMAIL - SOME CHARACTER(S) PERMITS DOT
        . '@'                        // A SINGLE AT-SIGN
        . '([A-Z0-9][A-Z0-9-]*\.)+'  // A DOMAIN NAME PERMITS DOT, ENDS DOT
        . '[A-Z\.]'                  // A TOP-LEVEL DOMAIN PERMITS DOT
        . '{2,6}'                    // TLD LENGTH >= 2 AND =< 6
        . '$'                        // ENDOF STRING
        . '/'                        // ENDOF REGEX DELIMITER
        . 'i'                        // CASE INSENSITIVE
        ;
        // TEST THE STRING FORMAT
        if (!preg_match($regex, $email)) return FALSE;
    }

    // TEST TO SEE IF THE DOMAIN IS IN OUR BLOCKED LIST
    foreach ($bogus as $badguy)
    {
        if (stripos($email, $badguy)) return FALSE;
    }

    // FILTER_VAR OR PREG_MATCH DOES NOT TEST IF THE DOMAIN IS ROUTABLE
    $domain = explode('@', $email);

    // MAN PAGE: http://php.net/manual/en/function.checkdnsrr.php
    if ( checkdnsrr($domain[1], "MX") || checkdnsrr($domain[1], "A") ) return TRUE;

    // EMAIL IS NOT ROUTABLE
    return FALSE;
}
if (isset($_POST["_task"]) && $_POST["_task"] == "SEND") {
    $sender = $_POST['sender'];
    $comment = $_POST['comment'];
    $subject = "Photos from Test";
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $headers .= 'From: Test <test@test.com>' . "\r\n";

    for ($i = 1; $i <= 4; $i++) {
        $r_email[] = $_POST['email_' . $i];
    }
    foreach ($r_email as $email){
        if ($email != "" && check_valid_email($email)) {
            $message = '<p style="width:450px; font-family:arial; font-size:14px;color:#717783;"></p><p style="width:450px; font-family:arial; font-size:14px;color:#717783;">These photos are being sent to you at the request of ' . $sender . ':</p><p style="width:450px; font-family:arial; font-size:14px;color:#717783;">' . $comment . '</p><table style="width:450px; font-family:arial; font-size:14px;color:#717783;" border="0" width="480" cellspacing="0" cellpadding="0"><tr>';
            $sql = mysql_query("select p.productid,pd.productname,pd.price,pd.description,pd.image from products p , products_description pd where pd.productid = p.productid and p.productid = $id");
            $row = mysql_fetch_row($sql);
            $productname = $row[1];
            $thumb = $row[4];
            $message .= '<tr><td valign="top"><br><br><div style="width:450px; font-family:arial; font-size:18px;color:#717783;">' . $productname . '</div><div style="width:450px; font-family:arial; font-size:14px;color:#717783;">' . $descriptions . '<br><br></div></td></tr></table>';
        }
        $message .= '';
        mail($email, $subject, $message, $headers);
    }
} else {
    header("location:products.php");
    exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <meta name="MSSmartTagsPreventParsing" content="TRUE" />

    </head>
    <body class="wishlist">
        <table width="1000" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">
            <tr>
                <td width="43"></td>
                <td width="227"  height="62" background="images/sidebarbg.png" style="background-repeat:repeat">&nbsp;</td>
                <td width="728"><?php include("topaddress.php") ?></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td background="images/sidebarbg.png" style="background-repeat:repeat"><img src="images/logo.png" width="227" height="98" border="0" /></td>
                <td><div class="navigation">
<?php include("topnavigation.php") ?>
                    </div></td>
            </tr>

            <td>&nbsp;</td>
            <td valign="top" background="images/sidebarbg.png" style="background-repeat:repeat"><div class="search">
<?php include("searchsidebar.php") ?>
                </div></td>
            <td>

                <div class="wishlist"><table width="613" border="0" cellspacing="0" cellpadding="0">
                        <tr><td width="613" height="20"></td></tr>
                        <tr>
                            <td style="font-size:16px">Product Detail has been Sent Successfully.</td>
                        </tr>
                        <tr><td height="20"></td></tr>
                        <tr>
                            <td>
                            </td>
                        </tr>
                    </table></div>
                <div id="footer">

            </td>
        </tr>

    </table>



</body>
</html>

Open in new window



Within your form I fixed some minor formal error.
I kept your javascript validation, but I integrated it with Ray's function check_valid_email (comment ID:37248571).
Instead to use the array in the form, I created the array in the receiving code: this way you can use both javascript and php validation methods.

If the code you posted here was complete and working, you should be ablke to replace it with these two snippets and get it working immediately, but I have some doubt.

Ifyou have some problem, you can post here the whole code ( the whole page with the form and the whole page with the receiving code and any other code is involved in this process) and we can try to get it working. In your form I don't see any input type submit: there is the image 'button_send_mail.gif' but it should have name='submit' to work (or so tell me my knowledge, which is limited)

Let me know if you have problems.

Cheers
0
kmurphychiAuthor Commented:
Thanks for all the help!  I am still having problems with it sending a Mail failure for all blank email fields.  I have implemented from MarcusG (I think I did it correctly, but probably not) but am still having the error.  The form does work but still sending the blank emails.
/code for first page where form lives.

<?php include("include/meta_seo.php");?>
<?php $pageid="2";
   $rsT=$db->getALLC("pages","id='2'");
   $rowT=$db->row($rsT);?>
<?php $title=$rowT["title"]; ?>
<?php $keywords=$rowT["keywords"]; ?>
<?php $description=$rowT["description"]; ?>
<?php include("include/header.php");?>
<body class="wishlist">
<table width="1000" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">
  <tr>
    <td width="43"></td>
    <td width="227"  height="62" background="images/sidebarbg.png" style="background-repeat:repeat">&nbsp;</td>
    <td width="728"><?php include("topaddress.php")?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td background="images/sidebarbg.png" style="background-repeat:repeat"><img src="images/logo.png" width="227" height="98" /></td>
    <td><div class="navigation">
        <?php include("topnavigation.php")?>
      </div></td>
  </tr>
  
  <td>&nbsp;</td>
  <td valign="top" background="images/sidebarbg.png" style="background-repeat:repeat"><div class="search">
      <?php include("searchsidebar.php")?>
    </div></td>
  <td>
   
    <div class="wishlist"><table cellpadding="0" cellspacing="0" width="675" border="0">
                    <tr><td height="10"></td></tr>
							
					<tr>
                    	<td>
                        	<!-- main Table -->
                        	
							<form name="form_id" method="post" action="email_product1.php" onSubmit="return validate();">
    <table width="475" border="0" cellspacing="0" cellpadding="0" class="box">
        <tr>
            <td class="cartheading " colspan="3">&nbsp;</td>
        </tr>
        <tr>
            <td width="100" class="normal">Your Name: </td>
            <td width="125" class="normal"><input type="text" size=26 name="sender" id="sender" value=""/></td>
        </tr>

        <tr>
            <td width="100" class="normal">Your Comment: </td>
            <td width="125" class="normal"><textarea name="comment" id="comment" rows="5"></textarea></td>
        </tr>

        <tr><td height="10">
            <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
            <input type="hidden" name="_task" value="SEND" />
        </td></tr>
        <tr>
            <td class="cartheading " colspan="3">&nbsp;</td>
        </tr>
        <?php for ($i = 1; $i <= 4; $i++) { ?>
            <tr>
    <!--<td width="143" class="normal">Recipent Detail #<?php echo $i ?></td>
    <td width="250" class="normal">Name: <input type="text" name="name_<?php echo $i ?>" id="name_<?php echo $i ?>" value=""/></td>-->
                <td width="100" class="normal">Recipient Email: </td><td><input type="text" size=26 name="email_<?php echo $i ?>" id="email_<?php echo $i ?>" value=""/>
                </td>
            </tr>
        <?php } ?>
        <tr>
            <td class="cartheading1 " colspan="2"></td><td class="cartheading1" align="center"><input type="image" src="images/button_send_mail.gif"/></td>
        </tr>
    </table>
</form>         
                        <!--End  main Table -->
                        </td>
                    </tr>
					<tr><td height="100"></td></tr>
					</table></div>
    <div id="footer">
      <div class="footernav"> <a href="index.php" class="black" title="">Home</a> &nbsp; | &nbsp; <a href="products.php" class="black" title="Products">Products</a> &nbsp; | &nbsp; <a href="contact_us.php" class="black" title="Contact Us">Contact</a> &nbsp; | &nbsp; <a href="terms.php" class="black" title="Terms">Terms</a> <br />
        Copyright &copy;<br />
        <br />
      </div>
      
    </div></td>
  </tr>
  
</table>
<script language="javascript">
function validate() {
   
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var sender = document.getElementById('sender').value;
   var email1 = document.getElementById('email_1').value;
   var email2 = document.getElementById('email_2').value;
   var email3 = document.getElementById('email_3').value;
   var email4 = document.getElementById('email_4').value;
   
   if(sender == "" || sender == " ") {
      alert('Please Enter your Name');
      document.getElementById('sender').focus();
      return false;
   }
   else if(reg.test(email1) == false) {
      alert('Please Enter valid Email Address');
      document.getElementById('email_1').focus();
      return false;
   }else if(reg.test(email2) == false && email2!='') {
      alert('Invalid Email Address');
      document.getElementById('email_2').focus();
      return false;
   }else if(reg.test(email3) == false && email3!='') {
      alert('Invalid Email Address');
      document.getElementById('email_3').focus();
      return false;
   }else if(reg.test(email4) == false && email4!='') {
      alert('Invalid Email Address');
      document.getElementById('email_4').focus();
      return false;
   }else return
}
</script>
</body>
</html>

Open in new window

/code for success page

<?php include("include/meta_seo.php");
$id = $_POST['id'];
$pageid="2";
   $rsT=$db->getALLC("pages","id='2'");
   $rowT=$db->row($rsT);?>
<?php $title=$rowT["title"]; ?>
<?php $keywords=$rowT["keywords"]; ?>
<?php $description=$rowT["description"]; ?>
<?php
function check_valid_email($email)
{
    // LIST OF BLOCKED DOMAINS
    $bogus = array
    ( '@unknown.com'
    , '@example.com'
    , '@gooseball.org'
    )
    ;

    // IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
    if (strnatcmp(phpversion(),'5.2') >= 0)
    {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) return FALSE;
    }

    // IF LOWER-LEVEL PHP, WE CAN CONSTRUCT A REGULAR EXPRESSION
    else
    {
        $regex
        = '/'                        // START REGEX DELIMITER
        . '^'                        // START STRING
        . '[A-Z0-9_-]'               // AN EMAIL - SOME CHARACTER(S)
        . '[A-Z0-9._-]*'             // AN EMAIL - SOME CHARACTER(S) PERMITS DOT
        . '@'                        // A SINGLE AT-SIGN
        . '([A-Z0-9][A-Z0-9-]*\.)+'  // A DOMAIN NAME PERMITS DOT, ENDS DOT
        . '[A-Z\.]'                  // A TOP-LEVEL DOMAIN PERMITS DOT
        . '{2,6}'                    // TLD LENGTH >= 2 AND =< 6
        . '$'                        // ENDOF STRING
        . '/'                        // ENDOF REGEX DELIMITER
        . 'i'                        // CASE INSENSITIVE
        ;
        // TEST THE STRING FORMAT
        if (!preg_match($regex, $email)) return FALSE;
    }

    // TEST TO SEE IF THE DOMAIN IS IN OUR BLOCKED LIST
    foreach ($bogus as $badguy)
    {
        if (stripos($email, $badguy)) return FALSE;
    }

    // FILTER_VAR OR PREG_MATCH DOES NOT TEST IF THE DOMAIN IS ROUTABLE
    $domain = explode('@', $email);

    // MAN PAGE: http://php.net/manual/en/function.checkdnsrr.php
    if ( checkdnsrr($domain[1], "MX") || checkdnsrr($domain[1], "A") ) return TRUE;

    // EMAIL IS NOT ROUTABLE
    return FALSE;
}
if (isset($_POST["_task"]) && $_POST["_task"] == "SEND") {
    $sender = $_POST['sender'];
    $comment = $_POST['comment'];
    $subject = "Photos from Tablescapes Party Rentals";
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $headers .= 'From: Email <rentals@email.com>' . "\r\n";

    for ($i = 1; $i <= 4; $i++) {
        $r_email[] = $_POST['email_' . $i];
    }
    foreach ($r_email as $email){
        if ($email != "" && check_valid_email($email)) {
       $message='<p style="width:450px; font-family:arial; font-size:14px;color:#717783;"></p><p style="width:450px; font-family:arial; font-size:14px;color:#717783;">These photos are being sent to you at the request of '.$sender.':</p><p style="width:450px; font-family:arial; font-size:14px;color:#717783;">'.$comment.'</p><table style="width:450px; font-family:arial; font-size:14px;color:#717783;" border="0" width="480" cellspacing="0" cellpadding="0"><tr>';$sql = mysql_query("select p.productid,pd.productname,pd.price,pd.description,pd.image from products p , products_description pd where pd.productid = p.productid and p.productid = $id");$row = mysql_fetch_row($sql); 
$productname=$row[1];
$thumb=$row[4];
$message .= '<tr><td valign="top"><img src="http://www.websiteaddress.com/images/product-images/'.$thumb.'"  border="0" /><br><br><div style="width:450px; font-family:arial; font-size:18px;color:#717783;">'.$productname.'</div><div style="width:450px; font-family:arial; font-size:14px;color:#717783;">'.$descriptions.'<br><br></div></td></tr></table>';}
$message .= '<p style="width:450px; font-family:arial; font-size:18px;color:#717783;">Thank you text</p></p>';
        mail($email, $subject, $message, $headers);
    }
} else {
    header("location:products.php");
    exit();
}
?>
<?php include("include/header.php");?>
<body class="wishlist">
<table width="1000" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">
  <tr>
    <td width="43"></td>
    <td width="227"  height="62" background="images/sidebarbg.png" style="background-repeat:repeat">&nbsp;</td>
    <td width="728"><?php include("topaddress.php")?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td background="images/sidebarbg.png" style="background-repeat:repeat"></td>
    <td><div class="navigation">
        <?php include("topnavigation.php")?>
      </div></td>
  </tr>
  
  <td>&nbsp;</td>
  <td valign="top" background="images/sidebarbg.png" style="background-repeat:repeat"><div class="search">
      <?php include("searchsidebar.php")?>
    </div></td>
  <td>
   
    <div class="wishlist"><table width="613" border="0" cellspacing="0" cellpadding="0">
                           <tr><td width="613" height="20"></td></tr>
						   <tr>
							<td style="font-size:16px">Product Detail has been Sent Successfully.</td>
							</tr>
							<tr><td height="20"></td></tr>
							<tr>
							<td>
							</td>
							</tr>
                          </table></div>
    <div id="footer">
      <div class="footernav">  &nbsp; | &nbsp; <a href="products.php" class="black" title="Products">Products</a> &nbsp; | &nbsp; <a href="contact_us.php" class="black" title="Contact Us">Contact</a> &nbsp; | &nbsp; <a href="terms.php" class="black" title="Terms">Terms</a> <br />
         <br />
        <br />
      </div>

    </div></td>
  </tr>
  
</table>
<script language="javascript">
function validate() {
   
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var sender = document.getElementById('sender').value;
   var email1 = document.getElementById('email_1').value;
   var email2 = document.getElementById('email_2').value;
   var email3 = document.getElementById('email_3').value;
   var email4 = document.getElementById('email_4').value;
   
   if(sender == "" || sender == " ") {
      alert('Please Enter your Name');
      document.getElementById('sender').focus();
      return false;
   }
   else if(reg.test(email1) == false) {
      alert('Please Enter valid Email Address');
      document.getElementById('email_1').focus();
      return false;
   }
   else if(reg.test(email2) == false  && email2!='') {
      alert('Invalid Email Address');
      document.getElementById('email_2').focus();
      return false;
   }else if(reg.test(email3) == false && email3!='') {
      alert('Invalid Email Address');
      document.getElementById('email_3').focus();
      return false;
   }else if(reg.test(email4) == false && email4!='') {
      alert('Invalid Email Address');
      document.getElementById('email_4').focus();
      return false;
   }else return
}
</script>
</body>
</html>

Open in new window

0
xtermCommented:
Try mine
0
kmurphychiAuthor Commented:
xterm I tried yours and replaced the line of code you supplied on my above code on what is now line 76.  Is that correct?  If so it's still sending the blank emails.
0
xtermCommented:
I don't really  have the time to validate the other experts' code.  Please try it on your original unpatched code.
0
Marco GasiFreelancerCommented:
Probably you have some error in mysql_query. Try to use tha page attached as 'success' page:  

<?php
//code for success page
include("include/meta_seo.php");
$id = $_POST['id'];
$pageid = "2";
$rsT = $db->getALLC("pages", "id='2'");
$rowT = $db->row($rsT);
?>
<?php $title = $rowT["title"]; ?>
<?php $keywords = $rowT["keywords"]; ?>
<?php $description = $rowT["description"]; ?>
<?php

function check_valid_email($email) {
    // LIST OF BLOCKED DOMAINS
    $bogus = array
        ('@unknown.com'
        , '@example.com'
        , '@gooseball.org'
            )
    ;

    // IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
    if (strnatcmp(phpversion(), '5.2') >= 0) {
        if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
            return FALSE;
    }

    // IF LOWER-LEVEL PHP, WE CAN CONSTRUCT A REGULAR EXPRESSION
    else {
        $regex
                = '/'                        // START REGEX DELIMITER
                . '^'                        // START STRING
                . '[A-Z0-9_-]'               // AN EMAIL - SOME CHARACTER(S)
                . '[A-Z0-9._-]*'             // AN EMAIL - SOME CHARACTER(S) PERMITS DOT
                . '@'                        // A SINGLE AT-SIGN
                . '([A-Z0-9][A-Z0-9-]*\.)+'  // A DOMAIN NAME PERMITS DOT, ENDS DOT
                . '[A-Z\.]'                  // A TOP-LEVEL DOMAIN PERMITS DOT
                . '{2,6}'                    // TLD LENGTH >= 2 AND =< 6
                . '$'                        // ENDOF STRING
                . '/'                        // ENDOF REGEX DELIMITER
                . 'i'                        // CASE INSENSITIVE
        ;
        // TEST THE STRING FORMAT
        if (!preg_match($regex, $email))
            return FALSE;
    }

    // TEST TO SEE IF THE DOMAIN IS IN OUR BLOCKED LIST
    foreach ($bogus as $badguy) {
        if (stripos($email, $badguy))
            return FALSE;
    }

    // FILTER_VAR OR PREG_MATCH DOES NOT TEST IF THE DOMAIN IS ROUTABLE
    $domain = explode('@', $email);

    // MAN PAGE: http://php.net/manual/en/function.checkdnsrr.php
    if (checkdnsrr($domain[1], "MX") || checkdnsrr($domain[1], "A"))
        return TRUE;

    // EMAIL IS NOT ROUTABLE
    return FALSE;
}

if (isset($_POST["_task"]) && $_POST["_task"] == "SEND") {
    $sender = $_POST['sender'];
    $comment = $_POST['comment'];
    $subject = "Photos from Tablescapes Party Rentals";
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $headers .= 'From: Email <rentals@email.com>' . "\r\n";

    for ($i = 1; $i <= 4; $i++) {
        $r_email[] = $_POST['email_' . $i];
    }
    foreach ($r_email as $email) {
        //HERE WE DO MYSQL_QUERY AND...
        $sql = "select p.productid,pd.productname,pd.price,pd.description,pd.image from products p , products_description pd where pd.productid = p.productid 
               and p.productid = $id";
        //..CHECK FOR ERRORS (BUT ONLY IN DEVELOPING ENVIRONMENT: IN PRODUCTION ENVIRONMENT SUPPRESS ERROR MESSAGES
        $result = mysql_query($sql) or die("Query was " . $sql . PHP_EOL . "Error is: " . mysql_error());
        $row = mysql_fetch_row($sql);
        $productname = $row[1];
        $thumb = $row[4];
        if ($email != "" && check_valid_email($email)) {
            $message = '<p style="width:450px; font-family:arial; font-size:14px;color:#717783;"></p><p style="width:450px; font-family:arial; font-size:14px;color:#717783;">
           These photos are being sent to you at the request of ' . $sender . ':</p><p style="width:450px; font-family:arial; font-size:14px;color:#717783;">' . $comment . '</p>
               <table style="width:450px; font-family:arial; font-size:14px;color:#717783;" border="0" width="480" cellspacing="0" cellpadding="0"><tr>';
            $message .= '<tr><td valign="top"><img src="http://www.websiteaddress.com/images/product-images/' . $thumb . '"  border="0" /><br><br>
    <div style="width:450px; font-family:arial; font-size:18px;color:#717783;">' . $productname . '</div>
        <div style="width:450px; font-family:arial; font-size:14px;color:#717783;">' . $descriptions . '<br><br></div></td></tr></table>';
        }
        $message .= '<p style="width:450px; font-family:arial; font-size:18px;color:#717783;">Thank you text</p></p>';
        mail($email, $subject, $message, $headers);
    }
} else {
    header("location:products.php");
    exit();
}
?>
<?php include("include/header.php"); ?>
<body class="wishlist">
    <table width="1000" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">
        <tr>
            <td width="43"></td>
            <td width="227"  height="62" background="images/sidebarbg.png" style="background-repeat:repeat">&nbsp;</td>
            <td width="728"><?php include("topaddress.php") ?></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td background="images/sidebarbg.png" style="background-repeat:repeat"></td>
            <td><div class="navigation">
<?php include("topnavigation.php") ?>
                </div></td>
        </tr>

        <td>&nbsp;</td>
        <td valign="top" background="images/sidebarbg.png" style="background-repeat:repeat"><div class="search">
<?php include("searchsidebar.php") ?>
            </div></td>
        <td>

            <div class="wishlist"><table width="613" border="0" cellspacing="0" cellpadding="0">
                    <tr><td width="613" height="20"></td></tr>
                    <tr>
                        <td style="font-size:16px">Product Detail has been Sent Successfully.</td>
                    </tr>
                    <tr><td height="20"></td></tr>
                    <tr>
                        <td>
                        </td>
                    </tr>
                </table></div>
            <div id="footer">
                <div class="footernav">  &nbsp; | &nbsp; <a href="products.php" class="black" title="Products">Products</a> &nbsp; | &nbsp; <a href="contact_us.php" class="black" title="Contact Us">Contact</a> &nbsp; | &nbsp; <a href="terms.php" class="black" title="Terms">Terms</a> <br />
                    <br />
                    <br />
                </div>

            </div></td>
    </tr>

</table>
<script language="javascript">
    function validate() {
   
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var sender = document.getElementById('sender').value;
        var email1 = document.getElementById('email_1').value;
        var email2 = document.getElementById('email_2').value;
        var email3 = document.getElementById('email_3').value;
        var email4 = document.getElementById('email_4').value;
   
        if(sender == "" || sender == " ") {
            alert('Please Enter your Name');
            document.getElementById('sender').focus();
            return false;
        }
        else if(reg.test(email1) == false) {
            alert('Please Enter valid Email Address');
            document.getElementById('email_1').focus();
            return false;
        }
        else if(reg.test(email2) == false  && email2!='') {
            alert('Invalid Email Address');
            document.getElementById('email_2').focus();
            return false;
        }else if(reg.test(email3) == false && email3!='') {
            alert('Invalid Email Address');
            document.getElementById('email_3').focus();
            return false;
        }else if(reg.test(email4) == false && email4!='') {
            alert('Invalid Email Address');
            document.getElementById('email_4').focus();
            return false;
        }else return
    }
</script>
</body>
</html>

Open in new window


Cheers
0
kmurphychiAuthor Commented:
Thanks everyone.  xterm I tried using your code on the previous version of code I had and it's still sending the blank emails out.  MarqusG, I tried copying your code and using that but for some weird reason it is still sending out the emails to the catchall email address.
0
Marco GasiFreelancerCommented:
I don't understand a thing: where is defined the 'catchall' email address? What is it exactly?
0
xtermCommented:
I'm an idiot, line 15 should be as below.  That will stop the blank emails.
if($r_email != "email_") {

Open in new window

0
kmurphychiAuthor Commented:
xterm it's still not working. Sorry.  I am trying it on the original code that I posted. The email that is sent to the catchall email address for the domain (the email address that is set up for bounces) is:

A message that you sent contained no recipient addresses, and therefore no
delivery could be attempted.
0
xtermCommented:
I put your code exactly as is with zero changes on my own server and it works perfectly and sends no empty emails.  No modifications are needed.

I think something may be wrong with the catchall address or the address being entered in the form, because the code works fine.  In fact, why don't you change the catchall email address to your own email address temporarily and fill out the form a few times and see for yourself.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.