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