Skip to content

Commit 23b58b3

Browse files
committed
- fix not having initial 'handshake' in smtp plain auth method causing failed authentication
- fix uppercase wording for login/plain into lowercase - added $SMTPAuthMethod in Config\Email file with default value of login - refactor SMTPAuthenticate() to reduce complexity and code repetition
1 parent d207194 commit 23b58b3

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

app/Config/Email.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class Email extends BaseConfig
3030
*/
3131
public string $SMTPHost = '';
3232

33+
/**
34+
* Which SMTP authentication method to use: login, plain
35+
*/
36+
public string $SMTPAuthMethod = 'login';
37+
3338
/**
3439
* SMTP Username
3540
*/

system/Email/Email.php

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

user_guide_src/source/libraries/email.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Preference Default Value Options Description
120120
or ``smtp``
121121
**mailPath** /usr/sbin/sendmail The server path to Sendmail.
122122
**SMTPHost** SMTP Server Hostname.
123-
**SMTPAuthMethod** LOGIN ``LOGIN``, ``PLAIN`` SMTP Authentication Method.
123+
**SMTPAuthMethod** login ``login``, ``plain`` SMTP Authentication Method.
124124
**SMTPUser** SMTP Username.
125125
**SMTPPass** SMTP Password.
126126
**SMTPPort** 25 SMTP Port. (If set to ``465``, TLS will be used for the connection

0 commit comments

Comments
 (0)