|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\SendIt\Renderer; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
|
8
|
|
|
use Spiral\Mailer\Exception\MailerException; |
|
9
|
|
|
use Spiral\Mailer\MessageInterface; |
|
10
|
|
|
use Spiral\SendIt\Event\PostRender; |
|
11
|
|
|
use Spiral\SendIt\Event\PreRender; |
|
12
|
|
|
use Spiral\SendIt\RendererInterface; |
|
13
|
|
|
use Spiral\Views\Exception\ViewException; |
|
14
|
|
|
use Spiral\Views\ViewsInterface; |
|
15
|
|
|
use Symfony\Component\Mime\Email; |
|
16
|
|
|
|
|
17
|
|
|
final class ViewRenderer implements RendererInterface |
|
18
|
|
|
{ |
|
19
|
3 |
|
public function __construct( |
|
20
|
|
|
private readonly ViewsInterface $views, |
|
21
|
|
|
private readonly ?EventDispatcherInterface $eventDispatcher = null, |
|
22
|
3 |
|
) {} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Copy-pasted form https://stackoverflow.com/a/20806227 |
|
26
|
|
|
* Make sure the subject is ASCII-clean |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $subject Subject to encode |
|
29
|
|
|
* @return string Encoded subject |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public static function escapeSubject(string $subject): string |
|
32
|
|
|
{ |
|
33
|
1 |
|
if (!\preg_match('/[^\x20-\x7e]/', $subject)) { |
|
34
|
|
|
// ascii-only subject, return as-is |
|
35
|
1 |
|
return $subject; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Subject is non-ascii, needs encoding |
|
39
|
|
|
$encoded = \base64_encode($subject); |
|
40
|
|
|
$prefix = '=?UTF-8?B?'; |
|
41
|
|
|
$suffix = '?='; |
|
42
|
|
|
|
|
43
|
|
|
return $prefix . \str_replace("=\r\n", $suffix . "\r\n " . $prefix, $encoded) . $suffix; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
5 |
|
public function render(MessageInterface $message): Email |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
5 |
|
$view = $this->views->get($message->getSubject()); |
|
50
|
1 |
|
} catch (ViewException $e) { |
|
51
|
1 |
|
throw new MailerException( |
|
52
|
1 |
|
\sprintf('Invalid email template `%s`: %s', $message->getSubject(), $e->getMessage()), |
|
53
|
1 |
|
$e->getCode(), |
|
54
|
1 |
|
$e, |
|
55
|
1 |
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
$email = new Email(); |
|
59
|
|
|
|
|
60
|
4 |
|
if ($message->getFrom() !== null) { |
|
61
|
1 |
|
$email->from($message->getFrom()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
4 |
|
if ($message->getReplyTo() !== null) { |
|
65
|
1 |
|
$email->replyTo($message->getReplyTo()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
$email->to(...$message->getTo()); |
|
69
|
4 |
|
$email->cc(...$message->getCC()); |
|
70
|
4 |
|
$email->bcc(...$message->getBCC()); |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
4 |
|
$cloneMessage = clone $message; |
|
74
|
4 |
|
$this->eventDispatcher?->dispatch(new PreRender(message: $cloneMessage, email: $email)); |
|
75
|
|
|
// render message partials |
|
76
|
4 |
|
$view->render(\array_merge(['_msg_' => $email], $cloneMessage->getData())); |
|
77
|
4 |
|
$this->eventDispatcher?->dispatch(new PostRender(message: $cloneMessage, email: $email)); |
|
78
|
|
|
} catch (ViewException $e) { |
|
79
|
|
|
throw new MailerException( |
|
80
|
|
|
\sprintf('Unable to render email `%s`: %s', $message->getSubject(), $e->getMessage()), |
|
81
|
|
|
$e->getCode(), |
|
82
|
|
|
$e, |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
return $email; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|