Skip to content

Commit 40656ce

Browse files
committed
refactor: improve authorization validation flow and email error response messages
1 parent 23b58b3 commit 40656ce

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

system/Email/Email.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,16 +2019,28 @@ protected function SMTPAuthenticate()
20192019
return false;
20202020
}
20212021

2022-
// send initial 'handshake' command
2022+
// normalize in case user entered capital words LOGIN/PLAIN
2023+
$this->SMTPAuthMethod = strtolower($this->SMTPAuthMethod);
2024+
2025+
// Validate supported authentication methods
2026+
$validMethods = ['login', 'plain'];
2027+
if (! in_array($this->SMTPAuthMethod, $validMethods, true)) {
2028+
$this->setErrorMessage(lang('Email.invalidSMTPAuthMethod', [$this->SMTPAuthMethod]));
2029+
2030+
return false;
2031+
}
2032+
2033+
// send initial 'AUTH' command
20232034
$this->sendData('AUTH ' . strtoupper($this->SMTPAuthMethod));
20242035
$reply = $this->getSMTPData();
20252036

20262037
if (str_starts_with($reply, '503')) { // Already authenticated
20272038
return true;
20282039
}
20292040

2041+
// if 'AUTH' command is unsuported by the server
20302042
if (! str_starts_with($reply, '334')) {
2031-
$this->setErrorMessage(lang('Email.failedSMTPLogin', [$reply]));
2043+
$this->setErrorMessage(lang('Email.failureSMTPAuthMethod', [strtoupper($this->SMTPAuthMethod)]));
20322044

20332045
return false;
20342046
}
@@ -2053,16 +2065,11 @@ protected function SMTPAuthenticate()
20532065

20542066
$this->sendData(base64_encode($authString));
20552067
break;
2056-
2057-
default:
2058-
$this->setErrorMessage(lang('Email.noSMTPAuthMethod'));
2059-
2060-
return false;
20612068
}
20622069

20632070
$reply = $this->getSMTPData();
20642071
if (! str_starts_with($reply, '235')) { // Authentication failed
2065-
$errorMessage = $this->SMTPAuthMethod === 'plain' ? 'Email.failedSMTPLogin' : 'Email.SMTPAuthPassword';
2072+
$errorMessage = $this->SMTPAuthMethod === 'plain' ? 'Email.SMTPAuthCredentials' : 'Email.SMTPAuthPassword';
20662073

20672074
$this->setErrorMessage(lang($errorMessage, [$reply]));
20682075

system/Language/en/Email.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,25 @@
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-
'noSMTPAuthMethod' => 'Error: you must assign an SMTP authentication method ("LOGIN" or "PLAIN")',
31-
'failedSMTPLogin' => 'Failed to send AUTH LOGIN command. Error: {0}',
32-
'SMTPAuthUsername' => 'Failed to authenticate username. Error: {0}',
33-
'SMTPAuthPassword' => 'Failed to authenticate password. Error: {0}',
34-
'SMTPDataFailure' => 'Unable to send data: {0}',
35-
'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}',
3637
];

0 commit comments

Comments
 (0)