Skip to content

Commit

Permalink
Establish STARTTLS connection without authentication (#17)
Browse files Browse the repository at this point in the history
For using smartHost, wich authenticates the client via his ip address, to establish the STARTTLS connection without sending credentials.

Co-authored-by: armin <armin.graefe@moredata.de>
Co-authored-by: Schengawegga <schengawegga@gmail.com>
  • Loading branch information
3 people authored Jul 14, 2022
1 parent 368c52f commit 66ac6d1
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions Mail/smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,7 +40,7 @@
* @package HTTP_Request
* @author Jon Parise <jon@php.net>
* @author Chuck Hagenbuch <chuck@horde.org>
* @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/
Expand Down Expand Up @@ -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?
*
Expand Down Expand Up @@ -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.
Expand All @@ -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'];
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 66ac6d1

Please sign in to comment.