public function getErrorDesc() {
return $this->errorDesc;
}
+
+ /**
+ * Switches TLS support for this connection.
+ * Usually used in combination with 'STARTTLS'
+ *
+ * @param boolean $enable Whether TLS support should be enabled
+ * @return boolean True on success, false otherwise
+ */
+ public function setTLS($enable) {
+ if (!$this->hasTLSSupport()) return false;
+
+ return stream_socket_enable_crypto($this->resource, $enable, STREAM_CRYPTO_METHOD_TLS_CLIENT);
+ }
+
+ /**
+ * Returns whether TLS support is available.
+ *
+ * @return boolean
+ */
+ public function hasTLSSupport() {
+ return function_exists('stream_socket_enable_crypto');
+ }
}
/**
* Sends a Mail with a connection to a smtp server.
*
- * @author Alexander Ebert
+ * @author Tim Duesterhus, Alexander Ebert
* @copyright 2001-2014 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @package com.woltlab.wcf
// send ehlo
$this->write('EHLO '.$host);
- $this->getSMTPStatus();
+ $extensions = explode(Mail::$lineEnding, $this->read());
+ $this->getSMTPStatus(array_shift($extensions));
if ($this->statusCode == 250) {
+ $extensions = array_map(function($element) {
+ return strtolower(substr($element, 4));
+ }, $extensions);
+
+ if ($this->connection->hasTLSSupport() && in_array('starttls', $extensions)) {
+ $this->write('STARTTLS');
+ $this->getSMTPStatus();
+
+ if ($this->statusCode != 220) {
+ throw new SystemException($this->formatError("cannot enable STARTTLS, though '".MAIL_SMTP_HOST.":".MAIL_SMTP_PORT."' advertised it"));
+ }
+
+ if (!$this->connection->setTLS(true)) {
+ throw new SystemException('enabling TLS failed');
+ }
+
+ // repeat EHLO
+ $this->write('EHLO '.$host);
+ $extensions = explode(Mail::$lineEnding, $this->read());
+ $this->getSMTPStatus(array_shift($extensions));
+
+ if ($this->statusCode != 250) {
+ throw new SystemException($this->formatError("could not EHLO after enabling STARTTLS at '".MAIL_SMTP_HOST.":".MAIL_SMTP_PORT."'"));
+ }
+ }
+
// do authentication
if (MAIL_SMTP_USER != '' || MAIL_SMTP_PASSWORD != '') {
$this->auth();
$result .= $read;
if (substr($read, 3, 1) == " ") break;
}
+
return $result;
}