diff --git a/changelog.md b/changelog.md index 06ec7d84..7c7ff8c8 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,7 @@ * Chinese language update (Thanks to @binaryoung) * Allow custom Mailer types (thanks to @michield) * Cope with spaces around SMTP host specs +* Fix processing of multiple hosts in connect string ## Version 5.2.7 (September 12th 2013) * Add Ukranian translation from @Krezalis diff --git a/class.phpmailer.php b/class.phpmailer.php index fa0f9369..8770824c 100644 --- a/class.phpmailer.php +++ b/class.phpmailer.php @@ -1242,18 +1242,36 @@ class PHPMailer $this->smtp->setDebugLevel($this->SMTPDebug); $this->smtp->setDebugOutput($this->Debugoutput); $this->smtp->setVerp($this->do_verp); - $tls = ($this->SMTPSecure == 'tls'); - $ssl = ($this->SMTPSecure == 'ssl'); $hosts = explode(';', $this->Host); $lastexception = null; foreach ($hosts as $hostentry) { - $host = trim($hostentry); - $port = $this->Port; - if (strpos($host, ':') !== false) { - list($host, $port) = explode(':', $host); + $hostinfo = array(); + if (!preg_match('/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*):?([0-9]*)$/', trim($hostentry), $hostinfo)) { + //Not a valid host entry + continue; } - if ($this->smtp->connect(($ssl ? 'ssl://' : '') . $host, $port, $this->Timeout, $options)) { + //$hostinfo[2]: optional ssl or tls prefix + //$hostinfo[3]: the hostname + //$hostinfo[4]: optional port number + //The host string prefix can temporarily override the current setting for SMTPSecure + //If it's not specified, the default value is used + $prefix = ''; + $tls = ($this->SMTPSecure == 'tls'); + if ($hostinfo[2] == 'ssl') { + $prefix = 'ssl://'; + $tls = false; //Can't have SSL and TLS at once + } elseif ($hostinfo[2] == 'tls') { + $tls = true; + //tls doesn't use a prefix + } + $host = $hostinfo[3]; + $port = $this->Port; + $tport = (integer)$hostinfo[4]; + if ($tport > 0 and $tport < 65536) { + $port = $tport; + } + if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) { try { if ($this->Helo) { $hello = $this->Helo; diff --git a/test/phpmailerTest.php b/test/phpmailerTest.php index 9b24b548..c6fb2708 100644 --- a/test/phpmailerTest.php +++ b/test/phpmailerTest.php @@ -1033,7 +1033,7 @@ EOT; { $this->assertTrue($this->Mail->smtpConnect(), 'SMTP single connect failed'); $this->Mail->smtpClose(); - $this->Mail->Host = "localhost:12345;10.10.10.10:54321"; + $this->Mail->Host = "ssl://localhost:12345;tls://localhost:587;10.10.10.10:54321;localhost:12345;10.10.10.10"; $this->assertFalse($this->Mail->smtpConnect(), 'SMTP bad multi-connect succeeded'); $this->Mail->smtpClose(); $this->Mail->Host = "localhost:12345;10.10.10.10:54321;" . $_REQUEST['mail_host'];