Page 1 of 1

mental blockage....

Posted: Thu May 22, 2008 3:24 am
by dillion
For the life of me, I can't remember how to continue the Swiftmail process rather than breaking out if the email address is invalid.

I had a cron set up to send out emails at 1am, and this morning I received a cron email displaying error:
PHP Fatal error: Uncaught exception 'Exception' with message 'The recipients parameter must either be a valid string email address, an instance of Swift_RecipientList or an instance of Swift_Address.' in <PATH TO CLASS.SWIFTMAIL.PHP>:337
Stack trace:
#0 <PATH TO PHP> (58): Swift->send(Object(Swift_Message), 'me@ohoh...', 'email@domain...')
#1 {main}
thrown in <PATH TO CLASS.SWIFTMAIL.PHP> on line 337
Turns out that the email address was invalid (me@ohoh>com rather than me@ohoh.com).

I thought

Code: Select all

 
// call swiftmail class
......
$sent = $swift->send($message, $row['subscriberemailaddress'], "from@domain.com"); // Try sending the email
if(!$sent) {
  // error sending
  $param_sent = "0";
  $param_status = ":: SENDFAILED";
  $log = Swift_LogContainer::getLog();
  $param_statusmessage = $log->dump(true);
  $log->addFailedRecipient($row['subscriberemailaddress']);
}
else {
  // send was successful
  $param_sent = "1";
  $param_status = ":: SENDSUCCESS";
  $log = Swift_LogContainer::getLog();
  $param_statusmessage = $log->dump(true);
}
// do mySQL stuff
.....
 
Am I missing something silly somewhere?

Re: mental blockage....

Posted: Wed May 28, 2008 3:29 am
by dillion
bump.... Any ideas? :(

What I don't get is why if $sent failed then surely it should log (e.g. $param_sent = "0") then carry on to next email address rather than fail completely?

Code: Select all

 
$sent = $swift->send($message, $row['subscriberemailaddress'], "from@domain.com"); // Try sending the email
if(!$sent) {
  // error sending
  $param_sent = "0";
  $param_status = ":: SENDFAILED";
  $log = Swift_LogContainer::getLog();
  $param_statusmessage = $log->dump(true);
  $log->addFailedRecipient($row['subscriberemailaddress']);
}
else {
  // email was sent
}
 

Re: mental blockage....

Posted: Wed May 28, 2008 9:12 pm
by Chris Corbyn
The exception is thrown before the logger gets a chance to log it. The reason is that sending is never even attempted because the address is not valid. I could probably have changed that but v4 won't behave the same anyway.

Code: Select all

try
{
  $sent = $swift->send( ... );
}
catch (Exception $e)
{
  //Handle failure here
}

Re: mental blockage....

Posted: Thu May 29, 2008 6:25 am
by dillion
thanks mate for your response. I already have a try catch statement:

Code: Select all

 
try {       
            //Start Swift
            $swift =& new Swift(new Swift_Connection_SMTP($mail_host));
            $log =& Swift_LogContainer::getLog();
            $log->setLogLevel(4);
            
            // load a Anti flood plugin
            $swift->attachPlugin(new Swift_Plugin_AntiFlood($smtp_flood_run, $smtp_flood_pause), "anti-flood");
        
            do {
                $message  =& new Swift_Message("subject");
                $message->attach(new Swift_Message_Part("message"), "text/plain"));
                $message->attach(new Swift_Message_Part("<b>message</b>), "text/html"));
                $message->setReturnPath("mail@domain.com");
 
                $sent = $swift->send($message, $row['emailaddress'], "noreply@domain.com"); // Try sending the email
                if(!$sent) {
                    // error sending
                    $param_sent = "0";
                    $param_status = ":: SENDFAILED";
                    $log = Swift_LogContainer::getLog();
                    $param_statusmessage = $log->dump(true);
                    $log->addFailedRecipient($row['emailaddress']);
                }
                else {
                    // send was successful
                    $param_sent = "1";
                    $param_status = ":: SENDSUCCESS";
                }
                // log status
    
            } while ($row = mysql_fetch_assoc($set));
            
            $swift->disconnect();
        } catch (Swift_ConnectionException $e) {
            echo "There was a problem communicating with SMTP :" . $e->getMessage();
            $swift->reset();
            exit();
        } catch (Swift_Message_MimeException $e) {
            echo "There was an unexpected problem building the email: " . $e->getMessage();
            $swift->reset();
            exit();
        }
 
I take it that I need to add another try catch statement or place it inside the loop? :dubious:

Re: mental blockage....

Posted: Fri May 30, 2008 9:28 pm
by Chris Corbyn
I have to wonder what I was thinking by throwing an Exception which isn't subclassed. The problem now is that if you place a try/catch inside the other you'd have to catch everything of type "Exception" which would break things. I can only provide you with a dirty hack I'm afraid.

Code: Select all

try {      
            //Start Swift
            $swift =& new Swift(new Swift_Connection_SMTP($mail_host));
            $log =& Swift_LogContainer::getLog();
            $log->setLogLevel(4);
           
            // load a Anti flood plugin
            $swift->attachPlugin(new Swift_Plugin_AntiFlood($smtp_flood_run, $smtp_flood_pause), "anti-flood");
       
            do {
              try {
                $message  =& new Swift_Message("subject");
                $message->attach(new Swift_Message_Part("message"), "text/plain"));
                $message->attach(new Swift_Message_Part("<b>message</b>", "text/html"));
                $message->setReturnPath("mail@domain.com");
 
                $sent = $swift->send($message, $row['emailaddress'], "noreply@domain.com"); // Try sending the email
                if(!$sent) {
                    // error sending
                    $param_sent = "0";
                    $param_status = ":: SENDFAILED";
                    $log = Swift_LogContainer::getLog();
                    $param_statusmessage = $log->dump(true);
                    $log->addFailedRecipient($row['emailaddress']);
                }
                else {
                    // send was successful
                    $param_sent = "1";
                    $param_status = ":: SENDSUCCESS";
                }
                // log status
              } catch (Exception $e) {
                if (get_class($e) != 'Exception') { //Re-throw it if not just "Exception"
                  throw $e;
                }
              }
   
            } while ($row = mysql_fetch_assoc($set));
           
            $swift->disconnect();
        } catch (Swift_ConnectionException $e) {
            echo "There was a problem communicating with SMTP :" . $e->getMessage();
            $swift->reset();
            exit();
        } catch (Swift_Message_MimeException $e) {
            echo "There was an unexpected problem building the email: " . $e->getMessage();
            $swift->reset();
            exit();
        }
 

Re: mental blockage....

Posted: Tue Jun 03, 2008 3:39 am
by dillion
very much appreciated mate! :cool:

I take it that version 4 won't have this issue?

Btw, hope all is still going well in Australia. :)

Re: mental blockage....

Posted: Tue Jun 03, 2008 7:15 am
by Chris Corbyn
dillion wrote:very much appreciated mate! :cool:

I take it that version 4 won't have this issue?

Btw, hope all is still going well in Australia. :)
No it won't. It's not so aggressive at throwing exceptions and it certainly won't throw anything which is only "Exception"!