@@ -280,11 +280,11 @@ class Email
280280 protected $ SMTPAuth = false ;
281281
282282 /**
283- * Which SMTP atuh method to use ('LOGIN', 'PLAIN')
283+ * Which SMTP authentication method to use: login, plain
284284 *
285285 * @var string
286286 */
287- protected $ SMTPAuthMethod = 'LOGIN ' ;
287+ protected $ SMTPAuthMethod = 'login ' ;
288288
289289 /**
290290 * Whether to send a Reply-To header
@@ -2019,21 +2019,22 @@ protected function SMTPAuthenticate()
20192019 return false ;
20202020 }
20212021
2022- switch ($ this ->SMTPAuthMethod ) {
2023- case 'LOGIN ' :
2024- $ this ->sendData ('AUTH LOGIN ' );
2025- $ reply = $ this ->getSMTPData ();
2022+ // send initial 'handshake' command
2023+ $ this ->sendData ('AUTH ' . strtoupper ($ this ->SMTPAuthMethod ));
2024+ $ reply = $ this ->getSMTPData ();
20262025
2027- if (str_starts_with ($ reply , '503 ' )) { // Already authenticated
2028- return true ;
2029- }
2026+ if (str_starts_with ($ reply , '503 ' )) { // Already authenticated
2027+ return true ;
2028+ }
20302029
2031- if (! str_starts_with ($ reply , '334 ' )) {
2032- $ this ->setErrorMessage (lang ('Email.failedSMTPLogin ' , [$ reply ]));
2030+ if (! str_starts_with ($ reply , '334 ' )) {
2031+ $ this ->setErrorMessage (lang ('Email.failedSMTPLogin ' , [$ reply ]));
20332032
2034- return false ;
2035- }
2033+ return false ;
2034+ }
20362035
2036+ switch ($ this ->SMTPAuthMethod ) {
2037+ case 'login ' :
20372038 $ this ->sendData (base64_encode ($ this ->SMTPUser ));
20382039 $ reply = $ this ->getSMTPData ();
20392040
@@ -2044,31 +2045,13 @@ protected function SMTPAuthenticate()
20442045 }
20452046
20462047 $ this ->sendData (base64_encode ($ this ->SMTPPass ));
2047- $ reply = $ this ->getSMTPData ();
2048-
2049- if (! str_starts_with ($ reply , '235 ' )) {
2050- $ this ->setErrorMessage (lang ('Email.SMTPAuthPassword ' , [$ reply ]));
2051-
2052- return false ;
2053- }
20542048 break ;
20552049
2056- case 'PLAIN ' :
2057- // Generate single command for PLAIN authentication
2050+ case 'plain ' :
2051+ // send credentials as the single second command
20582052 $ authString = "\0" . $ this ->SMTPUser . "\0" . $ this ->SMTPPass ;
20592053
2060- $ this ->sendData ('AUTH PLAIN ' . base64_encode ($ authString ));
2061-
2062- if (str_starts_with ($ this ->getSMTPData (), '503 ' )) { // Already authenticated
2063- return true ;
2064- }
2065-
2066- if (! str_starts_with ($ this ->getSMTPData (), '235 ' )) {
2067- $ this ->setErrorMessage (lang ('Email.failedSMTPLogin ' , [$ this ->getSMTPData ()]));
2068-
2069- return false ;
2070- }
2071-
2054+ $ this ->sendData (base64_encode ($ authString ));
20722055 break ;
20732056
20742057 default :
@@ -2077,6 +2060,15 @@ protected function SMTPAuthenticate()
20772060 return false ;
20782061 }
20792062
2063+ $ reply = $ this ->getSMTPData ();
2064+ if (! str_starts_with ($ reply , '235 ' )) { // Authentication failed
2065+ $ errorMessage = $ this ->SMTPAuthMethod === 'plain ' ? 'Email.failedSMTPLogin ' : 'Email.SMTPAuthPassword ' ;
2066+
2067+ $ this ->setErrorMessage (lang ($ errorMessage , [$ reply ]));
2068+
2069+ return false ;
2070+ }
2071+
20802072 if ($ this ->SMTPKeepAlive ) {
20812073 $ this ->SMTPAuth = false ; // Prevent re-authentication for keep-alive sessions
20822074 }
0 commit comments