diff --git a/Mail/smtp.php b/Mail/smtp.php index 5556355..3cf7497 100644 --- a/Mail/smtp.php +++ b/Mail/smtp.php @@ -6,7 +6,7 @@ * * LICENSE: * - * Copyright (c) 2010-2017, Chuck Hagenbuch & Jon Parise + * Copyright (c) 2010-2021, Chuck Hagenbuch & Jon Parise * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -40,7 +40,7 @@ * @package HTTP_Request * @author Jon Parise * @author Chuck Hagenbuch - * @copyright 2010-2017 Chuck Hagenbuch + * @copyright 2010-2021 Chuck Hagenbuch * @license http://opensource.org/licenses/BSD-3-Clause New BSD License * @version CVS: $Id$ * @link http://pear.php.net/package/Mail/ @@ -105,6 +105,25 @@ class Mail_smtp extends Mail { */ var $port = 25; + /** + * Should STARTTLS connection be used? + * + * This value may be set to true or false. + * + * If the value is set to true, the Net_SMTP package will attempt to use + * a STARTTLS encrypted connection. + * + * If the value is set to false, the Net_SMTP package will avoid + * a STARTTLS encrypted connection. + * + * NULL indicates only STARTTLS if $auth is set. + * + * PEAR/Net_SMTP >= 1.10.0 required. + * + * @var boolean + */ + var $starttls = null; + /** * Should SMTP authentication be used? * @@ -185,7 +204,8 @@ class Mail_smtp extends Mail { * passed in. It looks for the following parameters: * host The server to connect to. Defaults to localhost. * port The port to connect to. Defaults to 25. - * auth SMTP authentication. Defaults to none. + * auth SMTP authentication. Defaults to none. + * starttls Should STARTTLS connection be used? No default. PEAR/Net_SMTP >= 1.10.0 required. * username The username to use for SMTP auth. No default. * password The password to use for SMTP auth. No default. * localhost The local hostname / domain. Defaults to localhost. @@ -207,6 +227,7 @@ public function __construct($params) if (isset($params['host'])) $this->host = $params['host']; if (isset($params['port'])) $this->port = $params['port']; if (isset($params['auth'])) $this->auth = $params['auth']; + if (isset($params['starttls'])) $this->starttls = $params['starttls']; if (isset($params['username'])) $this->username = $params['username']; if (isset($params['password'])) $this->password = $params['password']; if (isset($params['localhost'])) $this->localhost = $params['localhost']; @@ -392,16 +413,29 @@ public function getSMTPObject() /* Attempt to authenticate if authentication has been enabled. */ if ($this->auth) { $method = is_string($this->auth) ? $this->auth : ''; + + $tls = $this->starttls === false ? false : true; if (PEAR::isError($res = $this->_smtp->auth($this->username, $this->password, - $method))) { + $method, + $tls))) { $error = $this->_error("$method authentication failure", $res); $this->_smtp->rset(); return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_AUTH); } } + + /* Attempt to establish a TLS encrypted connection. PEAR/Net_SMTP >= 1.10.0 required. */ + if ($this->starttls && !$this->auth) { + $starttls = $this->_smtp->starttls(); + if (PEAR::isError($starttls)) { + return PEAR::raiseError($starttls); + } elseif ($starttls === false) { + return PEAR::raiseError('STARTTLS failed'); + } + } return $this->_smtp; }