smalot/smtp-server

基于 ReactPHP 的 SMTP 服务器

v0.1 2017-01-08 20:48 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:20:19 UTC


README

基于 ReactPHP 的 SMTP 服务器。

受到了 SAM-IT/react-smtp 的广泛启发。

Scrutinizer Code Quality Code Coverage Build Status

特性

  • 支持许多并发 SMTP 连接
  • 支持匿名连接
  • 支持 PLAIN, LOGIN 和 CRAM-MD5 认证方法
  • 使用 Symfony 事件调度器

建议安装额外的 PHP 库

安全

默认情况下,usernamepassword 不进行检查。然而,您可以覆盖 Server 类来实现自己的逻辑。

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;
    }
}

示例代码

服务器端 - 启动器

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);
}

客户端

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

为客户端和服务器部分提供示例项目代码。

{
    "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"
        }
    ]
}

解码消息

使用 "php-mime-mail-parser/php-mime-mail-parser": "^2.6" 包,您可以解析整个消息。

文档: https://packagist.org.cn/packages/php-mime-mail-parser/php-mime-mail-parser

但是,需要安装 mailparse PHP 扩展。为此,您需要安装 mbstring PHP 扩展,使用 PEAR 编译它,并在 mbstring 之后启用 mailparse 扩展(使用更高的数字)。

由于此检查中的错误,可能需要修改源代码以删除对 mbstring 存在的检查。