Results 1 to 6 of 6

Thread: return true/false? if else in foreach

  1. #1
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question return true/false? if else in foreach

    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:
    PHP Code:
    $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:
    PHP Code:
            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...

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    have you tried printing out the original split array, before your foreach statement?
    Code:
    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.

  3. #3
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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...

  4. #4
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    anyone else? I can't figure it out...

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    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

    Code:
    $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);
    Code:
    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;
         }
    }

  6. #6
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •