Skip to content

Commit 1c95f9e

Browse files
committed
feat(mailer): add mailer package
1 parent 286d9a0 commit 1c95f9e

Some content is hidden

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

44 files changed

+1896
-0
lines changed

composer.json

Lines changed: 7 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",
@@ -63,6 +64,7 @@
6364
"rector/rector": "^2.0-rc2",
6465
"spatie/phpunit-snapshot-assertions": "^5.1.8",
6566
"spaze/phpstan-disallowed-calls": "^4.0",
67+
"symfony/postmark-mailer": "^7.2.6",
6668
"symplify/monorepo-builder": "^11.2",
6769
"tempest/blade": "^0.1.0",
6870
"twig/twig": "^3.16"
@@ -84,6 +86,7 @@
8486
"tempest/http": "self.version",
8587
"tempest/http-client": "self.version",
8688
"tempest/log": "self.version",
89+
"tempest/mailer": "self.version",
8790
"tempest/mapper": "self.version",
8891
"tempest/reflection": "self.version",
8992
"tempest/router": "self.version",
@@ -118,6 +121,7 @@
118121
"Tempest\\HttpClient\\": "packages/http-client/src",
119122
"Tempest\\Http\\": "packages/http/src",
120123
"Tempest\\Log\\": "packages/log/src",
124+
"Tempest\\Mail\\": "packages/mailer/src",
121125
"Tempest\\Mapper\\": "packages/mapper/src",
122126
"Tempest\\Reflection\\": "packages/reflection/src",
123127
"Tempest\\Router\\": "packages/router/src",
@@ -174,6 +178,9 @@
174178
"Tempest\\HttpClient\\Tests\\": "packages/http-client/tests",
175179
"Tempest\\Http\\Tests\\": "packages/http/tests",
176180
"Tempest\\Log\\Tests\\": "packages/log/tests",
181+
"Tempest\\Mail\\Tests\\": [
182+
"packages/mailer/tests"
183+
],
177184
"Tempest\\Mapper\\Tests\\": "packages/mapper/tests",
178185
"Tempest\\Reflection\\Tests\\": "packages/reflection/tests",
179186
"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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "tempest/mailer",
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+
"symfony/mailer": "^7.2.6"
10+
},
11+
"require-dev": {
12+
"symfony/postmark-mailer": "^7.2.6"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"Tempest\\Mail\\": "src"
17+
}
18+
}
19+
}

packages/mailer/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/mailer/src/Attachment.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Tempest\Mail;
4+
5+
interface Attachment
6+
{
7+
/**
8+
* Resolves the attachment to raw data.
9+
*/
10+
public \Closure $resolve {
11+
get;
12+
}
13+
14+
/**
15+
* File name of the attachment.
16+
*/
17+
public ?string $name {
18+
get;
19+
}
20+
21+
/**
22+
* Content type of the attachment.
23+
*
24+
* TODO: enum?
25+
*/
26+
public ?string $contentType {
27+
get;
28+
}
29+
}

packages/mailer/src/Content.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Tempest\Mail;
4+
5+
use Tempest\Mail\Exceptions\MissingContentException;
6+
use Tempest\View\View;
7+
8+
/**
9+
* Represents the content of an {@see \Tempest\Mailer\Envelope}.
10+
*/
11+
final class Content
12+
{
13+
public function __construct(
14+
public null|string|View $html = null,
15+
public ?string $text = null,
16+
/** @var Attachment[] */
17+
public array $attachments = [],
18+
) {
19+
if (! $text && ! $html) {
20+
throw new MissingContentException();
21+
}
22+
}
23+
}

packages/mailer/src/Email.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Tempest\Mail;
4+
5+
interface Email
6+
{
7+
/**
8+
* The envelope of the email.
9+
*/
10+
public Envelope $envelope {
11+
get;
12+
}
13+
14+
/**
15+
* The content of the email.
16+
*/
17+
public Content $content {
18+
get;
19+
}
20+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Mail;
6+
7+
use Symfony\Component\Mime\Email as SymfonyEmail;
8+
use Tempest\Mail\Exceptions\MissingExpeditorAddressException;
9+
use Tempest\Mail\Exceptions\MissingRecipientAddressException;
10+
use Tempest\Mapper\Mapper;
11+
use Tempest\Support\Arr;
12+
use Tempest\View\View;
13+
use Tempest\View\ViewRenderer;
14+
15+
final readonly class EmailToSymfonyEmailMapper implements Mapper
16+
{
17+
public function __construct(
18+
private readonly MailerConfig $mailerConfig,
19+
private readonly ViewRenderer $viewRenderer,
20+
) {}
21+
22+
public function canMap(mixed $from, mixed $to): bool
23+
{
24+
if ($from instanceof Email) {
25+
return true;
26+
}
27+
28+
return false;
29+
}
30+
31+
public function map(mixed $from, mixed $to): SymfonyEmail
32+
{
33+
/** @var Email $email */
34+
$email = $from;
35+
$symfonyEmail = new SymfonyEmail();
36+
37+
if ($email->envelope->from) {
38+
$symfonyEmail->from(...Arr\wrap($email->envelope->from));
39+
} elseif ($this->mailerConfig->from) {
40+
$symfonyEmail->from($this->mailerConfig->from);
41+
} else {
42+
throw new MissingExpeditorAddressException();
43+
}
44+
45+
if ($email->envelope->to) {
46+
$symfonyEmail->to(...Arr\wrap($email->envelope->to));
47+
} else {
48+
throw new MissingRecipientAddressException();
49+
}
50+
51+
if ($email->envelope->cc) {
52+
$symfonyEmail->cc(...Arr\wrap($email->envelope->cc));
53+
}
54+
55+
if ($email->envelope->bcc) {
56+
$symfonyEmail->bcc(...Arr\wrap($email->envelope->bcc));
57+
}
58+
59+
if ($email->envelope->replyTo) {
60+
$symfonyEmail->replyTo(...Arr\wrap($email->envelope->replyTo));
61+
}
62+
63+
if ($email->envelope->subject) {
64+
$symfonyEmail->subject($email->envelope->subject);
65+
}
66+
67+
if ($email->envelope->priority) {
68+
$symfonyEmail->priority($email->envelope->priority->value);
69+
}
70+
71+
if ($email->content->text) {
72+
$symfonyEmail->text($email->content->text);
73+
}
74+
75+
if ($email->content->html instanceof View) {
76+
$symfonyEmail->html($this->viewRenderer->render($email->content->html));
77+
} elseif ($email->content->html) {
78+
$symfonyEmail->html($email->content->html);
79+
}
80+
81+
/** @var Attachment $attachment */
82+
foreach (Arr\wrap($email->content->attachments) as $attachment) {
83+
$symfonyEmail->attach(($attachment->resolve)(), $attachment->name, $attachment->contentType);
84+
}
85+
86+
return $symfonyEmail;
87+
}
88+
}

packages/mailer/src/Envelope.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tempest\Mail;
4+
5+
use Symfony\Component\Mime\Address;
6+
7+
/**
8+
* Represents the envelope of an email.
9+
*/
10+
final class Envelope
11+
{
12+
public function __construct(
13+
public ?string $subject,
14+
public null|string|array|Address $to,
15+
public null|string|array|Address $from = null,
16+
public null|string|array|Address $cc = null,
17+
public null|string|array|Address $bcc = null,
18+
public null|string|array|Address $replyTo = null,
19+
public ?Priority $priority = null,
20+
) {}
21+
}

0 commit comments

Comments
 (0)