-
Notifications
You must be signed in to change notification settings - Fork 2
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
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 b473f4c
Issue #110: update book tutorial
Howriq 564d9bf
Issue #110: update book tutorial
Howriq bd407bf
Issue #110: update book tutorial
Howriq b1789bd
Issue #110: update book tutorial
Howriq cc62378
Issue #110: update book tutorial
Howriq 44cd800
Issue #110: update book tutorial
Howriq ae15291
Issue #110: update book tutorial
Howriq dec0212
Issue #110: update book tutorial
Howriq 721a2a7
Issue #110: update book tutorial
Howriq 396ac51
Issue #110: update book tutorial
Howriq 99eba21
Issue #104: how to send an email and parse the content
Howriq 51be99d
Issue #104: how to send an email and parse the content
Howriq c642ea1
Issue #104: how to send an email and parse the content
Howriq 6e34aa9
Issue #104: how to send an email and parse the content
Howriq f3e1720
Issue #104: how to send an email and parse the content
Howriq 88628e1
Issue #104: how to send an email and parse the content
Howriq 3e8480a
Issue #104: how to send an email and parse the content
Howriq 96f916f
Issue #104: how to send an email and parse the content
Howriq a0b49b1
Issue #104: how to send an email and parse the content
Howriq e6d0528
Issue #104: how to send an email and parse the content
Howriq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
docs/book/v6/core-features/rendering-and-sending-emails.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.