2017 © Pedro PelĂĄez
 

library smtp-server

SMTP Server based on ReactPHP

image

smalot/smtp-server

SMTP Server based on ReactPHP

  • Monday, January 9, 2017
  • by smalot
  • Repository
  • 2 Watchers
  • 9 Stars
  • 99 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 2 Versions
  • 21 % Grown

The README.md

SMTP Server

SMTP Server based on ReactPHP., (*1)

Widely inspired from SAM-IT/react-smtp., (*2)

Scrutinizer Code Quality Code Coverage Build Status, (*3)

Features: * supports many concurrent SMTP connections * supports anonymous connections * supports PLAIN, LOGIN and CRAM-MD5 authentication methods * use Symfony event dispatcher, (*4)

It is advised to install additionnal PHP libraries: * events * mailparse, (*5)

Security

By default, username and password are not checked. However, you can override the Server class to implement your own logic., (*6)

class MyServer extends \Smalot\Smtp\Server\Server
{
    /**
     * @param Connection $connection
     * @param MethodInterface $method
     * @return bool
     */
    public function checkAuth(Connection $connection, MethodInterface $method)
    {
        $username = $method->getUsername();
        $password = $this->getPasswordForUsername();

        return $method->validateIdentity($password);
    }

    /**
     * @param string $username
     * @return string
     */
    protected function getPasswordForUsername($username)
    {
        // @Todo: Load password from Database or somewhere else.
        $password = '';

        return $password;
    }
}

Sample code

Server side - launcher

include 'vendor/autoload.php';

try {
    $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();

    $logger = new \Monolog\Logger('log');
    $dispatcher->addSubscriber(new \Smalot\Smtp\Server\Event\LogSubscriber($logger));

    $loop = React\EventLoop\Factory::create();
    $server = new \Smalot\Smtp\Server\Server($loop, $dispatcher);
    // Enable 3 authentication methods.
    $server->authMethods = [
      \Smalot\Smtp\Server\Connection::AUTH_METHOD_LOGIN,
      \Smalot\Smtp\Server\Connection::AUTH_METHOD_PLAIN,
      \Smalot\Smtp\Server\Connection::AUTH_METHOD_CRAM_MD5,
    ];
    // Listen on port 25.
    $server->listen(25);
    $loop->run();
}
catch(\Exception $e) {
    var_dump($e);
}

Client side

include 'vendor/autoload.php';

try {
    $mail = new PHPMailer();

    $mail->isSMTP();
    $mail->Host = 'localhost';
    $mail->Port = 25;
    $mail->SMTPDebug = true;

    $mail->SMTPAuth = true;
    $mail->Username = "foo@gmail.com";
    $mail->Password = "foo@gmail.com";

    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
}
catch(\Exception $e) {
    var_dump($e);
}

Composer

Sample project code for both client and server parts., (*7)

{
    "require": {
        "react/event-loop": "^0.4.2",
        "smalot/smtp-server": "dev-master",
        "phpmailer/phpmailer": "^5.2"
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "git@github.com:smalot/react-smtp.git"
        }
    ]
}

Decode message

Using the "php-mime-mail-parser/php-mime-mail-parser": "^2.6" package, you can parse the whole message., (*8)

doc: https://packagist.org/packages/php-mime-mail-parser/php-mime-mail-parser, (*9)

However, need to install the mailparse PHP Extension. To do such a thing, you need to install the mbstring PHP Extension, compile it with PEAR and enable the mailparse extension after the mbstring (using a higher digit)., (*10)

It should be necessary to alter source code to remove the check on mbstring existence due to an error in this check., (*11)

The Versions

09/01 2017

dev-master

9999999-dev

SMTP Server based on ReactPHP

  Sources   Download

MIT

The Requires

 

server event parse smtp reactphp

08/01 2017

v0.1

0.1.0.0

SMTP Server based on ReactPHP

  Sources   Download

MIT

The Requires

 

server event parse smtp reactphp