Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8364387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:25:07+00:00 2026-06-09T12:25:07+00:00

given a loop that sends an email to all subscriptions in an array foreach($subscriptions

  • 0

given a loop that sends an email to all subscriptions in an array

foreach($subscriptions as $s){
    if(!$s->send_email()){

    }
}

What would be the cleanest way to trigger a callback if all models have mailed successfully or show an error if one of the models fails to mail. Is it common to save all error messages till the end of the loop and print them as a whole, or break the loop with an error.

I’m using this in combination with a JSON REST API saving a project (/projects/1) which in turn emails all users.

The method I’m using now feels dirty with lot’s of nested if else, sending 3 different response on different places

if($project->save()){
    $subscriptions = Subscription::model()->findAllByAttributes(array('planning_id' => $planning->id));
    foreach($subscriptions as $s){
        if(!$s->send_email()){
            $errors[] = "failed to send email. Subscription ". $s->id;
        }
     }
     if(count($errors) > 0){
          //send json api response with error response
     } else {
         //send json api success response
    }
} else {
    //send json api response with project error response
}

I was wondering what convention is concerning this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:25:08+00:00Added an answer on June 9, 2026 at 12:25 pm

    It is a little messy – and it combines multiple concerns within the “save” function – anyone reading the code needs to understand what “save” means, how we loop through the contacts etc.

    I’d refactor it as follows:

    if($project->save()){
        $subscriptions = Subscription::model()->findAllByAttributes(array('planning_id' => $planning->id));
         $errors = sendMailToSubscribers($subscriptions);
    
         $response = determineResponse($errors);
         // send JSON API response
    
    } else {
        //send json api response with project error response
    }
    
    function sendMailToSubscribers($subscriptions){
      foreach($subscriptions as $s){
            if(!$s->send_email()){
                $errors[] = "failed to send email. Subscription ". $s->id;
            }
         }
       return $errors;
      }
    function determineResponse($errors){
            if(count($errors) > 0){
              //return json api response with error response
         } else {
             //return json api success response
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SharpDX has a RenderLoop that runs a given delegate in the render loop: RenderLoop.Run(m_RenderForm,
Using PHP , I would like to make a while loop that reads a
I need to create a PHP class that would send e-mails to all members
Given that the web application doesn't have su privileges, I'd like to execute a
Given a MySQL table of real estate data, I would like to generate a
I have an HTML form that sends 索索索 using post to my PHP file.
Hey I was working on an android application that sends and receives text messages,
I have a daterange (from, to) that i want loop through an different intervals
IS there a way to post a $var to $PHP_SELF so that I can
I notice that NetBeans is warning me about using Thread.sleep() in a while loop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.