Skip to content

Commit 3f5f31e

Browse files
innocenzibrendt
andauthored
feat(mailer): introduce mailer component (#1227)
Co-authored-by: Brent Roose <[email protected]>
1 parent e1bdcf2 commit 3f5f31e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2323
-36
lines changed

bin/validate-packages

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function checkPackageLicense(array $package): void
3333

3434
if (! file_exists($licenseFile)) {
3535
throw new UnexpectedValueException(
36-
sprintf('Package [tempest/%s] is missing it\'s license.', $package['name'])
36+
sprintf('Package [tempest/%s] is missing it\'s license (%s).', $package['name'], $licenseFile)
3737
);
3838
}
3939

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"psr/http-message": "^1.0|^2.0",
3232
"psr/log": "^3.0.0",
3333
"symfony/cache": "^7.2",
34+
"symfony/mailer": "^7.2.6",
3435
"symfony/process": "^7.1",
3536
"symfony/uid": "^7.1",
3637
"symfony/var-dumper": "^7.1",
@@ -65,6 +66,8 @@
6566
"rector/rector": "^2.0-rc2",
6667
"spatie/phpunit-snapshot-assertions": "^5.1.8",
6768
"spaze/phpstan-disallowed-calls": "^4.0",
69+
"symfony/amazon-mailer": "^7.2.0",
70+
"symfony/postmark-mailer": "^7.2.6",
6871
"symplify/monorepo-builder": "^11.2",
6972
"tempest/blade": "^0.1.0",
7073
"twig/twig": "^3.16"
@@ -88,6 +91,7 @@
8891
"tempest/icon": "self.version",
8992
"tempest/intl": "self.version",
9093
"tempest/log": "self.version",
94+
"tempest/mail": "self.version",
9195
"tempest/mapper": "self.version",
9296
"tempest/reflection": "self.version",
9397
"tempest/router": "self.version",
@@ -124,6 +128,7 @@
124128
"Tempest\\Icon\\": "packages/icon/src",
125129
"Tempest\\Intl\\": "packages/intl/src",
126130
"Tempest\\Log\\": "packages/log/src",
131+
"Tempest\\Mail\\": "packages/mail/src",
127132
"Tempest\\Mapper\\": "packages/mapper/src",
128133
"Tempest\\Reflection\\": "packages/reflection/src",
129134
"Tempest\\Router\\": "packages/router/src",
@@ -184,6 +189,7 @@
184189
"Tempest\\Icon\\Tests\\": "packages/icon/tests",
185190
"Tempest\\Intl\\Tests\\": "packages/intl/tests",
186191
"Tempest\\Log\\Tests\\": "packages/log/tests",
192+
"Tempest\\Mail\\Tests\\": "packages/mail/tests",
187193
"Tempest\\Mapper\\Tests\\": "packages/mapper/tests",
188194
"Tempest\\Reflection\\Tests\\": "packages/reflection/tests",
189195
"Tempest\\Router\\Tests\\": "packages/router/tests",

packages/mail/.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/mail/LICENSE.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/mail/composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "tempest/mail",
3+
"description": "A component for working with emails.",
4+
"license": "MIT",
5+
"minimum-stability": "dev",
6+
"require": {
7+
"php": "^8.4",
8+
"tempest/reflection": "dev-main",
9+
"tempest/support": "dev-main",
10+
"tempest/event-bus": "dev-main",
11+
"symfony/mailer": "^7.2.6"
12+
},
13+
"require-dev": {
14+
"symfony/postmark-mailer": "^7.2.6",
15+
"symfony/amazon-mailer": "^7.2.0"
16+
},
17+
"suggest": {
18+
"tempest/storage": "For storage attachment support"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"Tempest\\Mail\\": "src"
23+
}
24+
},
25+
"autoload-dev": {
26+
"psr-4": {
27+
"Tempest\\Mail\\Tests\\": "tests"
28+
}
29+
}
30+
}

packages/mail/phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 Mailer">
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+
</phpunit>

