Listen host (default: 0.0.0.0)\n"; echo " -p, --port Listen port (default: 25)\n"; echo " -m, --max-connections Maximum connections (default: 100)\n"; echo " --help Show this help message\n"; exit(0); } // 从配置文件获取默认值 $config = Config::getInstance(); $host = $options['h'] ?? $options['host'] ?? $config->get('smtp.host', '0.0.0.0'); $port = $options['p'] ?? $options['port'] ?? $config->get('smtp.port', 25); $maxConnections = $options['m'] ?? $options['max-connections'] ?? $config->get('smtp.max_connections', 100); // 转换端口和最大连接数为整数 $port = (int)$port; $maxConnections = (int)$maxConnections; // 验证参数 if ($port < 1 || $port > 65535) { echo "Invalid port number: $port\n"; exit(1); } if ($maxConnections < 1 || $maxConnections > 1000) { echo "Invalid maximum connections: $maxConnections\n"; exit(1); } // 创建并启动SMTP服务器 $server = new SmtpServer($host, $port, $maxConnections); echo "Starting SMTP Server on $host:$port...\n"; echo "Maximum connections: $maxConnections\n"; echo "Press Ctrl+C to stop the server\n\n"; // 捕获Ctrl+C信号,优雅关闭服务器(仅在支持PCNTL的系统上) if (function_exists('pcntl_signal')) { declare(ticks = 1); pcntl_signal(SIGINT, function() use ($server) { echo "\nStopping SMTP Server...\n"; $server->stop(); exit(0); }); } // 启动服务器 if (!$server->start()) { echo "Failed to start SMTP Server\n"; exit(1); }