Fail SMTP auth if credentials are configured but all mechanisms fail
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 11 Jan 2021 10:49:20 +0000 (11:49 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 11 Jan 2021 11:18:48 +0000 (12:18 +0100)
wcfsetup/install/files/lib/system/email/transport/SmtpEmailTransport.class.php

index 99b01f8ddaf7576cce512cde376e435deeffb0fd..54e9d6202d26e61c519ecf4173db8edd4cc9f450 100644 (file)
@@ -303,11 +303,12 @@ class SmtpEmailTransport implements IEmailTransport {
        protected function auth() {
                if (!$this->username || !$this->password) return;
                
+               $authException = null;
                foreach ($this->features as $feature) {
                        $parameters = explode(" ", $feature);
                        
                        if ($parameters[0] == 'auth') {
-                               // try mechanisms in order of preference
+                               // Try mechanisms in order of preference.
                                foreach (['login', 'plain'] as $method) {
                                        if (in_array($method, $parameters)) {
                                                switch ($method) {
@@ -317,6 +318,7 @@ class SmtpEmailTransport implements IEmailTransport {
                                                                        $this->read([334]);
                                                                }
                                                                catch (SystemException $e) {
+                                                                       $authException = $e;
                                                                        // try next authentication method
                                                                        continue 2;
                                                                }
@@ -326,6 +328,8 @@ class SmtpEmailTransport implements IEmailTransport {
                                                                $this->write(base64_encode($this->password));
                                                                $this->lastWrite = '*redacted*';
                                                                $this->read([235]);
+                                                               
+                                                               // Authentication was successful.
                                                                return;
                                                        break;
                                                        case 'plain':
@@ -335,22 +339,27 @@ class SmtpEmailTransport implements IEmailTransport {
                                                                        $this->read([334]);
                                                                }
                                                                catch (SystemException $e) {
+                                                                       $authException = $e;
                                                                        // try next authentication method
                                                                        continue 2;
                                                                }
                                                                $this->write(base64_encode("\0".$this->username."\0".$this->password));
                                                                $this->lastWrite = '*redacted*';
                                                                $this->read([235]);
+                                                               
+                                                               // Authentication was successful.
                                                                return;
                                                }
                                        }
                                }
                                
-                               return;
+                               // No mechanism was accepted.
+                               break;
                        }
                }
                
                // server does not support auth
+               throw new TransientFailure("Remote SMTP server does not support AUTH, but SMTP credentials are specified.", 0, $authException);
        }
        
        /**