From ce8c96b66fa6da6d034fda25926af44977505945 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Mon, 31 Aug 2020 11:21:50 +0800 Subject: [PATCH] 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 --- modules/system/models/MailSetting.php | 28 +++++++++++++++++++ modules/system/models/mailsetting/fields.yaml | 1 + 2 files changed, 29 insertions(+) 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