diff --git a/modules/system/models/MailSetting.php b/modules/system/models/MailSetting.php index 5be21a839..c211e27c4 100644 --- a/modules/system/models/MailSetting.php +++ b/modules/system/models/MailSetting.php @@ -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; + } + } + } } diff --git a/modules/system/models/mailsetting/fields.yaml b/modules/system/models/mailsetting/fields.yaml index d49851cba..44c6c9a6f 100644 --- a/modules/system/models/mailsetting/fields.yaml +++ b/modules/system/models/mailsetting/fields.yaml @@ -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