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