Log in

View Full Version : return true/false? if else in foreach



jamiller
03-14-2008, 05:58 PM
I'm creating an email script and am sending an 'email' variable to the send.php script. Because the email var isn't an array I make it one first:


$emails = $_POST['emails'];
$email_ar = split('[;,]', $emails);


I now need to send each individual email to the mailing function sendMail with the email address as a param. But in that email var above I have a bunch of groups that contain many emails. So first I need to select the group from my database and then all emails under that group. So that email var could look like: $emails = "group1; group2; test@testing.com"

I create foreach loop and an if else statement to decifer between "group" emailing lists and single email addresses as so:


foreach($email_ar as $email) {
// IF EMAIL IS A GROUP EMAIL
if(!stristr($email, "@") && !stristr($email, ".")) {
$result = mysql_query("SELECT * FROM `newsalert` WHERE `group`='$email'");
while($row = mysql_fetch_array($result)) {
sendMail($row['email']);
}
// ELSE IT'S A SINGLE ADDRESS
} else {
sendMail($email);
}
}


Wierd thing is that the if else statement within the foreach loop seems to just quit after the first element is processed. I'm thinking it could be a return false/true thing but don't really know where to put it...

boogyman
03-14-2008, 06:44 PM
have you tried printing out the original split array, before your foreach statement?


print_r($email_ar);

that will first tell you if your split is happening correctly.

if it is splitting correctly, the second bit is that in your sendMail() function there is something generated that is forcing the break of the foreach.

jamiller
03-14-2008, 07:39 PM
Yes, the split is working correctly. I also echoed out $email in the foreach to make sure.

Right now, for testing, the sendMail($addy) function only echoes out $addy so it's nothing within this function...

jamiller
03-19-2008, 07:09 PM
anyone else? I can't figure it out...

boogyman
03-19-2008, 08:02 PM
the only other thing I can think of is explode the groups into the email array before you run the email script.

it might also be useful to explicitly set a return value for whether the email was able to send or not



$n_email = array();
foreach($email_ar as $email)
{
if(!stristr($email, "@") && !stristr($email, "."))
{
$res = mysql_query("SELECT * FROM `newsalert` WHERE `group`='$email'");
while($row = mysql_fetch_array($res))
{
$n_email[] = $result;
}
else
{
$n_email[] = $email;
}
}
if(!is_array($n_email))
$n_email[] = array($n_email);

foreach($n_email as $send)
{
if(sendMail($send))
{
$sent[] = $send;
}
else
{
$not[] = $send;
}
}

// track emails that were not sent;
print_r($not);
// track success emails sent;
print_r($sent);




function sendMail($email)
{
// gets rid of any leading/trailing whitespace in the email address
$to = trim($email);
$subject = "subject";
$from = "From: Name <email@domain.ext>";
$body = "Message Content";

if(mail($to,$subject,$body,$from))
{
return true;
}
else
{
return false;
}
}

jamiller
03-25-2008, 05:34 PM
Just to let everyone know I was able to solve my problem!! The problem was occurring when the array was split. When I viewed the print_r($email_ar) in the browser everything looked fine. Then I viewed the SOURCE CODE and immediately saw the problem. There was a white space in front of every array element except the first.

By adding trim($email) to the beginning of my foreach loop MySQL was able to perform a successful query.