Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"tempest/http": "self.version",
"tempest/http-client": "self.version",
"tempest/log": "self.version",
"tempest/mailer": "self.version",
"tempest/mapper": "self.version",
"tempest/reflection": "self.version",
"tempest/router": "self.version",
Expand Down Expand Up @@ -118,6 +119,7 @@
"Tempest\\HttpClient\\": "packages/http-client/src",
"Tempest\\Http\\": "packages/http/src",
"Tempest\\Log\\": "packages/log/src",
"Tempest\\Mailer\\": "packages/mailer/src",
"Tempest\\Mapper\\": "packages/mapper/src",
"Tempest\\Reflection\\": "packages/reflection/src",
"Tempest\\Router\\": "packages/router/src",
Expand Down Expand Up @@ -174,6 +176,7 @@
"Tempest\\HttpClient\\Tests\\": "packages/http-client/tests",
"Tempest\\Http\\Tests\\": "packages/http/tests",
"Tempest\\Log\\Tests\\": "packages/log/tests",
"Tempest\\Mailer\\Tests\\": "packages/mailer/tests",
"Tempest\\Mapper\\Tests\\": "packages/mapper/tests",
"Tempest\\Reflection\\Tests\\": "packages/reflection/tests",
"Tempest\\Router\\Tests\\": "packages/router/tests",
Expand Down
10 changes: 10 additions & 0 deletions packages/mailer/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Exclude build/test files from the release
.github/ export-ignore
tests/ export-ignore
.gitattributes export-ignore
.gitignore export-ignore
phpunit.xml export-ignore
README.md export-ignore

# Configure diff output for .php and .phar files.
*.php diff=php
9 changes: 9 additions & 0 deletions packages/mailer/LICENCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024 Brent Roose [email protected]

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
21 changes: 21 additions & 0 deletions packages/mailer/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "tempest/mailer",
"description": "A component for mails.",
"license": "MIT",
"minimum-stability": "dev",
"require": {
"php": "^8.4",
"tempest/core": "dev-main",
"tempest/view": "dev-main"
},
"autoload": {
"psr-4": {
"Tempest\\Mailer\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tempest\\Mailer\\Tests\\": "tests"
}
}
}
16 changes: 16 additions & 0 deletions packages/mailer/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<testsuites>
<testsuite name="Tempest Mapper">
<directory>tests</directory>
</testsuite>
</testsuites>
<source restrictNotices="true" restrictWarnings="true" ignoreIndirectDeprecations="true">
<include>
<directory>src</directory>
</include>
</source>
<php>
<env name="MAIL_FROM" value="[email protected]" />
</php>
</phpunit>
5 changes: 5 additions & 0 deletions packages/mailer/src/Config/mail.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Tempest\Mailer\MailConfig;

return new MailConfig();
17 changes: 17 additions & 0 deletions packages/mailer/src/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tempest\Mailer;

use Stringable;

final readonly class Email
{
public function __construct(
public string|Stringable|null $from,
public string|Stringable|array $to,
public string|Stringable $subject,
public string|Stringable $body,
public array $attachments = [],
public bool $async = false,
) {}
}
13 changes: 13 additions & 0 deletions packages/mailer/src/InvalidFromAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tempest\Mailer;

use Exception;

final class InvalidFromAddress extends Exception
{
public function __construct()
{
parent::__construct("There's no valid from address configured.");
}
}
14 changes: 14 additions & 0 deletions packages/mailer/src/MailConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Tempest\Mailer;

final class MailConfig
{
public ?string $from;

public function __construct(
?string $from = null,
) {
$this->from = $from ?? env('MAIL_FROM');
}
}
58 changes: 58 additions & 0 deletions packages/mailer/src/Mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Tempest\Mailer;

use Tempest\View\ViewRenderer;

use function Tempest\view;

final class Mailer
{
public function __construct(
private MailConfig $config,
private ViewRenderer $renderer,
) {}

public function send(string $path, ...$params): void

Check warning on line 16 in packages/mailer/src/Mailer.php

View workflow job for this annotation

GitHub Actions / Run style check

strictness/require-parameter-type

Parameter `$params` is missing a type hint. Type hints improve code readability and help prevent type-related errors. Help: Consider adding a type hint to parameter `$params`.
{
$html = $this->renderer->render(view($path, ...$params));

[$headerString, $body] = explode(PHP_EOL, $html, 2);

$email = $this->makeEmail($headerString, $body);

$this->sendEmail($email);
}

public function sendEmail(Email $email): void
{
if ($email->from === null) {
throw new InvalidFromAddress();
}

ld($email);
}

private function makeEmail(string $headerString, string $body): Email
{
$parsedHeaders = [];

preg_match_all('/(?<header>\w+)="(?<value>.*?)"/', $headerString, $headers);

foreach ($headers[0] as $i => $line) {
$header = $headers['header'][$i];
$value = $headers['value'][$i];

$parsedHeaders[$header] = $value;
}

$parsedHeaders['attachments'] = explode(',', $parsedHeaders['attachments'] ?? '');

$data = [
...$parsedHeaders,
'body' => $body,
];

return new Email(...$data);
}
}
15 changes: 15 additions & 0 deletions packages/mailer/src/x-mail.view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from="{{ $from }}" to="{{ $to }}" subject="{{ $subject }}" async="{{ ($async ?? true) ? 'true' : 'false' }}" attachments="{{ implode(',', $attachments ?? []) }}"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>

<div :if="isset($pretext)" style="display: none;font-size: 1px;color:#fff;line-height: 1px;max-height: 0;opacity: 0;overflow: hidden;">{{ $pretext }}</div>

<x-slot/>

</body>
</html>
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
<env name="BASE_URI" value="" />
<env name="CACHE" value="null" />
<env name="DISCOVERY_CACHE" value="true" />
<env name="MAIL_FROM" value="[email protected]" />
</php>
</phpunit>
1 change: 1 addition & 0 deletions tests/Integration/Mailer/Fixtures/attachment.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hi
29 changes: 29 additions & 0 deletions tests/Integration/Mailer/MailerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Integration\Mailer;

use Tempest\Mailer\Mailer;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

final class MailerTest extends FrameworkIntegrationTestCase
{
public function test_mailer(): void
{
$mailer = $this->container->get(Mailer::class);

$user = (object) [
'email' => '[email protected]',
'name' => 'Brent Roose',
'first_name' => 'Brent',
];

$mailer->send(
__DIR__ . '/test-mail.view.php',
user: $user,
files: [
__DIR__ . '/Fixtures/attachment.txt',
__DIR__ . '/test-mail.view.php',
],
);
}
}
14 changes: 14 additions & 0 deletions tests/Integration/Mailer/test-mail.view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<x-mail
from="[email protected]"
:to="$user->email"
:subject="'Welcome ' . $user->name"
:attachments="$files"
>
Hello {{ $user->first_name }}

Thanks for checking out Tempest!

See you soon!

— The Tempest Team
</x-mail>
Loading