Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Add STARTTLS support #52

Closed
swiftmailer opened this issue Sep 2, 2011 · 8 comments
Closed

Add STARTTLS support #52

swiftmailer opened this issue Sep 2, 2011 · 8 comments

Comments

@swiftmailer
Copy link
Collaborator

Depends on #32.

Use the ESMTP extension API in Swift Mailer to add support for the use of STARTTLS.

This should be easy to achieve by performing a stream_enable_crypto() call on the SMTP socket immediately after sending the STARTTLS command.

Full RFC:

http://www.ietf.org/rfc/rfc3207.txt

Original creation date: 2009-03-09T14:29:05Z
Original reporter: xdecock
Original ticket: http://swiftmailer.lighthouseapp.com/projects/21527/tickets/67

@swiftmailer
Copy link
Collaborator Author

FWIW, I was able to get STARTTLS working in Swift Mailer v4.0.6 with the following changes:

Index: lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/EsmtpTransport.php

--- lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/EsmtpTransport.php(revision 775)
+++ lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/EsmtpTransport.php(working copy)
@@ -52,7 +52,8 @@
'port' => 25,
'timeout' => 30,
'blocking' => 1,

  • 'type' => Swift_Transport_IoBuffer::TYPE_SOCKET
  • 'type' => Swift_Transport_IoBuffer::TYPE_SOCKET,
  • 'starttls' => false
    );

/**
@@ -131,7 +132,15 @@
*/
public function setEncryption($enc)
{

  • $this->_params['protocol'] = $enc;
  • if ($enc == 'starttls') {
  •  $this->_params['protocol'] = 'tcp';
    
  •  $this->_params['starttls'] = true;
    
  • }
  • else
  • {
  •  $this->_params['protocol'] = $enc;
    
  •  $this->_params['starttls'] = false;
    
  • }
    return $this;
    }

@@ -141,7 +150,7 @@
*/
public function getEncryption()
{

  • return $this->_params['protocol'];
  • return $this->_params['starttls'] ? 'starttls' : $this->_params['protocol'];
    }

/**
@@ -244,7 +253,19 @@
{
return parent::_doHeloCommand();

}

  • if ($this->_params['starttls']) {
  •  $this->executeCommand("STARTTLS\r\n", array(220));
    
  •  if (!$this->_buffer->startTLS()) {
    
  •    throw new Swift_TransportException('Failed to enable TLS encryption');
    
  •  }
    
  •  $response = $this->executeCommand(
    
  •    sprintf("EHLO %s\r\n", $this->_domain), array(250)
    
  •    );
    
  • }

$this->_capabilities = $this->_getCapabilities($response);
$this->_setHandlerParams();
foreach ($this->_getActiveHandlers() as $handler)

Index: lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/StreamBuffer.php

--- lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/StreamBuffer.php(revision 775)
+++ lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/StreamBuffer.php(working copy)
@@ -101,6 +101,23 @@
}

/**

  • * Start TLS encryption on the buffer.
  • */
  • public function startTLS()
  • {
  • if (isset($this->_stream))
  • {
  •  return stream_socket_enable_crypto(
    
  •    $this->_stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT
    
  •    );
    
  • }
  • else
  • {
  •     return false;
    
  • }
  • }
  • /**
  • Perform any shutdown logic needed.
    */
    public function terminate()

Original creation date: 2010-06-04T17:11:28Z
Original reporter: ADragoo

@swiftmailer
Copy link
Collaborator Author

I guess I should have previewed and read the formatting tips before submitting my last comment.

FWIW, I was able to get STARTTLS working in Swift Mailer v4.0.6 with the following changes:

@@@

Index: lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/EsmtpTransport.php

--- lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/EsmtpTransport.php
(revision 775)
+++ lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/EsmtpTransport.php
(working copy)
@@ -52,7 +52,8 @@
'port' => 25,
'timeout' => 30,
'blocking' => 1,

  • 'type' => Swift_Transport_IoBuffer::TYPE_SOCKET
  • 'type' => Swift_Transport_IoBuffer::TYPE_SOCKET,
  • 'starttls' => false
    );

