Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #117 from Slamdunk/set-complete-quit
Browse files Browse the repository at this point in the history
Protocol\Smtp: toggle QUIT>221 command at disconnect

Conflicts:
	test/Protocol/SmtpTest.php
  • Loading branch information
weierophinney committed Feb 14, 2017
2 parents dc629d3 + 250bf46 commit 1b5b2a8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 23 deletions.
38 changes: 36 additions & 2 deletions src/Protocol/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ class Smtp extends AbstractProtocol
*/
protected $data = null;

/**
* Whether or not send QUIT command
*
* @var bool
*/
protected $useCompleteQuit = true;

/**
* Constructor.
*
Expand Down Expand Up @@ -128,6 +135,10 @@ public function __construct($host = '127.0.0.1', $port = null, array $config = n
}
}

if (array_key_exists('use_complete_quit', $config)) {
$this->setUseCompleteQuit($config['use_complete_quit']);
}

// If no port has been specified then check the master PHP ini file. Defaults to 25 if the ini setting is null.
if ($port === null) {
if (($port = ini_get('smtp_port')) == '') {
Expand All @@ -138,6 +149,25 @@ public function __construct($host = '127.0.0.1', $port = null, array $config = n
parent::__construct($host, $port);
}

/**
* Set whether or not send QUIT command
*
* @param int $useCompleteQuit use complete quit
*/
public function setUseCompleteQuit($useCompleteQuit)
{
return $this->useCompleteQuit = (bool) $useCompleteQuit;
}

/**
* Whether or not send QUIT command
*
* @return bool
*/
public function useCompleteQuit()
{
return $this->useCompleteQuit;
}

/**
* Connect to the server with the parameters given in the constructor.
Expand Down Expand Up @@ -350,8 +380,12 @@ public function quit()
{
if ($this->sess) {
$this->auth = false;
$this->_send('QUIT');
$this->_expect(221, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2

if ($this->useCompleteQuit()) {
$this->_send('QUIT');
$this->_expect(221, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
}

$this->stopSession();
}
}
Expand Down
69 changes: 48 additions & 21 deletions test/Protocol/SmtpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ public function testSendMinimalMail()
{
$headers = new Headers();
$headers->addHeaderLine('Date', 'Sun, 10 Jun 2012 20:07:24 +0200');

$message = new Message();
$message
->setHeaders($headers)
->setSender('ralph.schindler@zend.com', 'Ralph Schindler')
->setBody('testSendMailWithoutMinimalHeaders')
->addTo('zf-devteam@zend.com', 'ZF DevTeam')
;
$message->setHeaders($headers);
$message->setSender('ralph.schindler@zend.com', 'Ralph Schindler');
$message->setBody('testSendMailWithoutMinimalHeaders');
$message->addTo('zf-devteam@zend.com', 'ZF DevTeam');

$expectedMessage = "EHLO localhost\r\n"
. "MAIL FROM:<ralph.schindler@zend.com>\r\n"
. "RCPT TO:<zf-devteam@zend.com>\r\n"
. "DATA\r\n"
. "Date: Sun, 10 Jun 2012 20:07:24 +0200\r\n"
. "Sender: Ralph Schindler <ralph.schindler@zend.com>\r\n"
. "To: ZF DevTeam <zf-devteam@zend.com>\r\n"
. "\r\n"
. "testSendMailWithoutMinimalHeaders\r\n"
. ".\r\n";
. "MAIL FROM:<ralph.schindler@zend.com>\r\n"
. "RCPT TO:<zf-devteam@zend.com>\r\n"
. "DATA\r\n"
. "Date: Sun, 10 Jun 2012 20:07:24 +0200\r\n"
. "Sender: Ralph Schindler <ralph.schindler@zend.com>\r\n"
. "To: ZF DevTeam <zf-devteam@zend.com>\r\n"
. "\r\n"
. "testSendMailWithoutMinimalHeaders\r\n"
. ".\r\n";

$this->transport->send($message);

Expand All @@ -63,13 +63,13 @@ public function testSendEscapedEmail()
{
$headers = new Headers();
$headers->addHeaderLine('Date', 'Sun, 10 Jun 2012 20:07:24 +0200');

$message = new Message();
$message
->setHeaders($headers)
->setSender('ralph.schindler@zend.com', 'Ralph Schindler')
->setBody("This is a test\n.")
->addTo('zf-devteam@zend.com', 'ZF DevTeam')
;
$message->setHeaders($headers);
$message->setSender('ralph.schindler@zend.com', 'Ralph Schindler');
$message->setBody("This is a test\n.");
$message->addTo('zf-devteam@zend.com', 'ZF DevTeam');

$expectedMessage = "EHLO localhost\r\n"
. "MAIL FROM:<ralph.schindler@zend.com>\r\n"
. "RCPT TO:<zf-devteam@zend.com>\r\n"
Expand Down Expand Up @@ -111,4 +111,31 @@ public function testConnectHasVerboseErrors()

$smtp->connect('nonexistentremote');
}

public function testCanAvoidQuitRequest()
{
$this->assertTrue($this->connection->useCompleteQuit(), 'Default behaviour must be BC');

$this->connection->resetLog();
$this->connection->connect();
$this->connection->helo();
$this->connection->disconnect();

$this->assertContains('QUIT', $this->connection->getLog());

$this->connection->setUseCompleteQuit(false);
$this->assertFalse($this->connection->useCompleteQuit());

$this->connection->resetLog();
$this->connection->connect();
$this->connection->helo();
$this->connection->disconnect();

$this->assertNotContains('QUIT', $this->connection->getLog());

$connection = new SmtpProtocolSpy([
'use_complete_quit' => false,
]);
$this->assertFalse($connection->useCompleteQuit());
}
}

0 comments on commit 1b5b2a8

Please sign in to comment.