Fix Bug when trying to send a mail after the SMTPMailSender threw an exception
authorschlimpf <github.com@schlipf.it>
Fri, 13 Dec 2013 09:01:15 +0000 (10:01 +0100)
committerschlimpf <github.com@schlipf.it>
Fri, 13 Dec 2013 09:01:15 +0000 (10:01 +0100)
Added a abort() function to cancel the current process.
In case the SMTPMailSender throws an exception which is catched by another script, no further mails could be sent because the last command was not aborted.
It may be a good idea to use a different exception type than "SystemException"

wcfsetup/install/files/lib/system/mail/SMTPMailSender.class.php

index 1a1cd515d44ccb81d9b9c3ca55b7620d1ea8bc74..d2b9edf1617c32029b0517096a203c210fbc4c67 100644 (file)
@@ -137,6 +137,7 @@ class SMTPMailSender extends MailSender {
                $this->write('MAIL FROM:<'.$mail->getFrom().'>');
                $this->getSMTPStatus();
                if ($this->statusCode != 250) {
+                       $this->abort();
                        throw new SystemException($this->formatError("wrong from format '".$mail->getFrom()."'"));
                }
                
@@ -147,6 +148,7 @@ class SMTPMailSender extends MailSender {
                        $this->getSMTPStatus();
                        if ($this->statusCode != 250 && $this->statusCode != 251) {
                                if ($this->statusCode < 550) {
+                                       $this->abort();
                                        throw new SystemException($this->formatError("wrong recipient format '".$recipient."'"));
                                }
                                continue;
@@ -154,7 +156,7 @@ class SMTPMailSender extends MailSender {
                        $recipientCounter++;
                }
                if (!$recipientCounter) {
-                       $this->write("RSET");
+                       $this->abort();
                        return;
                }
                
@@ -162,6 +164,7 @@ class SMTPMailSender extends MailSender {
                $this->write("DATA");
                $this->getSMTPStatus();
                if ($this->statusCode != 354 && $this->statusCode != 250) {
+                       $this->abort();
                        throw new SystemException($this->formatError("smtp error"));
                }
                
@@ -179,6 +182,7 @@ class SMTPMailSender extends MailSender {
                
                $this->getSMTPStatus();
                if ($this->statusCode != 250) {
+                       $this->abort();
                        throw new SystemException($this->formatError("message sending failed"));
                }
        }
@@ -211,6 +215,15 @@ class SMTPMailSender extends MailSender {
                return $result;
        }
        
+       /**
+        * Aborts the current process. This is needed in case a new mail should be
+        * sent after a exception has occured
+        */
+       protected function abort() {
+               $this->write("RSET");
+               $this->read(); // read response, but do not care about status here
+       }
+       
        /**
         * Gets error code and message from a server message.
         *