Skip to content

Commit b1789bd

Browse files
committed
Issue #110: update book tutorial
Signed-off-by: horea <horea@rospace.com>
1 parent bd407bf commit b1789bd

1 file changed

Lines changed: 66 additions & 4 deletions

File tree

docs/book/v6/extended-features/email.md

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,73 @@
11
# Email sending and content parsing
22

33
In the previous version of Dotkernel API we have been using the `mezzio/mezzio-twigrenderer` package which added unnecessary complexity to the email sending in our API platform since APIs returns JSON data, not HTML.
4-
In order to fix this issue, we have come up with a lighter custom solution.
4+
Besides this, it used two services (`AdminService` and `UserService`) to send emails.
5+
It was not necessarily wrong, but their job should be only to manage Admin/User accounts.
6+
7+
In order to fix this those problems, we have come up with a lighter custom solution.
58
Now each project can prepare the bodies of the emails by using its preferred template renderer.
6-
`Core\App\MailService` is now decoupled by injecting the pre-rendered email body when calling its methods.
9+
`Core/src/App/src/Service/MailService` is now decoupled by injecting the pre-rendered email body when calling its methods.
10+
11+
Example from `Core/src/App/src/Service/MailService.php`:
12+
13+
```php
14+
<?php
15+
16+
declare(strict_types=1);
17+
18+
namespace Core\App\Service;
19+
20+
use Core\App\Message;
21+
use Core\User\Entity\User;
22+
use Dot\DependencyInjection\Attribute\Inject;
23+
use Dot\Log\LoggerInterface;
24+
use Dot\Mail\Exception\MailException;
25+
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
26+
27+
use function sprintf;
28+
29+
class MailService
30+
{
31+
/**
32+
* @param array<non-empty-string, mixed> $config
33+
*/
34+
#[Inject(
35+
'dot-mail.service.default',
36+
'dot-log.default_logger',
37+
'config',
38+
)]
39+
public function __construct(
40+
protected \Dot\Mail\Service\MailService $mailService,
41+
protected LoggerInterface $logger,
42+
private readonly array $config,
43+
) {
44+
}
45+
46+
/**
47+
* @throws MailException
48+
*/
49+
public function sendActivationMail(User $user, string $body): bool
50+
{
51+
if ($user->isActive()) {
52+
return false;
53+
}
54+
55+
$this->mailService->getMessage()->addTo($user->getEmail(), $user->getName());
56+
$this->mailService->setSubject('Welcome to ' . $this->config['application']['name']);
57+
$this->mailService->setBody($body);
58+
59+
try {
60+
return $this->mailService->send()->isValid();
61+
} catch (MailException | TransportExceptionInterface $exception) {
62+
$this->logger->err($exception->getMessage());
63+
throw new MailException(sprintf(Message::MAIL_NOT_SENT_TO, $user->getEmail()));
64+
}
65+
}
66+
}
67+
```
68+
69+
Rending example from `src/User/src/Handler/PostUserResourceHandler.php`:
770

8-
Example from `src/User/src/Handler/PostUserResourceHandler.php`:
971
```php
1072
if ($user->isPending()) {
1173
$this->mailService->sendActivationMail(
@@ -14,8 +76,8 @@ if ($user->isPending()) {
1476
);
1577
}
1678
```
79+
1780
In this case we are using the `phtml` template from `src/User/src/templates`.
1881
It has a lighter format compared to `twig`.
1982
It is then rendered before sending the activation email by our custom renderer from `src/App/src/Template/Rederer.php`.
2083
The other applications that use the Core structure such as [Dotkernel Admin](https://docs.dotkernel.org/admin-documentation/) use `mezzio/mezzio-twigrenderer` for this purpose.
21-

0 commit comments

Comments
 (0)