title: Sending email Via SMTP with STARTTLS command with cakePHP
body: <p>I have been asked to use an external SMTP server with the following instructions:</p>
  
  <p>"We expect you to connect to port 25 in cleartext, then issue a STARTTLS to commence TLS/SSL. Then log in"</p>
  
  <p>Is this possible and if so, how should I do it?  As I understand it, this is different to setting the TLS to true in the smtp connection array, is that right?</p>
  
  <p>UPDATE
  Plodding towards victory here.  Looking at the code in SmtpTransport.php I can see it matches up with the spec linked in the comments (obviously) but the block 102-107 seems to set the host to either client or localhost - I'm setting the remote ip in the host configuration. Am i doing it wrong?</p>
  
  <p>Cakes code:</p>
  
  <pre><code>protected function _connect() {
      $this-&gt;_generateSocket();
      if (!$this-&gt;_socket-&gt;connect()) {
          throw new SocketException(__d('cake_dev', 'Unable to connect to SMTP server.'));
      }
      $this-&gt;_smtpSend(null, '220');
  
      if (isset($this-&gt;_config['client'])) {
          $host = $this-&gt;_config['client'];
      } elseif ($httpHost = env('HTTP_HOST')) {
          list($host) = explode(':', $httpHost);
      } else {
          $host = 'localhost';
      }
  
      try {
          $this-&gt;_smtpSend("EHLO {$host}", '250');
          if ($this-&gt;_config['tls']) {
              $this-&gt;_smtpSend("STARTTLS", '220');
              $this-&gt;_socket-&gt;enableCrypto('tls');
              $this-&gt;_smtpSend("EHLO {$host}", '250');
          }
      } catch (SocketException $e) {
          if ($this-&gt;_config['tls']) {
              throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.'));
          }
          try {
              $this-&gt;_smtpSend("HELO {$host}", '250');
          } catch (SocketException $e2) {
              throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection.'));
          }
      }
  }
  </code></pre>
  
  <p>My Email Config:</p>
  
  <pre><code>    public $smtp = array(
      'transport' =&gt; 'Smtp',
      'from' =&gt; array('me@example.com' =&gt; 'my name'),
      'host' =&gt; 'secretipaddress',
      'port' =&gt; 25,
      'timeout' =&gt; 30,
      'username' =&gt; 'me@example.com',
      'password' =&gt; 'secretpassword',
      'client' =&gt; null,
      'log' =&gt; true,
      'tls' =&gt; true
  );
  </code></pre>