packages/mail/src/Address.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Tempest\Mail;
4+
5+
use Stringable;
6+
7+
final readonly class Address implements Stringable
8+
{
9+
public function __construct(
10+
public string $email,
11+
public ?string $name = null,
12+
) {}
13+
14+
public function __toString(): string
15+
{
16+
if ($this->name) {
17+
return sprintf('%s <%s>', $this->name, $this->email);
18+
}
19+
20+
return $this->email;
21+
}
22+
}

packages/mail/src/Attachment.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Tempest\Mail;
4+
5+
use Closure;
6+
use Tempest\Mail\Exceptions\FileAttachmentWasNotFound;
7+
use Tempest\Storage\Storage;
8+
use Tempest\Support\Filesystem;
9+
use Tempest\Support\Path;
10+
11+
final readonly class Attachment
12+
{
13+
/**
14+
* Creates an attachment that can be emailed.
15+
*
16+
* @param Closure $resolve Resolves the attachment to raw data.
17+
* @param null|string $name File name of the attachment.
18+
* @param null|string $contentType Content type of the attachment.
19+
*/
20+
public function __construct(
21+
public Closure $resolve,
22+
public ?string $name = null,
23+
public ?string $contentType = null,
24+
) {}
25+
26+
/**
27+
* Creates an attachment from the given closure.
28+
*/
29+
public static function fromClosure(callable $callable, ?string $name = null, ?string $contentType = null): self
30+
{
31+
return new self(
32+
resolve: Closure::fromCallable($callable),
33+
name: $name,
34+
contentType: $contentType,
35+
);
36+
}
37+
38+
/**
39+
* Creates an attachment from the storage.
40+
*/
41+
public static function fromStorage(Storage $storage, string $path, ?string $name = null, ?string $contentType = null): self
42+
{
43+
if (! $storage->fileOrDirectoryExists($path)) {
44+
throw FileAttachmentWasNotFound::forStorageFile($path);
45+
}
46+
47+
$path = Path\normalize($path);
48+
49+
return new self(
50+
resolve: fn () => $storage->readStream($path),
51+
name: $name ?? basename($path),
52+
contentType: $contentType ?? $storage->mimeType($path),
53+
);
54+
}
55+
56+
/**
57+
* Creates an attachment from the local filesystem.
58+
*/
59+
public static function fromFilesystem(string $path, ?string $name = null, ?string $contentType = null): self
60+
{
61+
$path = Path\normalize($path);
62+
63+
if (! Filesystem\is_file($path)) {
64+
throw new FileAttachmentWasNotFound($path);
65+
}
66+
67+
return new self(
68+
resolve: fn () => Filesystem\read_file($path),
69+
name: $name ?? basename($path),
70+
contentType: $contentType ?? finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path),
71+
);
72+
}
73+
}

packages/mail/src/Email.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Tempest\Mail;
4+
5+
use Tempest\View\View;
6+
7+
/**
8+
* Represents an email.
9+
*/
10+
interface Email
11+
{
12+
/**
13+
* The envelope of the email.
14+
*/
15+
public Envelope $envelope {
16+
get;
17+
}
18+
19+
/**
20+
* The content of the email can be a path to a view file, raw HTML, or a View object
21+
*/
22+
public string|View $html {
23+
get;
24+
}
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Tempest\Mail;
4+
5+
enum EmailPriority: int
6+
{
7+
/**
8+
* Indicates to email services that immediate attention is required.
9+
*/
10+
case HIGHEST = 1;
11+
12+
/**
13+
* Indicates to email services that this email is important, but not critical.
14+
*/
15+
case HIGH = 2;
16+
17+
/**
18+
* The default priority for regular emails.
19+
*/
20+
case NORMAL = 3;
21+
22+
/**
23+
* Indicates to email services that this email is not important and may be read later.
24+
*/
25+
case LOW = 4;
26+
27+
/**
28+
* Indicates to email services that this email is not important.
29+
*/
30+
case LOWEST = 5;
31+
}

0 commit comments

Comments
 (0)