0

I'm sending multiple emails from a PHP application, and I want to inform the user of the emails that failed to be sent.

What is the most elegant way to do the error handling when

  • I don't want to throw an exception that terminates the sending of the remaining emails
  • The call goes through several method calls

What I want is to get the $notificationSucceeded back from Suggestion::notifyDeletionToAll() to SuggestionController somehow nicely from all notifications.

The depth of the call stack made me doubt if returning it through all the methods is the most elegant way, especially when I already have a return value from Suggestion::cancel().

Is there a better way?

Controller:

class SuggestionController {
    function cancelSuggestion($suggestionId)
    {
        $suggestion = new Suggestion();
        $suggestion->fetch($suggestionId);

        $suggestionDeleted = $suggestion->cancel();

        print json_encode(array(
            'status' => 'ok',
            'suggestionDeleted' => $suggestionDeleted,
        ));
    }
}

Suggestion class:

class Suggestion {

    /**
     * Cancels membership of current user in the suggestion
     */
    public function cancel()
    {
        $this->cancelMembership();

        if (!$this->hasAcceptedMembers()) {
            $this->deleteAndNotify();
            return true;
        }

        return false;
    }

    /**
     * Deletes the suggestion and notifies all the users in it
     */
    private function deleteAndNotify()
    {
        $this->notifyDeletionToAll();
        DB::inst()->query("DELETE FROM suggestions WHERE id = {$this->id}");
    }

    /**
     * Notifies about the deletion of the suggestion to all members (users in the suggestion)
     */
    private function notifyDeletionToAll()
    {
        $result = DB::inst()->query("SELECT user_id FROM suggestions_users
            WHERE suggestion_id = {$this->id}");
        while ($member_id = DB::inst()->fetchFirstField($result)) {
            $member = new User();
            $member->fetch($member_id);
            $notificationSucceeded = $member->notifySuggestionDeleted($this);
        }
    }
}
3
  • Your question isn't clear. Add more details: your important code snippets, description of desired behavior e t.c. Commented Apr 7, 2014 at 6:03
  • Since you said "The call goes through several method calls", how about using return value of the method and everytime "false" is returned, add a line with something like "Email #1 was unable to send" into variable? Commented Apr 7, 2014 at 6:05
  • Thanks for the quick response, I clarified the question. Commented Apr 7, 2014 at 6:32

1 Answer 1

1

I can't understand your question clearly. but i hope this will help you

$successfully_sent_arr = array();
$failure_notsent_arr   = array();
if($mail->Send())
{
    $successfully_sent_arr[] = $to_email;
    //Once the last loop occurs, update this array in database
}
else
{
    $failure_notsent_arr[] = $to_email;
    //Once the last loop occurs, update this array in database
}
Sign up to request clarification or add additional context in comments.

2 Comments

I clarified the question.
@Ipa Ok will check and come back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.