Skip to content

Try to update smtp->connect() function to enable self-signed server certificates, disable peer (name) validation #30

@Qixuga

Description

@Qixuga

I use your mailer within dokuwiki and the smtp-modul:

Some features were missing so i tried to update the SMTP->connect() function to enable support for

  • selbst signed ssl/tls server certificates
  • disable peer validation
  • disable peer name validation
    and other options, but currently it won't work - maybe someone has same problem like me (small DMZ-Mail server with self-signed certificate)
// \Mailer\src\Mailer\SMTP.php:196
protected function connect()
{
	$this->logger && $this->logger->debug("Connecting to {$this->host} at {$this->port}");
	/*
	$host = ($this->secure == 'ssl') ? 'ssl://' . $this->host : $this->host;
	$this->smtp = @fsockopen($host, $this->port);
	//set block mode
	//    stream_set_blocking($this->smtp, 1);
	*/
	switch ( $this->secure ) {
		case 'ssl' :
			$host = 'ssl://' . $this->host;
			break;
		case 'tls' :
			$host = 'tls://' . $this->host;
			break;
		default:
			$host = $this->host;
	}
	$hostport = $host.($this->port ? ':' . $this->port : '' );
	$socket_options = array(
		'ssl' => array(
			'verify_peer' => false,
			'verify_peer_name' => false,
			'allow_self_signed' => true,
		)
	);
	$socket_context = stream_context_create($socket_options);
	$errno = '';
	$errstr = '';
	$this->smtp = stream_socket_client($hostport, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $socket_context);
	if (!$this->smtp){
		//throw new SMTPException("Could not open SMTP Port.");
		throw new SMTPException("Could not open SMTP Host/Port: ".$hostport.". Error:" . $errno . " - " . $errstr . ', '. var_export ( $socket_context, true) . ', ' . var_export($this->smtp, true) );
	}
	$code = $this->getCode();
	if ($code !== '220'){
		throw new CodeException('220', $code, array_pop($this->resultStack));
	}
	return $this;
}

i get the following error:

There was an unexpected problem communicating with SMTP: Could not open SMTP Host/Port: tls://:Error:0 - , NULL, false

Documentation stream_socket_client:

If the value returned in error_code is 0 and the function returned false, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket.

But i don't know - if i use openssl-cli the connection might no problem:

openssl s_client -connect <myhost>:<post> -starttls smtp

Can anyone help?
Thx!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions