0

I have a mailer script that is looping, processing outgoing emails from my server. Occasionally it hangs up with the following error.

PHP Fatal error: Uncaught exception 'phpmailerException' with message 'SMTP Error: Data not accepted.'

This causes my script to die before the reminder of the messages can complete.

Here is the code that kicks off the email.

$message = new \PHPMailer(true);
$message -> IsSMTP();
try
{
    $message -> SMTPAuth = true;
    $message -> Host = Config::HOST;
    $message -> Port = Config::PORT;
    $message -> Username = $account;
    $message -> Password = Config::PASS;
    $message -> AddReplyTo($account, Config::NAME);
    $message -> SetFrom($account, Config::NAME);
    $message -> AddAddress($recipient[0], $recipient[1]." ".$recipient[2]);
    $message -> Subject = $recipient,$this->subject;
    $message -> AltBody = 'Please enable HTML viewing in order to view this message. Thank you.';
    $message -> MsgHTML($recipient,$this->body);
    if($attachment !== false)
        $message->AddAttachment($attachment);
    $message -> Send();
}
catch (phpmailerException $e)
{
    return $error -> errorMessage();
}
catch (Exception $e)
{
    return $error -> getMessage();
}

I don't seem to be catching the exception. How can I recover from this gracefully?

EDIT

It was a namespace issue as indicated below.

Jason George
  • 6,992
  • 8
  • 44
  • 60
  • 2
    Have you tried putting the instantiation and `IsSMTP()` call inside the `try`? – Madbreaks Apr 13 '12 at 20:15
  • 1
    possible duplicate of [How do I catch a PHP Fatal Error](http://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error) – Treffynnon Apr 13 '12 at 20:16
  • @Madbreaks, I'll try giving this a go. It will take some work to recreate the failure to see if it works. – Jason George Apr 13 '12 at 21:46
  • @Treffynnon, can I get away with modifying the PhpMailer script to throw less than fatal exceptions? – Jason George Apr 13 '12 at 21:47
  • 1
    @Treffynnon This error is being caused by an uncaught exception. It's that exception that the op is attempting to catch..not the error itself. – Madbreaks Apr 13 '12 at 21:50
  • 1
    @JasonGeorge While it's true that you can't catch fatal errors, you can certainly attempt to catch the exception that's *causing* the fatal error. – Madbreaks Apr 13 '12 at 21:51
  • 1
    could be a namespace error, try this: 'catch (\phpmailerException $e)' – max Apr 13 '12 at 23:16
  • @papirtiger It was a namespace issue. Adding \phpmailerException corrected the issue (that and changing $e to $error). If you'll add your response as the answer I can accept it. – Jason George Apr 14 '12 at 21:24

1 Answers1

4

Most likely a namespace error - the sneakiest of errors.

catch (\phpmailerException $e)
max
  • 96,212
  • 14
  • 104
  • 165