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