Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

try catch does catches the errors, want to run again if there is error

<?php
$mail = new PHPMailer(true);
try {
  $mail->IsHTML(true); 
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
  $mail->From = $mail->Username;
  $mail->AddAddress($mail->Username);
$mail->Body = 'body'; 
  $mail->Subject="subject";
  $mail->Send();
  echo "Mail was sent.</p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

Open in new window


phpmailer
would I have to change the
$mail->Send() method
to run again if $mail is not sent
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Turn error reporting on and see if any errors are being thrown, a error may not throw an exception
ASKER CERTIFIED SOLUTION
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think elvin66 has you on the right track.

After your exception handler has caught the error, it's done.  Unless your code throws another exception from inside the exception handler (or does something "fatal") the error handling has been completed, full stop. Try running this a few times to see the effect.  You can extend exception handlers, as this shows.

In the original code snippet, I am not sure I understand the control structures -- it's not clear to me how you would get to the second catch block.

<?php // RAY_try_throw_catch.php
error_reporting(E_ALL);
echo "<pre>";

// DEMONSTRATE A TRY WITH MULTIPLE CATCH BLOCKS
// http://php.net/manual/en/reserved.exceptions.php
// http://php.net/manual/en/language.exceptions.php
// http://php.net/manual/en/language.exceptions.extending.php

Class ScrewUP
{
    public function __construct()
    {
        $this->code = rand(1,3);
    }

    public function hiccup()
    {
        if ($this->code == 1) throw new Exception_one;
        if ($this->code == 2) throw new Exception_two;
        throw new Exception;
    }
}

Class Exception_One extends Exception
{
    public function __construct()
    {
        echo "Exception One ";
    }
}

Class Exception_Two extends Exception
{
    public function __construct()
    {
        echo "Exception Two ";
    }
}

$x = new Screwup();
try
{
    $x->hiccup();
}
catch (exception_one $e)
{
    echo "Caught 1 ";
    var_dump($e);
}
catch (exception_two $e)
{
    echo "Caught 2 ";
    var_dump($e);
}
catch (exception $e)
{
    echo "Caught UnExtended ";
    var_dump($e);
}

Open in new window

Avatar of rgb192

ASKER

$mail->Send();

mail is sent


there is a catch which can be counted with a variable if mail was NOT sent


but there is no way of telling if mail was sent
no way of telling if mail was sent
Yeah, that's the unfortunate thing about email - without a lot of work there is no accountability.  All of the headers can be spoofed, there is no return signal, etc.  Once your PHP script tells PHP to send the mail, you've done all you can in PHP.  That's why I recommend ConstantContact.com -- they take care of a lot of the difficult stuff for you.
Avatar of rgb192

ASKER

I think the $tries variable is the closest to making a send
thanks
Thanks for the points. I just noticed a typo in my sample code

tries($tries+1);   should be

test($tries+1);

but you would obviously rename that to your function name. Good luck with it.