From 34a0af89642121585bc7be4df520fd6b4ccdb078 Mon Sep 17 00:00:00 2001 From: johndoh Date: Sat, 16 May 2020 13:05:28 +0100 Subject: [PATCH] Allow array in smtp_host config (#7296) --- config/config.inc.php.sample | 2 ++ config/defaults.inc.php | 2 ++ installer/test.php | 17 +++++++++++++--- program/include/rcmail_install.php | 29 +++++++++++++++++++++------- program/lib/Roundcube/rcube_smtp.php | 16 ++++++++++++++- 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/config/config.inc.php.sample b/config/config.inc.php.sample index eb0f0583e..99a54c495 100644 --- a/config/config.inc.php.sample +++ b/config/config.inc.php.sample @@ -50,6 +50,8 @@ $config['default_host'] = 'localhost'; // %d - domain (http hostname $_SERVER['HTTP_HOST'] without the first part) // %z - IMAP domain (IMAP hostname without the first part) // For example %n = mail.domain.tld, %t = domain.tld +// To specify differnt SMTP servers for different IMAP hosts provide an array +// of IMAP host (no prefix or port) and SMTP server e.g. array('imap.example.com' => 'smtp.example.net') $config['smtp_server'] = 'localhost'; // SMTP port. Use 25 for cleartext, 465 for Implicit TLS, or 587 for STARTTLS (default) diff --git a/config/defaults.inc.php b/config/defaults.inc.php index a74893190..40ccff7ab 100644 --- a/config/defaults.inc.php +++ b/config/defaults.inc.php @@ -267,6 +267,8 @@ $config['messages_cache_threshold'] = 50; // %d - domain (http hostname $_SERVER['HTTP_HOST'] without the first part) // %z - IMAP domain (IMAP hostname without the first part) // For example %n = mail.domain.tld, %t = domain.tld +// To specify differnt SMTP servers for different IMAP hosts provide an array +// of IMAP host (no prefix or port) and SMTP server e.g. array('imap.example.com' => 'smtp.example.net') $config['smtp_server'] = 'localhost'; // SMTP port. Use 25 for cleartext, 465 for Implicit TLS, or 587 for STARTTLS (default) diff --git a/installer/test.php b/installer/test.php index 132fe1ef0..c71f0350f 100644 --- a/installer/test.php +++ b/installer/test.php @@ -263,6 +263,15 @@ else { echo "
"; } +$smtp_hosts = $RCI->get_hostlist('smtp_server'); +if (!empty($smtp_hosts)) { + $smtp_host_field = new html_select(array('name' => '_smtp_host', 'id' => 'smtp_server')); + $smtp_host_field->add($smtp_hosts); +} +else { + $smtp_host_field = new html_inputfield(array('name' => '_smtp_host', 'id' => 'smtp_server')); +} + $user = $RCI->getprop('smtp_user', '(none)'); $pass = $RCI->getprop('smtp_pass', '(none)'); @@ -284,7 +293,7 @@ if ($pass == '%p') { - getprop('smtp_server', 'localhost')); ?> + show($_POST['_smtp_host']); ?> @@ -311,6 +320,9 @@ if (isset($_POST['sendmail'])) { echo '

Trying to send email...
'; + $smtp_host = trim($_POST['_smtp_host']); + $smtp_port = $RCI->getprop('smtp_port'); + $from = rcube_utils::idn_to_ascii(trim($_POST['_from'])); $to = rcube_utils::idn_to_ascii(trim($_POST['_to'])); @@ -340,8 +352,7 @@ if (isset($_POST['sendmail'])) { $head = $mail_object->txtHeaders($send_headers); $SMTP = new rcube_smtp(); - $SMTP->connect(rcube_utils::parse_host($RCI->getprop('smtp_server')), - $RCI->getprop('smtp_port'), $CONFIG['smtp_user'], $CONFIG['smtp_pass']); + $SMTP->connect($smtp_host, $smtp_port, $CONFIG['smtp_user'], $CONFIG['smtp_pass']); $status = $SMTP->send_mail($headers['From'], $headers['To'], $head, $body); $smtp_response = $SMTP->get_response(); diff --git a/program/include/rcmail_install.php b/program/include/rcmail_install.php index 8d80fb113..e400b000d 100644 --- a/program/include/rcmail_install.php +++ b/program/include/rcmail_install.php @@ -600,18 +600,33 @@ class rcmail_install } /** - * Return a list with all imap hosts configured + * Return a list with all imap/smtp hosts configured * - * @return array Clean list with imap hosts + * @return array Clean list with imap/smtp hosts */ - public function get_hostlist() + public function get_hostlist($prop = 'default_host') { - $default_hosts = (array) $this->getprop('default_host'); - $out = array(); + $hosts = (array) $this->getprop($prop); + $out = array(); + $imap_host = ''; - foreach ($default_hosts as $key => $name) { + if ($prop == 'smtp_server') { + // Set the imap host name for the %h macro + $default_hosts = $this->get_hostlist(); + $imap_host = !empty($default_hosts) ? $default_hosts[0] : ''; + } + + foreach ($hosts as $key => $name) { if (!empty($name)) { - $out[] = rcube_utils::parse_host(is_numeric($key) ? $name : $key); + if ($prop == 'smtp_server') { + // SMTP host array uses `IMAP host => SMTP host` format + $host = $name; + } + else { + $host = is_numeric($key) ? $name : $key; + } + + $out[] = rcube_utils::parse_host($host, $imap_host); } } diff --git a/program/lib/Roundcube/rcube_smtp.php b/program/lib/Roundcube/rcube_smtp.php index d78ea4a2a..eb433d4e4 100644 --- a/program/lib/Roundcube/rcube_smtp.php +++ b/program/lib/Roundcube/rcube_smtp.php @@ -57,9 +57,23 @@ class rcube_smtp // reset error/response var $this->error = $this->response = null; + if (!$host) { + $host = $rcube->config->get('smtp_server'); + if (is_array($host)) { + if (array_key_exists($_SESSION['storage_host'], $host)) { + $host = $host[$_SESSION['storage_host']]; + } + else { + $this->response[] = "Connection failed: No SMTP server found for IMAP host " . $_SESSION['storage_host']; + $this->error = array('label' => 'smtpconnerror', 'vars' => array('code' => '500')); + return false; + } + } + } + // let plugins alter smtp connection config $CONFIG = $rcube->plugins->exec_hook('smtp_connect', array( - 'smtp_server' => $host ?: $rcube->config->get('smtp_server'), + 'smtp_server' => $host, 'smtp_port' => $port ?: $rcube->config->get('smtp_port', 587), 'smtp_user' => $user !== null ? $user : $rcube->config->get('smtp_user', '%u'), 'smtp_pass' => $pass !== null ? $pass : $rcube->config->get('smtp_pass', '%p'),