Skip to content

Commit 275b608

Browse files
committed
wip
1 parent 98bfc5f commit 275b608

File tree

14 files changed

+203
-0
lines changed

14 files changed

+203
-0
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"tempest/http": "self.version",
8585
"tempest/http-client": "self.version",
8686
"tempest/log": "self.version",
87+
"tempest/mailer": "self.version",
8788
"tempest/mapper": "self.version",
8889
"tempest/reflection": "self.version",
8990
"tempest/router": "self.version",
@@ -118,6 +119,7 @@
118119
"Tempest\\HttpClient\\": "packages/http-client/src",
119120
"Tempest\\Http\\": "packages/http/src",
120121
"Tempest\\Log\\": "packages/log/src",
122+
"Tempest\\Mailer\\": "packages/mailer/src",
121123
"Tempest\\Mapper\\": "packages/mapper/src",
122124
"Tempest\\Reflection\\": "packages/reflection/src",
123125
"Tempest\\Router\\": "packages/router/src",
@@ -174,6 +176,7 @@
174176
"Tempest\\HttpClient\\Tests\\": "packages/http-client/tests",
175177
"Tempest\\Http\\Tests\\": "packages/http/tests",
176178
"Tempest\\Log\\Tests\\": "packages/log/tests",
179+
"Tempest\\Mailer\\Tests\\": "packages/mailer/tests",
177180
"Tempest\\Mapper\\Tests\\": "packages/mapper/tests",
178181
"Tempest\\Reflection\\Tests\\": "packages/reflection/tests",
179182
"Tempest\\Router\\Tests\\": "packages/router/tests",

packages/mailer/.gitattributes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Exclude build/test files from the release
2+
.github/ export-ignore
3+
tests/ export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
phpunit.xml export-ignore
7+
README.md export-ignore
8+
9+
# Configure diff output for .php and .phar files.
10+
*.php diff=php

packages/mailer/LICENCE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2024 Brent Roose [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/mailer/composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "tempest/mailer",
3+
"description": "A component for mails.",
4+
"license": "MIT",
5+
"minimum-stability": "dev",
6+
"require": {
7+
"php": "^8.4",
8+
"tempest/core": "dev-main",
9+
"tempest/view": "dev-main"
10+
},
11+
"autoload": {
12+
"psr-4": {
13+
"Tempest\\Mailer\\": "src"
14+
}
15+
},
16+
"autoload-dev": {
17+
"psr-4": {
18+
"Tempest\\Mailer\\Tests\\": "tests"
19+
}
20+
}
21+
}

packages/mailer/phpunit.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd" bootstrap="vendor/autoload.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" displayDetailsOnPhpunitDeprecations="true" failOnPhpunitDeprecation="false" failOnRisky="true" failOnWarning="true">
3+
<testsuites>
4+
<testsuite name="Tempest Mapper">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<source restrictNotices="true" restrictWarnings="true" ignoreIndirectDeprecations="true">
9+
<include>
10+
<directory>src</directory>
11+
</include>
12+
</source>
13+
<php>
14+
<env name="MAIL_FROM" value="[email protected]" />
15+
</php>
16+
</phpunit>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Tempest\Mailer\MailConfig;
4+
5+
return new MailConfig();

packages/mailer/src/Email.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Tempest\Mailer;
4+
5+
use Stringable;
6+
7+
final readonly class Email
8+
{
9+
public function __construct(
10+
public string|Stringable|null $from,
11+
public string|Stringable|array $to,
12+
public string|Stringable $subject,
13+
public string|Stringable $body,
14+
public array $attachments = [],
15+
public bool $async = false,
16+
) {}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Tempest\Mailer;
4+
5+
use Exception;
6+
7+
final class InvalidFromAddress extends Exception
8+
{
9+
public function __construct()
10+
{
11+
parent::__construct("There's no valid from address configured.");
12+
}
13+
}

packages/mailer/src/MailConfig.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Tempest\Mailer;
4+
5+
final class MailConfig
6+
{
7+
public ?string $from;
8+
9+
public function __construct(
10+
?string $from = null,
11+
) {
12+
$this->from = $from ?? env('MAIL_FROM');
13+
}
14+
}

packages/mailer/src/Mailer.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Tempest\Mailer;
4+
5+
use Tempest\View\ViewRenderer;
6+
use function Tempest\view;
7+
8+
final class Mailer
9+
{
10+
public function __construct(
11+
private MailConfig $config,
12+
private ViewRenderer $renderer,
13+
) {}
14+
15+
public function send(string $path, ...$params): void
16+
{
17+
$html = $this->renderer->render(view($path, ...$params));
18+
19+
[$header, $body] = explode(PHP_EOL, $html, 2);
20+
21+
$email = $this->makeEmail($header, $body);
22+
23+
$this->sendEmail($email);
24+
}
25+
26+
public function sendEmail(Email $email): void
27+
{
28+
if ($email->from === null) {
29+
throw new InvalidFromAddress();
30+
}
31+
32+
ld($email);
33+
}
34+
35+
private function makeEmail(string $header, string $body): Email
36+
{
37+
$parsedHeaders = [];
38+
39+
$headers = explode(';', $header);
40+
41+
foreach ($headers as $header) {
42+
[$name, $value] = explode(':', $header);
43+
44+
$parsedHeaders[trim($name)] = trim($value);
45+
}
46+
47+
$data = [
48+
...$parsedHeaders,
49+
'body' => $body,
50+
];
51+
52+
return new Email(...$data);
53+
}
54+
}

0 commit comments

Comments
 (0)