Automatically set SMTP port depending on mail encryption type.

This only occurs if the SMTP port field is using a standard port (25, 465, 587). Custom ports are not overwritten.

Fixes https://github.com/octobercms/october/issues/4755
This commit is contained in:
Ben Thomson 2020-08-31 11:21:50 +08:00
parent acd0616aaa
commit ce8c96b66f
No known key found for this signature in database
GPG Key ID: 8BDB18DD0909BE22
2 changed files with 29 additions and 0 deletions

View File

@ -145,4 +145,32 @@ class MailSetting extends Model
'ssl' => 'system::lang.mail.smtp_encryption_ssl',
];
}
/**
* Filter fields callback.
*
* We use this to automatically set the SMTP port to the encryption type's corresponding port, if it was originally
* using a default port.
*
* @param array $fields
* @param string|null $context
* @return void
*/
public function filterFields($fields, $context = null)
{
if (in_array($fields->smtp_port->value ?? 25, [25, 465, 587])) {
switch ($fields->smtp_encryption->value ?? '') {
case '':
default:
$fields->smtp_port->value = 25;
break;
case 'tls':
$fields->smtp_port->value = 587;
break;
case 'ssl':
$fields->smtp_port->value = 465;
break;
}
}
}
}

View File

@ -42,6 +42,7 @@ tabs:
label: system::lang.mail.smtp_port
tab: system::lang.mail.general
span: auto
dependsOn: smtp_encryption
trigger:
action: show
field: send_mode