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 6008033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:46:01+00:00 2026-05-23T01:46:01+00:00

This user asked a similar question but had no viable solution in the answers,

  • 0

This user asked a similar question but had no viable solution in the answers, so here is am revisiting the topic.

I would like to have emails sent from custom exceptions in a clean way. I can do this easy with plain old errors from a custom error_handler, but with extended exceptions I am a bit stumped.

The main problem is I rely on Zend_Mail to have transparent cross-platform mailing, easy smtp/ssl configuration for gmail account usage and a bunch of other goodies. I would like to access my Zend_Mail object in a custom exception. Static calls are not really an option that I am willing to use, although it seems to be the easiest to use a 3rd party object in a custom exception. Here is the constructer of base exception class:

 public function __construct($message = null, $code = 0,
                             Exception $previous = null);

The only solution I think may be worthwhile at the time being is adding a parameter for mailer object in the extended exception class, yet I do not want to require all subclasses to pass in mailer object. The idea is that maybe this mailer obj. would be better off being an optional dependency.

__construct signature would then become:

      public function __construct($mailobj = null, $message = null, $code = 0,
                                  Exception $previous = null);  

Notice the order of parameters, only $code is required and it is in the middle of the signature! This poses another issue but that is for another day. My question for today is, does anyone have ideas / suggestions on how to deal with dependencies in custom exception classes?
Keep in mind we are aiming for testable code.

Another easy option would have been to have the mailing code in a catch block, but that kind of defeats the purpose of using custom exceptions in my opinion, because the client of this class now has to think of adding mailing code in all catch blocks.

  • 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-05-23T01:46:02+00:00Added an answer on May 23, 2026 at 1:46 am

    I would approach it like this (am I missing some requirement here?)

    class CustomException extends Exception // Zend_Exception?
    {
        public function __construct($message = null, $code = 0, Exception $previous = null) {
            $mailobj = new Zend_Mail(/* ... */);
            // ....
            try {  // In order not to get infinitelly looped
                $mailobj->send(/*...*/);
            } catch(Exception $e) { }
        }
    }
    

    Whenever this exception is instantiated (i.e. throw new CustomException() ), someone will be receiving an email.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a similar question like this yesterday but after waiting for ever I
I asked a similar question here a while back but all the answers were
Various incarnations of this question have been asked here before, but I thought I'd
I asked a similar question over here:( Upload clocking script ), but then changed
I have asked a similar question to this but for GMail and I was
i have asked a similar question earlier, but this time i need a method
I previously asked a similar question on this topic a while back and got
Hey a similar question has been asked before, but none that answers mine exactly.
I've asked a similar question before, but I've done some more research and this
OK, I had asked a similar question before and had got an answer but

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.