/**
@@ -131,7 +132,15 @@
*/
public function setEncryption($enc)
{

  • $this->_params['protocol'] = $enc;
  • if ($enc == 'starttls') {
  •  $this->_params['protocol'] = 'tcp';
    
  •  $this->_params['starttls'] = true;
    
  • }
  • else
  • {
  •  $this->_params['protocol'] = $enc;
    
  •  $this->_params['starttls'] = false;
    
  • }
    return $this;
    }

@@ -141,7 +150,7 @@
*/
public function getEncryption()
{

  • return $this->_params['protocol'];
  • return $this->_params['starttls'] ? 'starttls' : $this->_params['protocol'];
    }

/**
@@ -244,7 +253,19 @@
{
return parent::_doHeloCommand();

}

  • if ($this->_params['starttls']) {
  •  $this->executeCommand("STARTTLS\r\n", array(220));
    
  •  if (!$this->_buffer->startTLS()) {
    
  •    throw new Swift_TransportException('Failed to enable TLS encryption');
    
  •  }
    
  •  $response = $this->executeCommand(
    
  •    sprintf("EHLO %s\r\n", $this->_domain), array(250)
    
  •    );
    
  • }

$this->_capabilities = $this->_getCapabilities($response);
$this->_setHandlerParams();
foreach ($this->_getActiveHandlers() as $handler)

Index: lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/StreamBuffer.php

--- lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/StreamBuffer.php
(revision 775)
+++ lib/thirdparty/Swift_Mailer/lib/classes/Swift/Transport/StreamBuffer.php
(working copy)
@@ -101,6 +101,23 @@
}

/**

  • * Start TLS encryption on the buffer.
  • */
  • public function startTLS()
  • {
  • if (isset($this->_stream))
  • {
  •  return stream_socket_enable_crypto(
    
  •    $this->_stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT
    
  •    );
    
  • }
  • else
  • {
  •     return false;
    
  •   }
    
  • }
  • /**
  • Perform any shutdown logic needed.
    */
    public function terminate()
    @@@

Original creation date: 2010-06-04T17:21:08Z
Original reporter: ADragoo

@swiftmailer
Copy link
Collaborator Author

These changes fixed the issue with sending email through Google Mail servers. Highly recommend adding the changes to the distribution.

Original creation date: 2010-08-08T14:16:50Z
Original reporter: Trinity13

@swiftmailer
Copy link
Collaborator Author

I can confirm that the above patch adds STARTTLS support to SwiftMailer and it works perfectly fine with my own mail server here.

Original creation date: 2010-10-27T18:49:33Z
Original reporter: AlexC

@swiftmailer
Copy link
Collaborator Author

I'm trying to get starttls working with Swiftmailer v4.0.6 as you have, but it is not working for me.

I take it you can run the script above? I'm currently making the alterations by hand so can someone give me a clue as to the type of script it is so I can run it?

Thanks!

Original creation date: 2010-12-26T02:53:11Z
Original reporter: Richard

@swiftmailer
Copy link
Collaborator Author

It's a diff. You can't "run" a diff, since all it does is describe changes (lines starting with "+" are additions, lines starting with "-" are deletions).

patch EsmtpTransport.php < EsmtpTransport.php.diff

(No idea how to do this on Windows, but this is the Linux/Mac/UNIX way to do it)

Original creation date: 2010-12-26T03:58:51Z
Original reporter: Chris Corbyn

@swiftmailer
Copy link
Collaborator Author

Thanks, the patch worked fine for me.

I've added a zip containing a patched version of swift mailer v4.0.6 for anyone not on Linux or unable to apply the patch... Swift Mailer with STARTTLS

Original creation date: 2010-12-26T21:49:39Z
Original reporter: Richard

@swiftmailer
Copy link
Collaborator Author

Added the patch in merge queue.

Original creation date: 2011-03-12T14:43:17Z
Original reporter: xdecock

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant