Skip to content
Permalink
Browse files
SMTPClient
https://github.com/andreyknupp/SMTPClient

Einzige Änderung: EHLO und HELO vertauscht
  • Loading branch information
georf committed Dec 8, 2013
0 parents commit 469cfeee1f6c4bc39aaf07ae34f52af7649df0f6
Showing with 2,761 additions and 0 deletions.
  1. +126 −0 api/utils/net/SMTP/Client.php
  2. +30 −0 api/utils/net/SMTP/Client/AbstractCommand.php
  3. +151 −0 api/utils/net/SMTP/Client/AbstractConnection.php
  4. +139 −0 api/utils/net/SMTP/Client/AbstractConnectionState.php
  5. +41 −0 api/utils/net/SMTP/Client/Authentication.php
  6. +75 −0 api/utils/net/SMTP/Client/Authentication/AbstractAuthentication.php
  7. +43 −0 api/utils/net/SMTP/Client/Authentication/Login.php
  8. +39 −0 api/utils/net/SMTP/Client/Authentication/Plain.php
  9. +18 −0 api/utils/net/SMTP/Client/Command.php
  10. +50 −0 api/utils/net/SMTP/Client/Command/AUTHCommand.php
  11. +49 −0 api/utils/net/SMTP/Client/Command/DATACommand.php
  12. +28 −0 api/utils/net/SMTP/Client/Command/EHLOCommand.php
  13. +31 −0 api/utils/net/SMTP/Client/Command/HELLOCommand.php
  14. +27 −0 api/utils/net/SMTP/Client/Command/HELOCommand.php
  15. +42 −0 api/utils/net/SMTP/Client/Command/InputCommand.php
  16. +39 −0 api/utils/net/SMTP/Client/Command/MAILCommand.php
  17. +30 −0 api/utils/net/SMTP/Client/Command/QUITCommand.php
  18. +39 −0 api/utils/net/SMTP/Client/Command/RCPTCommand.php
  19. +36 −0 api/utils/net/SMTP/Client/Command/STARTTLSCommand.php
  20. +24 −0 api/utils/net/SMTP/Client/CommandInvoker.php
  21. +60 −0 api/utils/net/SMTP/Client/Connection.php
  22. +26 −0 api/utils/net/SMTP/Client/Connection/SSLConnection.php
  23. +53 −0 api/utils/net/SMTP/Client/Connection/State/Closed.php
  24. +67 −0 api/utils/net/SMTP/Client/Connection/State/Connected.php
  25. +35 −0 api/utils/net/SMTP/Client/Connection/State/Established.php
  26. +26 −0 api/utils/net/SMTP/Client/Connection/TCPConnection.php
  27. +37 −0 api/utils/net/SMTP/Client/Connection/TLSConnection.php
  28. +69 −0 api/utils/net/SMTP/Client/ConnectionState.php
  29. +71 −0 api/utils/net/SMTP/Client/Message.php
  30. +261 −0 api/utils/net/SMTP/Message.php
  31. +119 −0 api/utils/net/SMTP/Message/AbstractAddressList.php
  32. +92 −0 api/utils/net/SMTP/Message/AbstractHeader.php
  33. +77 −0 api/utils/net/SMTP/Message/Address.php
  34. +29 −0 api/utils/net/SMTP/Message/AddressList.php
  35. +43 −0 api/utils/net/SMTP/Message/Composer.php
  36. +23 −0 api/utils/net/SMTP/Message/Encodable.php
  37. +27 −0 api/utils/net/SMTP/Message/Encoder.php
  38. +39 −0 api/utils/net/SMTP/Message/Encoder/Base64.php
  39. +76 −0 api/utils/net/SMTP/Message/Encoder/QuotedPrintable.php
  40. +31 −0 api/utils/net/SMTP/Message/Header.php
  41. +23 −0 api/utils/net/SMTP/Message/Header/Bcc.php
  42. +23 −0 api/utils/net/SMTP/Message/Header/Cc.php
  43. +29 −0 api/utils/net/SMTP/Message/Header/Date.php
  44. +42 −0 api/utils/net/SMTP/Message/Header/From.php
  45. +23 −0 api/utils/net/SMTP/Message/Header/ReplyTo.php
  46. +37 −0 api/utils/net/SMTP/Message/Header/Subject.php
  47. +23 −0 api/utils/net/SMTP/Message/Header/To.php
  48. +21 −0 api/utils/net/SMTP/Message/Header/Type/Structured.php
  49. +19 −0 api/utils/net/SMTP/Message/Header/Type/Unstructured.php
  50. +33 −0 api/utils/net/SMTP/Message/HeaderEncoder.php
  51. +93 −0 api/utils/net/SMTP/Message/HeaderSet.php
  52. +77 −0 api/utils/net/SMTP/Message/HeaderWrapper.php
@@ -0,0 +1,126 @@
<?php

/**
* @package utils.net.SMTP
* @author Andrey Knupp Vital <andreykvital@gmail.com>
* @filesource utils\net\SMTP\Client.php
*/
namespace utils\net\SMTP;
use utils\net\SMTP\Client\Connection;
use utils\net\SMTP\Client\Authentication;
use utils\net\SMTP\Message;
use utils\net\SMTP\Client\CommandInvoker;
use utils\net\SMTP\Client\Command\RCPTCommand;
use utils\net\SMTP\Client\Command\MAILCommand;
use utils\net\SMTP\Client\Command\DATACommand;
use utils\net\SMTP\Message\Composer;
use utils\net\SMTP\Message\Address;
use \RuntimeException;

class Client
{

/**
* The connection used to talk with the server.
* @var Connection
*/
private $connection = NULL;

/**
* - Constructor
* @param Connection $connection the connection used to talk with server
* @return Client
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}

/**
* Authenticates a user with provided authentication mechanism.
* @param Authentication $authentication the authentication mechanism
* @return boolean
*/
public function authenticate(Authentication $authentication)
{
return $this->connection->authenticate($authentication);
}

/**
* Closes an opened connection with the SMTP server.
* @return void
*/
public function close()
{
$this->connection->close();
}

/**
* Retrieves the latest exchanged message with the server.
* @see AbstractConnection::getLatestMessage()
* @return Message
*/
public function getLatestMessage()
{
return $this->connection->getLatestMessage();
}

/**
* Retrieves all exchanged messages with the server.
* @see AbstractConnection::getExchangedMessages()
* @return array[Message]
*/
public function getExchangedMessages()
{
return $this->connection->getExchangedMessages();
}

/**
* Sends a mail message
* @param Message $message the message to send
* @throws RuntimeException if the sender is not specified
* @return boolean
*/
public function send(Message $message)
{
$connection = $this->connection;
$commandInvoker = new CommandInvoker();

if(($from = $message->getFrom()) instanceof Address) {
$commandInvoker->invoke(new MAILCommand($connection, $from->getEmail()));

foreach($this->getRecipients($message) AS $recipient) {
$commandInvoker->invoke(new RCPTCommand($connection, $recipient));
}

$composer = new Composer();
$commandInvoker->invoke(new DATACommand($connection, $composer->compose($message)));
return true;
} else {
$message = "Couldn't send the message without specifying its sender";
throw new RuntimeException($message);
}
}

/**
* Prepares recipients addresses to send the message
* @param Message $message the message with their recipients
* @return array[string]
*/
private function getRecipients(Message $message)
{
$addresses = array_merge(
$message->getTo(), // Recipients
$message->getCc(), // Recipients that receive a copy
$message->getBcc() // Recipients that receive an blind copy
);

$recipients = array();
foreach($addresses AS $address) {
$recipients[] = $address->getEmail();
}

return array_unique($recipients);
}

}
@@ -0,0 +1,30 @@
<?php

/**
* @package utils.net.SMTP.Client
* @author Andrey Knupp Vital <andreykvital@gmail.com>
* @filesource utils\net\SMTP\Client\AbstractCommand.php
*/
namespace utils\net\SMTP\Client;
use utils\net\SMTP\Client\Command;
use utils\net\SMTP\Client\Connection;

abstract class AbstractCommand implements Command
{

/**
* The connection with server
* @var Connection
*/
protected $connection;

/**
* Sets the SMTP server connection to perform commands on it
* @param Connection $connection the SMTP server $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}

}
@@ -0,0 +1,151 @@
<?php

/**
* @package utils.net.SMTP.Client
* @author Andrey Knupp Vital <andreykvital@gmail.com>
* @filesource utils\net\SMTP\Client\AbstractConnection.php
*/
namespace utils\net\SMTP\Client;
use utils\net\SMTP\Client\Connection;
use utils\net\SMTP\Client\Connection\State\Closed;
use utils\net\SMTP\Client\ConnectionState;
use utils\net\SMTP\Client\CommandInvoker;
use utils\net\SMTP\Client\Command\EHLOCommand;
use utils\net\SMTP\Client\Command\HELOCommand;
use \Exception;

abstract class AbstractConnection implements Connection
{

/**
* Current connection state
* @var ConnectionState
*/
private $state;

/**
* Server name that we are connected (not resolved)
* @var string
*/
private $hostname;

/**
* Sets the initial state of connection (Closed).
* @return Connection
*/
public function __construct()
{
$this->state = new Closed();
}

/**
* Changes the connection state to a new one.
* @param ConnectionState $state the new state of the connection.
* @return void
*/
public function changeState(ConnectionState $state)
{
$this->state = $state;
}

/**
* Opens a connection with an SMTP server.
* @param string $protocol the protocol to be used to connect
* @param string $hostname the server hostname to connect
* @param integer $port the server listening port
* @param integer $timeout the timeout to wait for a connection
* @throws Exception if the server greeting wasn't received successfully
* @return boolean|void
*/
public function open($protocol, $hostname, $port, $timeout = 30)
{
if ($this->state->open($protocol, $hostname, $port, $timeout, $this)) {
$greeting = $this->read();
if (($code = $greeting->getCode()) !== 220) {
$message = "We haven't received the expected greeting";
throw new Exception($message, $code);
}

$this->hostname = $hostname;
$invoker = new CommandInvoker();
$invoker->invoke(new HELOCommand($this));
$invoker->invoke(new EHLOCommand($this));
return true;
}
}

/**
* Closes the opened connection with the server
* @return void
*/
public function close()
{
$this->state->close($this);
}

/**
* Reads a server reply.
* @return Message
*/
public function read()
{
return $this->state->read();
}

/**
* Writes data on the server stream.
* @param string $data the data to be written
* @return integer
*/
public function write($data)
{
return $this->state->write($data);
}

/**
* Authenticates an user in the SMTP server.
* @param Authentication $authentication the authentication mechanism to be used.
* @return boolean
*/
public function authenticate(Authentication $authentication)
{
return $this->state->authenticate($authentication, $this);
}

/**
* Retrieves the hostname of smtp server.
* @return string
*/
public function getHostname()
{
return $this->hostname;
}

/**
* Retrieves the stream connected with SMTP server.
* @return resource
*/
public function getStream()
{
return $this->state->getStream();
}

/**
* Retrieves the latest exchanged message with the server.
* @return Message
*/
public function getLatestMessage()
{
return $this->state->getLatestMessage();
}

/**
* Retrieves all exchanged messages with the server.
* @return array[Message]
*/
public function getExchangedMessages()
{
return $this->state->getExchangedMessages();
}

}
Loading

0 comments on commit 469cfee

Please sign in to comment.