title: Getting certificate from smtp in php
body: <p>I have a php function that can get the certificate from https-connections, is it possible to extend it to also be able to use on smtp-starttls?</p>
  
  <p>Can I open it as a "tcp://", and after sending the "STARTTLS" command, switch it to a "ssl://"?</p>
  
  <pre><code>function ssl_fetch_cert($domain, $port = 443)
  {
      $url = "ssl://{$domain}:{$port}";
      $connection_context_option['ssl']['capture_peer_cert'] = TRUE;
      $connection_context = stream_context_create($connection_context_option);
      $connection_client = stream_socket_client($url, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $connection_context);
      $connection_info = stream_context_get_params($connection_client);
      // $sha256 = openssl_x509_fingerprint($connection_info['options']['ssl']['peer_certificate'], 'sha256');
      return $connection_info['options']['ssl']['peer_certificate'];
  }
  </code></pre>