Skip to content

Commit f3b64ed

Browse files
committed
-revert: auto corrected (composer cs-fix) comments and auto added function return types
-comment: add depreceation comment for failedSMTPLogin email error message -refactor: improve authorization validation flow and email error response messages -added $SMTPAuthMethod in Config\Email file with default value of login
1 parent c283d1a commit f3b64ed

File tree

4 files changed

+76
-32
lines changed

4 files changed

+76
-32
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: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,13 @@ class Email
279279
*/
280280
protected $SMTPAuth = false;
281281

282+
/**
283+
* Which SMTP authentication method to use: login, plain
284+
*
285+
* @var string
286+
*/
287+
protected $SMTPAuthMethod = 'login';
288+
282289
/**
283290
* Whether to send a Reply-To header
284291
*
@@ -2019,45 +2026,72 @@ protected function SMTPAuthenticate()
20192026
return true;
20202027
}
20212028

2022-
if ($this->SMTPUser === '' && $this->SMTPPass === '') {
2029+
// If no username or password is set
2030+
if ($this->SMTPUser === '' || $this->SMTPPass === '') {
20232031
$this->setErrorMessage(lang('Email.noSMTPAuth'));
20242032

20252033
return false;
20262034
}
20272035

2028-
$this->sendData('AUTH LOGIN');
2036+
// normalize in case user entered capital words LOGIN/PLAIN
2037+
$this->SMTPAuthMethod = strtolower($this->SMTPAuthMethod);
2038+
2039+
// Validate supported authentication methods
2040+
$validMethods = ['login', 'plain'];
2041+
if (! in_array($this->SMTPAuthMethod, $validMethods, true)) {
2042+
$this->setErrorMessage(lang('Email.invalidSMTPAuthMethod', [$this->SMTPAuthMethod]));
2043+
2044+
return false;
2045+
}
2046+
2047+
// send initial 'AUTH' command
2048+
$this->sendData('AUTH ' . strtoupper($this->SMTPAuthMethod));
20292049
$reply = $this->getSMTPData();
20302050

20312051
if (str_starts_with($reply, '503')) { // Already authenticated
20322052
return true;
20332053
}
20342054

2055+
// if 'AUTH' command is unsuported by the server
20352056
if (! str_starts_with($reply, '334')) {
2036-
$this->setErrorMessage(lang('Email.failedSMTPLogin', [$reply]));
2057+
$this->setErrorMessage(lang('Email.failureSMTPAuthMethod', [strtoupper($this->SMTPAuthMethod)]));
20372058

20382059
return false;
20392060
}
20402061

2041-
$this->sendData(base64_encode($this->SMTPUser));
2042-
$reply = $this->getSMTPData();
2062+
switch ($this->SMTPAuthMethod) {
2063+
case 'login':
2064+
$this->sendData(base64_encode($this->SMTPUser));
2065+
$reply = $this->getSMTPData();
20432066

2044-
if (! str_starts_with($reply, '334')) {
2045-
$this->setErrorMessage(lang('Email.SMTPAuthUsername', [$reply]));
2067+
if (! str_starts_with($reply, '334')) {
2068+
$this->setErrorMessage(lang('Email.SMTPAuthUsername', [$reply]));
20462069

2047-
return false;
2070+
return false;
2071+
}
2072+
2073+
$this->sendData(base64_encode($this->SMTPPass));
2074+
break;
2075+
2076+
case 'plain':
2077+
// send credentials as the single second command
2078+
$authString = "\0" . $this->SMTPUser . "\0" . $this->SMTPPass;
2079+
2080+
$this->sendData(base64_encode($authString));
2081+
break;
20482082
}
20492083

2050-
$this->sendData(base64_encode($this->SMTPPass));
20512084
$reply = $this->getSMTPData();
2085+
if (! str_starts_with($reply, '235')) { // Authentication failed
2086+
$errorMessage = $this->SMTPAuthMethod === 'plain' ? 'Email.SMTPAuthCredentials' : 'Email.SMTPAuthPassword';
20522087

2053-
if (! str_starts_with($reply, '235')) {
2054-
$this->setErrorMessage(lang('Email.SMTPAuthPassword', [$reply]));
2088+
$this->setErrorMessage(lang($errorMessage, [$reply]));
20552089

20562090
return false;
20572091
}
20582092

20592093
if ($this->SMTPKeepAlive) {
2060-
$this->SMTPAuth = false;
2094+
$this->SMTPAuth = false; // Prevent re-authentication for keep-alive sessions
20612095
}
20622096

20632097
return true;

system/Language/en/Email.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@
1313

1414
// Email language settings
1515
return [
16-
'mustBeArray' => 'The email validation method must be passed an array.',
17-
'invalidAddress' => 'Invalid email address: "{0}"',
18-
'attachmentMissing' => 'Unable to locate the following email attachment: "{0}"',
19-
'attachmentUnreadable' => 'Unable to open this attachment: "{0}"',
20-
'noFrom' => 'Cannot send mail with no "From" header.',
21-
'noRecipients' => 'You must include recipients: To, Cc, or Bcc',
22-
'sendFailurePHPMail' => 'Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.',
23-
'sendFailureSendmail' => 'Unable to send email using Sendmail. Your server might not be configured to send mail using this method.',
24-
'sendFailureSmtp' => 'Unable to send email using SMTP. Your server might not be configured to send mail using this method.',
25-
'sent' => 'Your message has been successfully sent using the following protocol: {0}',
26-
'noSocket' => 'Unable to open a socket to Sendmail. Please check settings.',
27-
'noHostname' => 'You did not specify a SMTP hostname.',
28-
'SMTPError' => 'The following SMTP error was encountered: {0}',
29-
'noSMTPAuth' => 'Error: You must assign an SMTP username and password.',
30-
'failedSMTPLogin' => 'Failed to send AUTH LOGIN command. Error: {0}',
31-
'SMTPAuthUsername' => 'Failed to authenticate username. Error: {0}',
32-
'SMTPAuthPassword' => 'Failed to authenticate password. Error: {0}',
33-
'SMTPDataFailure' => 'Unable to send data: {0}',
34-
'exitStatus' => 'Exit status code: {0}',
16+
'mustBeArray' => 'The email validation method must be passed an array.',
17+
'invalidAddress' => 'Invalid email address: "{0}"',
18+
'attachmentMissing' => 'Unable to locate the following email attachment: "{0}"',
19+
'attachmentUnreadable' => 'Unable to open this attachment: "{0}"',
20+
'noFrom' => 'Cannot send mail with no "From" header.',
21+
'noRecipients' => 'You must include recipients: To, Cc, or Bcc',
22+
'sendFailurePHPMail' => 'Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.',
23+
'sendFailureSendmail' => 'Unable to send email using Sendmail. Your server might not be configured to send mail using this method.',
24+
'sendFailureSmtp' => 'Unable to send email using SMTP. Your server might not be configured to send mail using this method.',
25+
'sent' => 'Your message has been successfully sent using the following protocol: {0}',
26+
'noSocket' => 'Unable to open a socket to Sendmail. Please check settings.',
27+
'noHostname' => 'You did not specify a SMTP hostname.',
28+
'SMTPError' => 'The following SMTP error was encountered: {0}',
29+
'noSMTPAuth' => 'Error: You must assign an SMTP username and password.',
30+
'invalidSMTPAuthMethod' => 'Error: SMTP authorization method "{0}" is not supported in codeigniter, set either "login" or "plain" authorization method',
31+
'failureSMTPAuthMethod' => 'Unable to initiate AUTH command. Your server might not be configured to use AUTH {0} authentication method.',
32+
'SMTPAuthCredentials' => 'Failed to authenticate user credentials. Error: {0}',
33+
'SMTPAuthUsername' => 'Failed to authenticate username. Error: {0}',
34+
'SMTPAuthPassword' => 'Failed to authenticate password. Error: {0}',
35+
'SMTPDataFailure' => 'Unable to send data: {0}',
36+
'exitStatus' => 'Exit status code: {0}',
37+
// @deprecated
38+
'failedSMTPLogin' => 'Failed to send AUTH LOGIN command. Error: {0}',
3539
];

user_guide_src/source/libraries/email.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Here is a basic example demonstrating how you might send email:
3939
Setting Email Preferences
4040
=========================
4141

42-
There are 21 different preferences available to tailor how your email
42+
There are 22 different preferences available to tailor how your email
4343
messages are sent. You can either set them manually as described here,
4444
or automatically via preferences stored in your config file, described
4545
in `Email Preferences`_.
@@ -120,6 +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.
123124
**SMTPUser** SMTP Username.
124125
**SMTPPass** SMTP Password.
125126
**SMTPPort** 25 SMTP Port. (If set to ``465``, TLS will be used for the connection

0 commit comments

Comments
 (0)