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