Browse Source

Split SMTP auth mechanisms into separate methods.

ADmad 5 years ago
parent
commit
f6e46c9897

+ 30 - 6
src/Mailer/Transport/SmtpTransport.php

@@ -264,17 +264,41 @@ class SmtpTransport extends AbstractTransport
         $username = $this->_config['username'];
         $password = $this->_config['password'];
 
-        $replyCode = $this->_smtpSend(
+        $replyCode = $this->_authPlain($username, $password);
+        if ($replyCode === '235') {
+            return;
+        }
+
+        $this->_authLogin($username, $password);
+    }
+
+    /**
+     * Authenticate using AUTH PLAIN mechanism.
+     *
+     * @param string $username Username.
+     * @param string $password Password.
+     * @return string|null Response code for the command.
+     */
+    protected function _authPlain(string $username, string $password): ?string
+    {
+        return $this->_smtpSend(
             sprintf(
                 'AUTH PLAIN %s',
-                base64_encode($username . chr(0) . $username . chr(0) . $password)
+                base64_encode(chr(0) . $username . chr(0) . $password)
             ),
-            '235|504|535'
+            '235|504|534|535'
         );
-        if ($replyCode === '235') {
-            return;
-        }
+    }
 
+    /**
+     * Authenticate using AUTH LOGIN mechanism.
+     *
+     * @param string $username Username.
+     * @param string $password Password.
+     * @return void
+     */
+    protected function _authLogin(string $username, string $password): void
+    {
         $replyCode = $this->_smtpSend('AUTH LOGIN', '334|500|502|504');
         if ($replyCode === '334') {
             try {

+ 1 - 1
tests/TestCase/Mailer/Transport/SmtpTransportTest.php

@@ -96,7 +96,7 @@ class SmtpTransportTest extends TestCase
         $this->SmtpTransport->setSocket($this->socket);
         $this->SmtpTransport->setConfig(['client' => 'localhost']);
 
-        $this->credentialsEncoded = base64_encode('mark' . chr(0) . 'mark' . chr(0) . 'story');
+        $this->credentialsEncoded = base64_encode(chr(0) . 'mark' . chr(0) . 'story');
     }
 
     /**