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