1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nip\MailModule\Models\EmailsTable; |
5
|
|
|
|
6
|
|
|
use ByTIC\DataObjects\Behaviors\Timestampable\TimestampableTrait; |
7
|
|
|
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait; |
8
|
|
|
use Nip\Mail\Message; |
9
|
|
|
use Nip\Mail\Models\Mailable\RecordTrait as MailableRecordTrait; |
10
|
|
|
use Nip\MailModule\Models\EmailsTable\Traits\MergeTags\MergeTagsRecordTrait; |
11
|
|
|
use Nip\Records\AbstractModels\Record; |
12
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait EmailTrait |
16
|
|
|
* @package Nip\Mail\Models\EmailsTable |
17
|
|
|
* |
18
|
|
|
* @property int $id_item |
19
|
|
|
* @property string $type |
20
|
|
|
* @property string $smtp_host |
21
|
|
|
* @property string $smtp_user |
22
|
|
|
* @property string $smtp_password |
23
|
|
|
* @property string $to |
24
|
|
|
* @property string $reply_to |
25
|
|
|
* @property string $bcc |
26
|
|
|
* @property string $subject |
27
|
|
|
* @property string $compiled_subject |
28
|
|
|
* @property string $body |
29
|
|
|
* @property string $compiled_body |
30
|
|
|
* @property string $vars |
31
|
|
|
* @property string $is_html |
32
|
|
|
* @property string $sent |
33
|
|
|
* @property string $date_sent |
34
|
|
|
* @property string $created |
35
|
|
|
*/ |
36
|
1 |
|
trait EmailTrait |
37
|
1 |
|
{ |
38
|
1 |
|
use MailableRecordTrait; |
39
|
|
|
use HasMediaTrait; |
40
|
|
|
use MergeTagsRecordTrait; |
41
|
|
|
use TimestampableTrait; |
42
|
|
|
|
43
|
|
|
public ?string $body = null; |
44
|
|
|
|
45
|
|
|
public ?string $from = ''; |
46
|
|
|
public ?string $from_name = ''; |
47
|
|
|
|
48
|
|
|
public ?string $subject = ''; |
49
|
|
|
|
50
|
|
|
protected static $createTimestamps = 'created'; |
51
|
|
|
|
52
|
|
|
public function populateFromConfig() |
53
|
|
|
{ |
54
|
|
|
$config = app('config'); |
55
|
|
|
$this->from = $config->get('mail.from.address'); |
56
|
|
|
$this->from_name = $config->get('mail.from.name'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Record $item |
61
|
|
|
*/ |
62
|
|
|
public function populateFromItem($item) |
63
|
|
|
{ |
64
|
|
|
$this->id_item = $item->id; |
65
|
|
|
$this->type = inflector()->singularize($item->getManager()->getTable()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
* Used to decode html entities to proper chars |
71
|
|
|
*/ |
72
|
|
|
public function getFrom() |
73
|
|
|
{ |
74
|
|
|
if (empty($this->getPropertyRaw('from'))) { |
|
|
|
|
75
|
|
|
$config = app('config'); |
76
|
|
|
$this->from = $config->get('mail.from.address'); |
77
|
|
|
$this->from_name = $config->get('mail.from.name'); |
78
|
|
|
} |
79
|
|
|
return html_entity_decode($this->from_name, ENT_QUOTES) . ' <' . $this->from . '>'; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getPreviewBody() |
86
|
|
|
{ |
87
|
|
|
return $this->getBody(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getBody() |
94
|
|
|
{ |
95
|
|
|
return $this->body; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getSubject(): ?string |
102
|
|
|
{ |
103
|
|
|
return $this->subject; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function getTos() |
110
|
|
|
{ |
111
|
|
|
$emailsTos = []; |
112
|
|
|
if (preg_match_all('/\s*"?([^><,"]+)"?\s*((?:<[^><,]+>)?)\s*/', (string)$this->to, $matches, |
113
|
|
|
PREG_SET_ORDER) > 0) { |
114
|
|
|
foreach ($matches as $m) { |
115
|
|
|
if (!empty($m[2])) { |
116
|
|
|
$emailsTos[trim($m[2], '<>')] = html_entity_decode($m[1]); |
117
|
|
|
} else { |
118
|
|
|
$emailsTos[$m[1]] = ''; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $emailsTos; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function getReplyTos() |
130
|
|
|
{ |
131
|
|
|
if (empty($this->reply_to)) { |
132
|
|
|
return null; |
133
|
|
|
} |
134
|
|
|
return [$this->reply_to => html_entity_decode($this->from_name, ENT_QUOTES)]; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function getBccTos() |
141
|
|
|
{ |
142
|
|
|
if (empty($this->bcc)) { |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
return [$this->bcc => '']; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function delete() |
149
|
|
|
{ |
150
|
|
|
$this->clearAttachments(); |
151
|
|
|
parent::delete(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function clearAttachments() |
155
|
|
|
{ |
156
|
|
|
$this->getFiles()->delete(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param Message $message |
161
|
|
|
*/ |
162
|
|
|
public function buildMailMessageAttachments(&$message) |
163
|
|
|
{ |
164
|
|
|
$emailFiles = $this->getFiles(); |
165
|
|
|
foreach ($emailFiles as $emailFile) { |
166
|
|
|
$message->attachFromContent($emailFile->read(), $emailFile->getName()); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return array|null |
172
|
|
|
*/ |
173
|
|
|
protected function getCustomArgs(): ?array |
174
|
|
|
{ |
175
|
|
|
$args = []; |
176
|
|
|
$args['category'] = $this->type; |
177
|
|
|
$args['id_email'] = $this->id; |
178
|
|
|
|
179
|
|
|
return $args; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param null $value |
|
|
|
|
184
|
|
|
* @return bool |
185
|
|
|
* @noinspection PhpUnused |
186
|
|
|
* @noinspection PhpMethodNamingConventionInspection |
187
|
|
|
*/ |
188
|
|
|
public function IsHTML($value = null) |
189
|
|
|
{ |
190
|
|
|
if (is_bool($value)) { |
|
|
|
|
191
|
|
|
$this->is_html = $value ? 'yes' : 'no'; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $this->is_html == 'yes'; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return mixed |
199
|
|
|
*/ |
200
|
|
|
public function getActivitiesByEmail() |
201
|
|
|
{ |
202
|
|
|
if (!$this->getRegistry()->exists('activities-email')) { |
|
|
|
|
203
|
|
|
$actEmail = []; |
204
|
|
|
$activities = $this->getActivities(); |
|
|
|
|
205
|
|
|
foreach ($activities as $activity) { |
206
|
|
|
$actEmail[$activity->email][] = $activity; |
207
|
|
|
} |
208
|
|
|
$this->getRegistry()->set('activities-email', $actEmail); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $this->getRegistry()->get('activities-email'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return mixed |
216
|
|
|
*/ |
217
|
|
|
public function getLinksByEmail() |
218
|
|
|
{ |
219
|
|
|
if (!$this->getRegistry()->exists('links-email')) { |
220
|
|
|
$linksEmail = []; |
|
|
|
|
221
|
|
|
$links = $this->getLinks(); |
|
|
|
|
222
|
|
|
foreach ($links as $link) { |
223
|
|
|
$actEmail[$link->url][$link->email] = $link; |
224
|
|
|
} |
225
|
|
|
$this->getRegistry()->set('links-email', $actEmail); |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $this->getRegistry()->get('links-email'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param MailerInterface $mailer |
233
|
|
|
* @param Message $message |
234
|
|
|
* @param $response |
235
|
|
|
*/ |
236
|
|
|
protected function afterSend($mailer, $message) |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
$this->sent = 'yes'; |
239
|
|
|
$this->smtp_user = ''; |
240
|
|
|
$this->smtp_host = ''; |
241
|
|
|
$this->smtp_password = ''; |
242
|
|
|
// $this->subject = ''; |
243
|
|
|
// $this->body = ''; |
244
|
|
|
// $this->vars = ''; |
245
|
|
|
$this->date_sent = date('Y-m-d H:i:s'); |
246
|
|
|
$this->update(); |
|
|
|
|
247
|
|
|
|
248
|
|
|
$this->clearAttachments(); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|