-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151: add Sender mail to file
- Loading branch information
Showing
11 changed files
with
307 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,9 +214,13 @@ Environment variables can also be used to set endpoints: | |
Buggregator Trap provides a variety of "senders" that dictate where the dumps will be sent. Currently, the available | ||
sender options include: | ||
|
||
- `console`: This option displays dumps directly in the console. | ||
- `server`: With this choice, dumps are sent to a remote Buggregator server. | ||
- `file`: This allows for dumps to be stored in a file for future reference. | ||
- `console`: Shows dumps directly in the console. | ||
- `server`: Sends dumps to a remote Buggregator server. | ||
- `file`: Saves dumps in a file for later use. | ||
- `mail-to-file`: Creates a folder for each recipient and saves each message as a JSON file. Useful for testing mails. | ||
If you send a mail `To: [email protected], [email protected]`, the following folders will be created: | ||
- `runtime/mail/[email protected]` | ||
- `runtime/mail/[email protected]` | ||
|
||
By default, the Trap server is set to display dumps in the console. However, you can easily select your preferred | ||
senders using the `-s` option. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Sender; | ||
|
||
use Buggregator\Trap\Proto\Frame; | ||
use Buggregator\Trap\Proto\Frame\Smtp; | ||
use Buggregator\Trap\Sender; | ||
use Buggregator\Trap\Support\FileSystem; | ||
use Buggregator\Trap\Traffic\Message; | ||
use Buggregator\Trap\Traffic\Message\Smtp\Contact; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class MailToFileSender implements Sender | ||
{ | ||
private readonly string $path; | ||
|
||
public function __construct( | ||
string $path = 'runtime/mail', | ||
) { | ||
$this->path = \rtrim($path, '/\\'); | ||
FileSystem::mkdir($path); | ||
} | ||
|
||
public function send(iterable $frames): void | ||
{ | ||
/** @var Frame $frame */ | ||
foreach ($frames as $frame) { | ||
if (!$frame instanceof Smtp) { | ||
continue; | ||
} | ||
|
||
foreach (self::fetchDirectories($frame->message) as $dirName) { | ||
$path = $this->path . DIRECTORY_SEPARATOR . $dirName; | ||
FileSystem::mkdir($path); | ||
$filepath = \sprintf("%s/%s.json", $path, $frame->time->format('Y-m-d-H-i-s-v')); | ||
|
||
\assert(!\file_exists($filepath)); | ||
\file_put_contents($filepath, \json_encode($frame->message, \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get normalized email address for file or directory name. | ||
* | ||
* @return non-empty-string | ||
Check failure on line 50 in src/Sender/MailToFileSender.php GitHub Actions / psalm (ubuntu-latest, 8.2, locked)MoreSpecificReturnType
|
||
*/ | ||
private static function normalizeEmail(string $email): string | ||
{ | ||
return \preg_replace( | ||
Check failure on line 54 in src/Sender/MailToFileSender.php GitHub Actions / psalm (ubuntu-latest, 8.2, locked)LessSpecificReturnStatement
|
||
['/[^a-z0-9.\\- @]/i', '/\s+/'], | ||
['!', '_'], | ||
$email, | ||
); | ||
} | ||
|
||
/** | ||
* @return list<non-empty-string> | ||
Check failure on line 62 in src/Sender/MailToFileSender.php GitHub Actions / psalm (ubuntu-latest, 8.2, locked)MoreSpecificReturnType
|
||
*/ | ||
private static function fetchDirectories(Message\Smtp $message): array | ||
{ | ||
return | ||
\array_filter( | ||
Check failure on line 67 in src/Sender/MailToFileSender.php GitHub Actions / psalm (ubuntu-latest, 8.2, locked)LessSpecificReturnStatement
|
||
\array_unique( | ||
\array_map( | ||
static fn(Contact $c) => self::normalizeEmail($c->email), | ||
Check failure on line 70 in src/Sender/MailToFileSender.php GitHub Actions / psalm (ubuntu-latest, 8.2, locked)PossiblyNullArgument
|
||
\array_merge($message->getBcc(), $message->getTo()), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Support; | ||
|
||
/** | ||
* @internal | ||
* @psalm-internal Buggregator\Trap | ||
*/ | ||
final class FileSystem | ||
{ | ||
public static function mkdir(string $path, int $mode = 0777, bool $recursive = true): void | ||
{ | ||
\is_dir($path) or \mkdir($path, $mode, $recursive) or \is_dir($path) or throw new \RuntimeException( | ||
\sprintf('Directory "%s" was not created.', $path), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Tests\Unit\Sender; | ||
|
||
use Buggregator\Trap\Info; | ||
use Buggregator\Trap\Proto\Frame\Smtp as SmtpFrame; | ||
use Buggregator\Trap\Sender\MailToFileSender; | ||
use Buggregator\Trap\Traffic\Message\Smtp as SmtpMessage; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Buggregator\Trap\Sender\MailToFileSender | ||
*/ | ||
final class MailToFileSenderTest extends TestCase | ||
{ | ||
/** @var list<non-empty-string> */ | ||
private array $cleanupFolders = []; | ||
|
||
public function testForSmtp(): void | ||
{ | ||
$this->cleanupFolders[] = $root = Info::TRAP_ROOT . '/runtime/tests/mail-to-file-sender'; | ||
|
||
$message = SmtpMessage::create( | ||
protocol: [ | ||
'FROM' => ['<[email protected]>'], | ||
'BCC' => [ | ||
'<[email protected]>', | ||
'<[email protected]>', | ||
], | ||
], | ||
headers: [ | ||
'From' => ['Some User <[email protected]>'], | ||
'To' => [ | ||
'User1 <[email protected]>', | ||
'[email protected]', | ||
'User without email', // no email | ||
'User3 <[email protected]>, User4 <[email protected]>, [email protected]', | ||
], | ||
'Subject' => ['Very important theme'], | ||
'Content-Type' => ['text/plain'], | ||
], | ||
); | ||
$frame = new SmtpFrame($message); | ||
$sender = new MailToFileSender($root); | ||
$sender->send([$frame]); | ||
|
||
$this->assertRecipient("$root/[email protected]"); | ||
$this->assertRecipient("$root/[email protected]"); | ||
$this->assertRecipient("$root/[email protected]"); | ||
$this->assertRecipient("$root/[email protected]"); | ||
$this->assertRecipient("$root/[email protected]"); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
foreach ($this->cleanupFolders as $folder) { | ||
\array_map('unlink', \glob("$folder/*/*.*")); | ||
\array_map('rmdir', \glob("$folder/*")); | ||
\rmdir($folder); | ||
} | ||
} | ||
|
||
private function assertRecipient(string $folder): void | ||
{ | ||
self::assertDirectoryExists($folder); | ||
$files = \glob(\str_replace('[', '[[]', "$folder/*.json")); | ||
self::assertCount(1, $files); | ||
$arr = \json_decode(\file_get_contents($files[0]), true, \JSON_THROW_ON_ERROR); | ||
self::assertArrayHasKey('protocol', $arr); | ||
self::assertArrayHasKey('headers', $arr); | ||
self::assertArrayHasKey('messages', $arr); | ||
self::assertArrayHasKey('attachments', $arr); | ||
} | ||
} |
Oops, something went wrong.