|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Distilleries\MailerSaver\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Swift_Mailer; |
|
6
|
|
|
use Illuminate\Mail\Mailer; |
|
7
|
|
|
use Illuminate\Events\Dispatcher; |
|
8
|
|
|
use Wpb\String_Blade_Compiler\Factory; |
|
9
|
|
|
use Distilleries\MailerSaver\Contracts\MailModelContract; |
|
10
|
|
|
use Illuminate\Contracts\Mail\Mailable as MailableContract; |
|
11
|
|
|
|
|
12
|
|
|
class Mail extends Mailer |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Mail model instance. |
|
16
|
|
|
* |
|
17
|
|
|
* @var \Distilleries\MailerSaver\Contracts\MailModelContract |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $model; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Configuration override data. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $override; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Mailersaver instance constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \Distilleries\MailerSaver\Contracts\MailModelContract $model |
|
32
|
|
|
* @param \Wpb\String_Blade_Compiler\Factory $views |
|
33
|
|
|
* @param \Swift_Mailer $swift |
|
34
|
|
|
* @param \Illuminate\Events\Dispatcher $events |
|
35
|
|
|
*/ |
|
36
|
12 |
|
public function __construct(MailModelContract $model, Factory $views, Swift_Mailer $swift, Dispatcher $events = null) |
|
37
|
|
|
{ |
|
38
|
12 |
|
$this->model = $model; |
|
39
|
|
|
|
|
40
|
12 |
|
$this->override = config('mailersaver.override'); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
12 |
|
parent::__construct($views, $swift, $events); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Render the given mail view. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $view |
|
49
|
|
|
* @param array $data |
|
50
|
|
|
* @return string |
|
51
|
|
|
* @throws \Throwable |
|
52
|
|
|
*/ |
|
53
|
12 |
|
protected function renderView($view, $data) |
|
54
|
|
|
{ |
|
55
|
12 |
|
$body = $this->model->getTemplate($view); |
|
56
|
12 |
|
$body = empty($body) ? $this->views->make($view, $data)->render() : $body; |
|
57
|
|
|
|
|
58
|
12 |
|
$subjectTemplate = $this->model->getSubject(); |
|
59
|
12 |
|
if (! empty($subjectTemplate)) { |
|
60
|
8 |
|
$subject = view([ |
|
|
|
|
|
|
61
|
8 |
|
'template' => $subjectTemplate, |
|
62
|
8 |
|
'cache_key' => uniqid() . rand(), |
|
63
|
8 |
|
'updated_at' => 0, |
|
64
|
8 |
|
], $data); |
|
65
|
8 |
|
$data['subject'] = $subject->render(); |
|
|
|
|
|
|
66
|
|
|
} else { |
|
67
|
12 |
|
$data['subject'] = $subjectTemplate; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
12 |
|
$data['body_mail'] = view([ |
|
|
|
|
|
|
71
|
12 |
|
'template' => $body, |
|
72
|
12 |
|
'cache_key' => uniqid(), |
|
73
|
12 |
|
'updated_at' => 0, |
|
74
|
12 |
|
], $data); |
|
75
|
|
|
|
|
76
|
12 |
|
return $this->views->make(config('mailersaver.template'), $data)->render(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Send contained mail instance. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $view |
|
83
|
|
|
* @param array $data |
|
84
|
|
|
* @param mixed $callback |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
12 |
|
public function send($view, array $data = [], $callback = null) |
|
88
|
|
|
{ |
|
89
|
12 |
|
if ($view instanceof MailableContract) { |
|
90
|
|
|
return $this->sendMailable($view); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// First we need to parse the view, which could either be a string or an array |
|
94
|
|
|
// containing both an HTML and plain text versions of the view which should |
|
95
|
|
|
// be used when sending an e-mail. We will extract both of them out here. |
|
96
|
12 |
|
[$view, $plain, $raw] = $this->parseView($view); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
12 |
|
$model = $this->model->initByTemplate($view); |
|
99
|
12 |
|
$template = $model->where('action', $view)->first(); |
|
100
|
12 |
|
$plain = ! empty($template) ? $template->getPlain() : $plain; |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
12 |
|
if (! empty($template)) { |
|
103
|
8 |
|
$this->model = $template; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
12 |
|
$data['message'] = $message = $this->createMessage(); |
|
107
|
|
|
|
|
108
|
|
|
// Once we have retrieved the view content for the e-mail we will set the body |
|
109
|
|
|
// of this message using the HTML type, which will provide a simple wrapper |
|
110
|
|
|
// to creating view based emails that are able to receive arrays of data. |
|
111
|
12 |
|
call_user_func($callback, $message); |
|
112
|
|
|
|
|
113
|
12 |
|
$this->addContent($message, $view, $plain, $raw, $data); |
|
114
|
|
|
|
|
115
|
12 |
|
$this->addSubject($message); |
|
116
|
12 |
|
$this->addCc($message); |
|
117
|
12 |
|
$this->addBcc($message); |
|
118
|
12 |
|
$this->overrideTo($message); |
|
119
|
|
|
|
|
120
|
|
|
// Next we will determine if the message should be sent. We give the developer |
|
121
|
|
|
// one final chance to stop this message and then we will send it to all of |
|
122
|
|
|
// its recipients. We will then fire the sent event for the sent message. |
|
123
|
12 |
|
$swiftMessage = $message->getSwiftMessage(); |
|
124
|
|
|
|
|
125
|
12 |
|
if ($this->shouldSendMessage($swiftMessage, $data)) { |
|
126
|
12 |
|
$this->sendSwiftMessage($swiftMessage); |
|
127
|
|
|
|
|
128
|
12 |
|
$this->dispatchSentEvent($message, $data); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set subject to message. |
|
134
|
|
|
* |
|
135
|
|
|
* @param \Illuminate\Mail\Message $message |
|
136
|
|
|
* @return void |
|
137
|
|
|
*/ |
|
138
|
12 |
|
public function addSubject($message) |
|
139
|
|
|
{ |
|
140
|
12 |
|
$subject = $this->model->getSubject(); |
|
141
|
12 |
|
if (! empty($subject)) { |
|
142
|
8 |
|
$message->setSubject($subject); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set CC addresses to message. |
|
148
|
|
|
* |
|
149
|
|
|
* @param \Illuminate\Mail\Message $message |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
12 |
|
public function addCc($message) |
|
153
|
|
|
{ |
|
154
|
12 |
|
$cc = $this->isOverride() ? $this->override['cc'] : (! empty($this->model) ? $this->model->getCc() : ''); |
|
155
|
12 |
|
if (! empty($cc)) { |
|
156
|
4 |
|
$message->setCc($cc); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Set BCC addresses to message. |
|
162
|
|
|
* |
|
163
|
|
|
* @param \Illuminate\Mail\Message $message |
|
164
|
|
|
* @return void |
|
165
|
|
|
*/ |
|
166
|
12 |
|
public function addBcc($message) |
|
167
|
|
|
{ |
|
168
|
12 |
|
$bcc = $this->isOverride() ? $this->override['bcc'] : $this->model->getBcc(); |
|
169
|
12 |
|
if (! empty($bcc)) { |
|
170
|
4 |
|
$message->setBcc($bcc); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Set TO addresses to message. |
|
176
|
|
|
* |
|
177
|
|
|
* @param \Illuminate\Mail\Message $message |
|
178
|
|
|
* @return void |
|
179
|
|
|
*/ |
|
180
|
12 |
|
public function overrideTo($message) |
|
181
|
|
|
{ |
|
182
|
12 |
|
$to = $this->isOverride() ? explode(',', $this->override['to']) : []; |
|
183
|
12 |
|
if (! empty($to)) { |
|
184
|
8 |
|
$message->setTo($to); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Return if mail configuration is supercharged. |
|
190
|
|
|
* |
|
191
|
|
|
* @return bool |
|
192
|
|
|
*/ |
|
193
|
12 |
|
public function isOverride() |
|
194
|
|
|
{ |
|
195
|
12 |
|
return (bool) $this->override['enabled']; |
|
196
|
|
|
} |
|
197
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..