Jump to content

How to take out empty results when emailing


tlflow

Recommended Posts

Hi All,

 

I'm baffled as to how to do this.  No doubt, I'm making this harder than it has to be. 

 

Client wants to 10 textboxes that once the send button is clicked it sends a generic email to 10 people.  This is all simple until somebody tries just inserting email addresses in say the third box and the ninth box.

 

How do I get my code to skip over all the empty strings (results from empty textboxes) and send the ones that just have actual email addresses in them?

 

I've been reading over foreach in the php manual and it seems like I could use it, but I have no clue as to how to even start with that yet.  I've even tried bringing everything over and then exploding it, but that seems pointless and a waste of time - (besides I can't figure out what would happen if the last box is empty).

 

Any ideas?

 

Thanks,

 

Tlflow

Link to comment
Share on other sites

Sure, 

 

Here's what I have so far...mind you, it's not much...

 

$email1 = $_POST['email1'];

$email2 = $_POST['email2'];

$email3 = $_POST['email3'];

$email4 = $_POST['email4'];

$email5 = $_POST['email5'];

$email6 = $_POST['email6'];

$email7 = $_POST['email7'];

$email8 = $_POST['email8'];

$email9 = $_POST['email9'];

$email10 = $_POST['email10'];

 

$mailto = "$email1,$email2,$email3";

echo $mailto;

 

$array = explode(",", $mailto);

 

function isEmpty($array = array()) {

foreach ($array as $element) {

if (!empty($element)) {

}

  }

return true;

}

Link to comment
Share on other sites

<?php
// checks if form is submitted, may be edited
// depending on your submit button value + name
if ($_POST['submit'] == "submit"){
foreach($_POST as $p){
if (!empty($p)){
// may want to use preg_match,eregi,ereg etc to
// validate that the input is in fact an email
// then send the email
}}}
?>

Link to comment
Share on other sites

OK,

 

I kinda get it...I think that's the direction I was going towards, but I still don't get how ito just print out the ones that aren't empty as one string (I guess I shouldve said that earlier).  I'm thinking having it print out to one string (for example, $mailto = "email1@yahoo.com, email2@hotmail.com, email3@gmail.com";) would be the best way to send it, right? The way I have it now, it would be $mailto = "email1@yahoo.com, , email3@gmail.com" if my user didn't enter anything into box #2.

 

Thanks,

 

Tlflow

Link to comment
Share on other sites

OK, Ken2K7, maybe I'm just missing it....this is what I have so far:

 

<?

// $mailto = "$_POST['email1'], $_POST['email2'], $_POST['email3'], $_POST['email4'], $_POST['email5'], $_POST['email6'], $_POST['email7'], $_POST['email8'], $_POST['email9'], $_POST['email10']";

$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$email3 = $_POST['email3'];
$email4 = $_POST['email4'];
$email5 = $_POST['email5'];
$email6 = $_POST['email6'];
$email7 = $_POST['email7'];
$email8 = $_POST['email8'];
$email9 = $_POST['email9'];
$email10 = $_POST['email10'];
$myname = $_POST['myname'] ;
$myemail = $_POST['myemail'] ;		


// checks if form is submitted, may be edited
// depending on your submit button value + name
if (!$_POST['submit'] == "submit"){

foreach($_POST as $p){
if (!empty($p)){

 	// may want to use preg_match,eregi,ereg etc to
    // validate that the input is in fact an email
    // then send the email

    $subject = "Help Make a Miracle Happen!" ;
    
    $formurl = "form.php" ;
    $errorurl = "error.php" ;
    $thankyouurl = "thankyou.php" ;
    
    $uself = 1;

    $headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
    $http_referrer = getenv( "HTTP_REFERER" );


    if (empty($myname) || empty($myemail)) {
       header( "Location: $errorurl" );
       exit ;
    }
    if ( ereg( "[\r\n]", $myname ) || ereg( "[\r\n]", $myemail ) ) {
    	header( "Location: $errorurl" );
    	exit ;
    }

    $messageproper =
    
    	"Hi,\n\n" .
    	"This is a simple message.\n\n" .
    	"Sincerely,\n\n" .
    	"$myname\n\n" ;

      mail($mailto, $subject, $messageproper,
      	"From: \"$myname\" <$myemail>" . $headersep . "Reply-To: \"$myname\" <$myemail>" . $headersep . "X-Mailer: chfeedback.php 2.07" );
      	header( "Location: $thankyouurl" );
      exit ;

		}}}

?>



 

Link to comment
Share on other sites

Try this: (Read my comment)

 

<?php
// must have the submit's input attr name to be "submit" and value to be "submit"
if ($_POST['submit'] == "submit"){
$errorurl = "error.php";
if (empty($_POST['myname']) || empty($_POST['myemail']) || !preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i",$_POST['email'])){
	header("Location: $errorurl");
	exit;
}
else {
	$myname = mysql_real_escape_string($_POST['myname']);
	$myemail = mysql_real_escape_string($_POST['myemail']);
}
foreach($_POST as $p){
	if (!empty($p)){
    		$subject = "Help Make a Miracle Happen!" ;
    		$formurl = "form.php" ;
    		$thankyouurl = "thankyou.php" ;
    		$uself = 1;
    		$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
    		
	    $messageproper =
	    "Hi,\r\n\r\n" .
    		"This is a simple message.\r\n\r\n" .
    		"Sincerely,\r\n\r\n" .
    		"$myname\r\n\r\n" ;
    		
    		// Ken2k7: $mailto is undefined up to this point //
    		mail($mailto, $subject, $messageproper,
      			"From: \"$myname\" <$myemail>" . $headersep . "Reply-To: \"$myname\" <$myemail>" . $headersep . "X-Mailer: chfeedback.php 2.07" );
      		header( "Location: $thankyouurl" );
      		exit ;
      	}
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.