Fatal error: Maximum execution time of 30 seconds exceeded

Hi, I need your help please,…I am using Swiftmailer in sending email,now what i need is how can i catch this "Fatal error: Maximum execution time of 30 seconds exceeded "…so that i can prompt this to the user.


 try{


        $transport = Swift_SmtpTransport::newInstance('smtp parameter here')

            ->setUsername('username')
            ->setPassword('password')
        ;

        $mailer = Swift_Mailer::newInstance($transport);

            $message = Swift_Message::newInstance()


                ->setSubject('this is the test')


                ->setFrom(array($email=> "john"))


                ->setTo(array($tosomeone)


                ->setBody('hello','text/html');

           $mailer->send($message);



        }







    }
    catch(Swift_SwiftException $err){
        $failed = $err->getMessage();
    }







Thank you

IMHO if you are exceeding 30 seconds of time ( a HUGE amount BTW) then it’s not something the user needs to be concerned with - it’s the programmer. You’ve got a loop or something in your code that is not doing what you expect

I’m afraid I am not familiar with the type of syntax you are using in your code, so I can offer no solution, other than to say it’s not the user’s fault and he/she should never see this happening. You need to figure out why you are getting this error.

try/catch won’t get fatal errors. If it is this section of the code that’s causing the issue, the problem likely is that it can’t connect to the mail server and it times out. The solution there would be to make sure the firewall is not restricting you, the mail server is running properly, etc.

Thank you for the reply…

Maybe this is the cause for my attachment the file size is 196kb file this will always raise to fatal error like i posted above…but if i will attach only 27.7kb,it succeeded send.

Thank you in advance.

You can change the maximum execution time allowed in the PHP.ini settings or using the htaccess file. Also consider using a cli script, which has no maximum execution time restriction (though this will need to be fired from a cron job and is tricky to set up).

IMHO this is a poor solution to the OP’s problem. Thirty seconds of cpu time is not something that anyone should be running. It is a problem, not a “long running task”, unless he is calculating planetary orbits or something.

And - if I am not mistaken, the html file upload process occurs before the php script runs, therefore it does not impact the cpu time limit of a script. I could be wrong here tho.

Although throwing more time or memory at a script might work, unless it’s running on your localhost and you don’t mind waiting (and even then) it’s much preferable to fix the problem if you can.

This is especially true if your on a shared host. They aren’t likely to be pleased if you start hammering resources.

Then again, if only a minor tweak is needed, it might be OK I suppose.

Are you sure it’s the file size? i.e. not doing multiple uploads/sends at the same time.

I noticed this when comparing the code from your post with how it is in the documentation:


->setFrom(array($email=> "john"))

Change that bit to:


->setFrom(array("$email" => 'John Doe'))

Give it a try once that change is made, did it make any difference to the execution time (better, worse or about the same)?