Skip to content

Issue #104: how to send an email and parse the content #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9757b37
Issue #110: update book tutorial
Howriq May 12, 2025
b473f4c
Issue #110: update book tutorial
Howriq May 13, 2025
564d9bf
Issue #110: update book tutorial
Howriq May 13, 2025
bd407bf
Issue #110: update book tutorial
Howriq May 13, 2025
b1789bd
Issue #110: update book tutorial
Howriq May 13, 2025
cc62378
Issue #110: update book tutorial
Howriq May 14, 2025
44cd800
Issue #110: update book tutorial
Howriq May 14, 2025
ae15291
Issue #110: update book tutorial
Howriq May 14, 2025
dec0212
Issue #110: update book tutorial
Howriq May 15, 2025
721a2a7
Issue #110: update book tutorial
Howriq May 16, 2025
396ac51
Issue #110: update book tutorial
Howriq May 19, 2025
99eba21
Issue #104: how to send an email and parse the content
Howriq May 19, 2025
51be99d
Issue #104: how to send an email and parse the content
Howriq May 20, 2025
c642ea1
Issue #104: how to send an email and parse the content
Howriq May 20, 2025
6e34aa9
Issue #104: how to send an email and parse the content
Howriq May 20, 2025
f3e1720
Issue #104: how to send an email and parse the content
Howriq May 20, 2025
88628e1
Issue #104: how to send an email and parse the content
Howriq May 20, 2025
3e8480a
Issue #104: how to send an email and parse the content
Howriq May 20, 2025
96f916f
Issue #104: how to send an email and parse the content
Howriq May 21, 2025
a0b49b1
Issue #104: how to send an email and parse the content
Howriq May 21, 2025
e6d0528
Issue #104: how to send an email and parse the content
Howriq May 21, 2025
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
44 changes: 44 additions & 0 deletions docs/book/v6/core-features/rendering-and-sending-emails.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Rendering and sending emails

In the previous versions of Dotkernel API we have been composing email bodies using **Twig** from the `mezzio/mezzio-twigrenderer` package.
In the current version of Dotkernel API, we introduced the core mail service `Core/src/App/src/Service/MailService` which is responsible for sending all emails.

Being a core service, `MailService` is used across all projects implementing the Core architecture.
To compose and send an email, a solid implementation of `TemplateRendererInterface` was required to be injected into `MailService`, because each method rendered and parsed their respective templates in place before sending an email.
This is acceptable with other Dotkernel applications which in most cases return a rendered template, but being that Dotkernel API mostly returns JSON objects, rendered with a different renderer, **Twig** had to be replaced with a lighter solution.

The solution is a custom [`Api\App\Template\Renderer`](https://github.com/dotkernel/api/blob/6.0/src/App/src/Template/Renderer.php) implementing [`Api\App\Template\RendererInterface`](https://github.com/dotkernel/api/blob/6.0/src/App/src/Template/RendererInterface.php).
This is a lightweight renderer, aimed at rendering a combination of **PHP** and **HTML** files with `phtml` extension.

With the new solution, `MailService` requires no implementation of any renderer because it no longer has to render templates internally.
Instead, an implementation of `Api\App\Template\RendererInterface` is first injected in the handler:

```php
class ExampleHandler extends AbstractHandler
{
#[Inject(
MailService::class,
RendererInterface::class,
)]
public function __construct(
protected MailService $mailService,
protected RendererInterface $renderer,
) {
}
```

Then, the handler calls the renderer and saves the rendered template in a variable:

```php
$body = $this->renderer->render('user::welcome', ['user' => $user]);
```

And finally, the handler calls the mail service with the composed $body being passed as a parameter to the method which sends the email:

```php
// $user object contains email, firstname and lastname

$this->mailService->sendWelcomeMail($user, $body);
```

> Other Dotkernel applications implementing the Core architecture do the same in the handlers, but keep using Twig as the template renderer.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ nav:
- "Exceptions": v6/core-features/exceptions.md
- "Dependency Injection": v6/core-features/dependency-injection.md
- "Error reporting": v6/core-features/error-reporting.md
- "Rendering and Sending emails": v6/core-features/rendering-and-sending-emails.md
- Extended features:
- "Core and App": v6/extended-features/core-and-app.md
- "New Handler Structure": v6/extended-features/handler-structure.md
Expand Down