forked from dotkernel/dot-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailService.php
More file actions
174 lines (145 loc) · 4.75 KB
/
MailService.php
File metadata and controls
174 lines (145 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
namespace Dot\Mail\Service;
use Dot\Mail\Email;
use Dot\Mail\Event\MailEvent;
use Dot\Mail\Event\MailEventListenerAwareInterface;
use Dot\Mail\Event\MailEventListenerAwareTrait;
use Dot\Mail\Exception\MailException;
use Dot\Mail\Options\MailOptions;
use Dot\Mail\Result\MailResult;
use Dot\Mail\Result\ResultInterface;
use Exception;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Mime\Part\AbstractPart;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\MixedPart;
use function array_merge;
use function basename;
use function count;
use function fopen;
use function is_file;
use function is_string;
class MailService implements MailServiceInterface, MailEventListenerAwareInterface
{
use MailEventListenerAwareTrait;
protected LogServiceInterface $logService;
protected Email $message;
protected TransportInterface $transport;
protected MailOptions $mailOptions;
protected array $attachments = [];
public function __construct(
LogServiceInterface $logService,
Email $message,
TransportInterface $transport,
MailOptions $mailOptions
) {
$this->logService = $logService;
$this->message = $message;
$this->transport = $transport;
$this->mailOptions = $mailOptions;
}
/**
* @throws MailException|TransportExceptionInterface
*/
public function send(): ResultInterface
{
$result = new MailResult();
$this->message->setEncoding('utf-8');
try {
$this->getEventManager()->triggerEvent($this->createMailEvent());
//attach files before sending
$this->attachFiles();
$this->getTransport()->send($this->getMessage());
$this->getMessage()->setBody(null);
$this->getEventManager()->triggerEvent($this->createMailEvent(MailEvent::EVENT_MAIL_POST_SEND, $result));
} catch (Exception $e) {
$result = $this->createMailResultFromException($e);
//trigger error event
$this->getEventManager()->triggerEvent($this->createMailEvent(MailEvent::EVENT_MAIL_SEND_ERROR, $result));
throw new MailException($result->getMessage());
}
if ($result->isValid()) {
$this->logService->sent($this->getMessage());
}
return $result;
}
public function createMailEvent(
string $name = MailEvent::EVENT_MAIL_PRE_SEND,
?ResultInterface $result = null
): MailEvent {
$event = new MailEvent($this, $name);
if (isset($result)) {
$event->setResult($result);
}
return $event;
}
public function attachFiles(): false|Email
{
if (count($this->attachments) === 0) {
return false;
}
$body = $this->message->getBody();
$parts = [];
foreach ($this->attachments as $key => $attachment) {
if (! is_file($attachment)) {
continue;
}
$basename = is_string($key) ? $key : basename($attachment);
$parts[] = new DataPart(fopen($attachment, 'r'), $basename);
}
if (count($parts) > 0) {
$this->message->setBody(new MixedPart($body, ...$parts));
}
return $this->message;
}
public function setBody(string|AbstractPart $body, ?string $charset = null): void
{
if (is_string($body)) {
$this->message->html($body);
} else {
$this->message->setBody($body);
}
}
public function createMailResultFromException(Exception $e): ResultInterface
{
return new MailResult(false, $e->getMessage(), $e);
}
public function getMessage(): Email
{
return $this->message;
}
public function setSubject(string $subject): void
{
$this->message->subject($subject);
}
public function addAttachment(string $path, ?string $filename = null): void
{
if (isset($filename)) {
$this->attachments[$filename] = $path;
} else {
$this->attachments[] = $path;
}
}
public function addAttachments(array $paths): void
{
$this->setAttachments(array_merge($this->attachments, $paths));
}
public function getAttachments(): array
{
return $this->attachments;
}
public function setAttachments(array $paths): void
{
$this->attachments = $paths;
}
public function getTransport(): TransportInterface
{
return $this->transport;
}
public function setTransport(TransportInterface $transport): void
{
$this->transport = $transport;
}
}