, delivers nothing anywhere and writes one JSON line per message (envelope sender, envelope * recipients, raw data) to . It stops when .stop exists or after (default 120). * * Usage: php tests/Support/smtp-sink.php [seconds] */ $port = (int) ($argv[1] ?? 2525); $file = (string) ($argv[2] ?? sys_get_temp_dir().'/smtp-sink.jsonl'); $until = time() + (int) ($argv[3] ?? 120); $server = stream_socket_server('tcp://127.0.0.1:'.$port, $errorCode, $errorMessage); if ($server === false) { fwrite(STDERR, "SMTP sink: {$errorMessage}\n"); exit(1); } stream_set_blocking($server, false); file_put_contents($file.'.ready', (string) getmypid()); /** @var array, data: ?string}> $clients */ $clients = []; $send = function ($socket, string $line): void { @fwrite($socket, $line."\r\n"); }; while (time() < $until && ! file_exists($file.'.stop')) { $read = [$server]; foreach ($clients as $entry) { $read[] = $entry['socket']; } $write = $except = null; if (@stream_select($read, $write, $except, 0, 200000) === false) { break; } foreach ($read as $socket) { if ($socket === $server) { $connection = @stream_socket_accept($server, 0); if ($connection !== false) { stream_set_blocking($connection, false); $clients[(int) $connection] = ['socket' => $connection, 'buffer' => '', 'from' => null, 'to' => [], 'data' => null]; $send($connection, '220 fewo-smtp-sink ESMTP'); } continue; } $id = (int) $socket; $chunk = @fread($socket, 65536); if ($chunk === '' || $chunk === false) { if (feof($socket)) { fclose($socket); unset($clients[$id]); } continue; } $clients[$id]['buffer'] .= $chunk; while (true) { $client = &$clients[$id]; if ($client['data'] !== null) { $end = strpos($client['buffer'], "\r\n.\r\n"); if ($end === false) { break; } $client['data'] .= substr($client['buffer'], 0, $end); $client['buffer'] = substr($client['buffer'], $end + 5); file_put_contents($file, json_encode(['from' => $client['from'], 'to' => $client['to'], 'data' => $client['data']])."\n", FILE_APPEND | LOCK_EX); $client['from'] = null; $client['to'] = []; $client['data'] = null; $send($socket, '250 OK: queued in the sink'); continue; } $position = strpos($client['buffer'], "\r\n"); if ($position === false) { break; } $line = substr($client['buffer'], 0, $position); $client['buffer'] = substr($client['buffer'], $position + 2); $command = strtoupper(substr($line, 0, 4)); match (true) { $command === 'EHLO' => $send($socket, "250-fewo-smtp-sink\r\n250 8BITMIME"), $command === 'HELO' => $send($socket, '250 fewo-smtp-sink'), $command === 'MAIL' => [$client['from'] = trim(substr($line, 10), ' <>'), $send($socket, '250 OK')], $command === 'RCPT' => [$client['to'][] = trim(substr($line, 8), ' <>'), $send($socket, '250 OK')], $command === 'DATA' => [$client['data'] = '', $send($socket, '354 End data with .')], $command === 'RSET' => [$client['from'] = null, $client['to'] = [], $send($socket, '250 OK')], $command === 'NOOP' => $send($socket, '250 OK'), $command === 'QUIT' => [$send($socket, '221 Bye'), fclose($socket), $clients[$id] = null], default => $send($socket, '502 Command not implemented'), }; unset($client); if ($clients[$id] === null) { unset($clients[$id]); break; } } unset($client); } } foreach ($clients as $entry) { @fclose($entry['socket']); } fclose($server); @unlink($file.'.ready');