Skip to content

Commit

Permalink
feat: support multiple emails in one connection
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 19, 2025
1 parent 40849c5 commit c022a54
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/Traffic/Dispatcher/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function dispatch(StreamClient $stream): iterable
{
$stream->sendData($this->createResponse(self::READY, 'mailamie'));
$protocol = [];

$message = null;
while (!$stream->isFinished()) {
$response = $stream->fetchLine();
Expand All @@ -47,20 +46,14 @@ public function dispatch(StreamClient $stream): iterable
$stream->sendData($this->createResponse(self::OK));
} elseif (\str_starts_with($response, 'QUIT')) {
$stream->sendData($this->createResponse(self::CLOSING));
$stream->disconnect();
yield new Frame\Smtp($message, $stream->getCreatedAt());

Check failure on line 49 in src/Traffic/Dispatcher/Smtp.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

PossiblyNullArgument

src/Traffic/Dispatcher/Smtp.php:49:38: PossiblyNullArgument: Argument 1 of Buggregator\Trap\Proto\Frame\Smtp::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)
$protocol = [];
} elseif (\str_starts_with($response, 'DATA')) {
$stream->sendData($this->createResponse(self::START_MAIL_INPUT));

$message = $this->parser->parseStream($protocol, $stream);
$stream->sendData($this->createResponse(self::OK));
}
}

if ($message === null) {
return;
}

yield new Frame\Smtp($message, $stream->getCreatedAt());
}

public function detect(string $data, \DateTimeImmutable $createdAt): ?bool
Expand Down
27 changes: 27 additions & 0 deletions tests/Unit/Traffic/Dispatcher/SmtpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ public function testDispatchOneMail(): void
});
}

public function testDispatchMultipleMails(): void
{
$stream = StreamClientMock::createFromGenerator((static function (\Generator ...$generators) {
foreach ($generators as $generator) {
yield from $generator;
}
})(
$this->mailMe('Test email 1'),
$this->mailMe('Test email 2'),
$this->mailMe('Test email 3'),
));

$this->runInFiber(static function () use ($stream) {
$i = 1;
foreach ((new Smtp())->dispatch($stream) as $frame) {
self::assertInstanceOf(SmtpFrame::class, $frame);
self::assertSame("Test email $i", $frame->message->getSubject());

if (++$i === 3) {
return;
}
}

self::fail('No frame was yielded.');
});
}

private function mailMe(string $subject = 'Test email'): \Generator
{
yield "EHLO\r\n";
Expand Down

0 comments on commit c022a54

Please sign in to comment.