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