|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace h4kuna\MailManager; |
|
4
|
|
|
|
|
5
|
|
|
use h4kuna\MailManager\Exceptions\DirectoryNotFound; |
|
6
|
|
|
use h4kuna\MailManager\Message; |
|
7
|
|
|
use Nette\Application\UI\ITemplate; |
|
8
|
|
|
use Nette\Mail; |
|
9
|
|
|
|
|
10
|
|
|
class MailManager |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** @var Mail\IMailer */ |
|
14
|
|
|
private $mailer; |
|
15
|
|
|
|
|
16
|
|
|
/** @var Message\IMessageFactory */ |
|
17
|
|
|
private $messageFactory; |
|
18
|
|
|
|
|
19
|
|
|
/** @var Template\LayoutFactory */ |
|
20
|
|
|
private $layoutFactory; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $assetsDir = ''; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
Mail\IMailer $mailer, |
|
28
|
|
|
Message\IMessageFactory $messageFactory, |
|
29
|
|
|
Template\LayoutFactory $layoutFactory |
|
30
|
|
|
) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->mailer = $mailer; |
|
33
|
|
|
$this->messageFactory = $messageFactory; |
|
34
|
|
|
$this->layoutFactory = $layoutFactory; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
public function getLastTemplate(): Template\Layout |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->layoutFactory->getLastLayout(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
public function setAssetsDir(string $path): void |
|
45
|
|
|
{ |
|
46
|
|
|
$dir = realpath($path); |
|
47
|
|
|
if ($dir === false) { |
|
48
|
|
|
throw new DirectoryNotFound($path); |
|
49
|
|
|
} |
|
50
|
|
|
$this->assetsDir = $dir; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string|ITemplate|Template\Layout $body content or filepath |
|
56
|
|
|
* @param mixed[] $data |
|
57
|
|
|
*/ |
|
58
|
|
|
public function createMessage($body, array $data = []): Mail\Message |
|
59
|
|
|
{ |
|
60
|
|
|
if ($body instanceof Template\Layout) { |
|
61
|
|
|
$layout = $body; |
|
62
|
|
|
} else { |
|
63
|
|
|
$layout = $this->layoutFactory->createHtml($body); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$message = $this->messageFactory->create(); |
|
67
|
|
|
$layout->bindMessage($message, $data, $this->assetsDir); |
|
68
|
|
|
return $message; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
public function getLastLayout(): Template\Layout |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->layoutFactory->getLastLayout(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
public function send(Mail\Message $message): void |
|
79
|
|
|
{ |
|
80
|
|
|
$this->mailer->send($message); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Plain message generated by function mail |
|
86
|
|
|
*/ |
|
87
|
|
|
public function createSystemMail(string $content): Message\SystemMessage |
|
88
|
|
|
{ |
|
89
|
|
|
$message = $this->messageFactory->createSystemMessage(); |
|
90
|
|
|
$message->setBody($content); |
|
91
|
|
|
return $message; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|