Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Notifications\Messages;
4
 
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\Mail\Attachable;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Contracts\Support\Renderable;
9
use Illuminate\Mail\Attachment;
10
use Illuminate\Mail\Markdown;
11
use Illuminate\Support\Traits\Conditionable;
12
 
13
class MailMessage extends SimpleMessage implements Renderable
14
{
15
    use Conditionable;
16
 
17
    /**
18
     * The view to be rendered.
19
     *
20
     * @var array|string
21
     */
22
    public $view;
23
 
24
    /**
25
     * The view data for the message.
26
     *
27
     * @var array
28
     */
29
    public $viewData = [];
30
 
31
    /**
32
     * The Markdown template to render (if applicable).
33
     *
34
     * @var string|null
35
     */
36
    public $markdown = 'notifications::email';
37
 
38
    /**
39
     * The current theme being used when generating emails.
40
     *
41
     * @var string|null
42
     */
43
    public $theme;
44
 
45
    /**
46
     * The "from" information for the message.
47
     *
48
     * @var array
49
     */
50
    public $from = [];
51
 
52
    /**
53
     * The "reply to" information for the message.
54
     *
55
     * @var array
56
     */
57
    public $replyTo = [];
58
 
59
    /**
60
     * The "cc" information for the message.
61
     *
62
     * @var array
63
     */
64
    public $cc = [];
65
 
66
    /**
67
     * The "bcc" information for the message.
68
     *
69
     * @var array
70
     */
71
    public $bcc = [];
72
 
73
    /**
74
     * The attachments for the message.
75
     *
76
     * @var array
77
     */
78
    public $attachments = [];
79
 
80
    /**
81
     * The raw attachments for the message.
82
     *
83
     * @var array
84
     */
85
    public $rawAttachments = [];
86
 
87
    /**
88
     * The tags for the message.
89
     *
90
     * @var array
91
     */
92
    public $tags = [];
93
 
94
    /**
95
     * The metadata for the message.
96
     *
97
     * @var array
98
     */
99
    public $metadata = [];
100
 
101
    /**
102
     * Priority level of the message.
103
     *
104
     * @var int
105
     */
106
    public $priority;
107
 
108
    /**
109
     * The callbacks for the message.
110
     *
111
     * @var array
112
     */
113
    public $callbacks = [];
114
 
115
    /**
116
     * Set the view for the mail message.
117
     *
118
     * @param  array|string  $view
119
     * @param  array  $data
120
     * @return $this
121
     */
122
    public function view($view, array $data = [])
123
    {
124
        $this->view = $view;
125
        $this->viewData = $data;
126
 
127
        $this->markdown = null;
128
 
129
        return $this;
130
    }
131
 
132
    /**
133
     * Set the Markdown template for the notification.
134
     *
135
     * @param  string  $view
136
     * @param  array  $data
137
     * @return $this
138
     */
139
    public function markdown($view, array $data = [])
140
    {
141
        $this->markdown = $view;
142
        $this->viewData = $data;
143
 
144
        $this->view = null;
145
 
146
        return $this;
147
    }
148
 
149
    /**
150
     * Set the default markdown template.
151
     *
152
     * @param  string  $template
153
     * @return $this
154
     */
155
    public function template($template)
156
    {
157
        $this->markdown = $template;
158
 
159
        return $this;
160
    }
161
 
162
    /**
163
     * Set the theme to use with the Markdown template.
164
     *
165
     * @param  string  $theme
166
     * @return $this
167
     */
168
    public function theme($theme)
169
    {
170
        $this->theme = $theme;
171
 
172
        return $this;
173
    }
174
 
175
    /**
176
     * Set the from address for the mail message.
177
     *
178
     * @param  string  $address
179
     * @param  string|null  $name
180
     * @return $this
181
     */
182
    public function from($address, $name = null)
183
    {
184
        $this->from = [$address, $name];
185
 
186
        return $this;
187
    }
188
 
189
    /**
190
     * Set the "reply to" address of the message.
191
     *
192
     * @param  array|string  $address
193
     * @param  string|null  $name
194
     * @return $this
195
     */
196
    public function replyTo($address, $name = null)
197
    {
198
        if ($this->arrayOfAddresses($address)) {
199
            $this->replyTo += $this->parseAddresses($address);
200
        } else {
201
            $this->replyTo[] = [$address, $name];
202
        }
203
 
204
        return $this;
205
    }
206
 
207
    /**
208
     * Set the cc address for the mail message.
209
     *
210
     * @param  array|string  $address
211
     * @param  string|null  $name
212
     * @return $this
213
     */
214
    public function cc($address, $name = null)
215
    {
216
        if ($this->arrayOfAddresses($address)) {
217
            $this->cc += $this->parseAddresses($address);
218
        } else {
219
            $this->cc[] = [$address, $name];
220
        }
221
 
222
        return $this;
223
    }
224
 
225
    /**
226
     * Set the bcc address for the mail message.
227
     *
228
     * @param  array|string  $address
229
     * @param  string|null  $name
230
     * @return $this
231
     */
232
    public function bcc($address, $name = null)
233
    {
234
        if ($this->arrayOfAddresses($address)) {
235
            $this->bcc += $this->parseAddresses($address);
236
        } else {
237
            $this->bcc[] = [$address, $name];
238
        }
239
 
240
        return $this;
241
    }
242
 
243
    /**
244
     * Attach a file to the message.
245
     *
246
     * @param  string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment  $file
247
     * @param  array  $options
248
     * @return $this
249
     */
250
    public function attach($file, array $options = [])
251
    {
252
        if ($file instanceof Attachable) {
253
            $file = $file->toMailAttachment();
254
        }
255
 
256
        if ($file instanceof Attachment) {
257
            return $file->attachTo($this);
258
        }
259
 
260
        $this->attachments[] = compact('file', 'options');
261
 
262
        return $this;
263
    }
264
 
265
    /**
266
     * Attach in-memory data as an attachment.
267
     *
268
     * @param  string  $data
269
     * @param  string  $name
270
     * @param  array  $options
271
     * @return $this
272
     */
273
    public function attachData($data, $name, array $options = [])
274
    {
275
        $this->rawAttachments[] = compact('data', 'name', 'options');
276
 
277
        return $this;
278
    }
279
 
280
    /**
281
     * Add a tag header to the message when supported by the underlying transport.
282
     *
283
     * @param  string  $value
284
     * @return $this
285
     */
286
    public function tag($value)
287
    {
288
        array_push($this->tags, $value);
289
 
290
        return $this;
291
    }
292
 
293
    /**
294
     * Add a metadata header to the message when supported by the underlying transport.
295
     *
296
     * @param  string  $key
297
     * @param  string  $value
298
     * @return $this
299
     */
300
    public function metadata($key, $value)
301
    {
302
        $this->metadata[$key] = $value;
303
 
304
        return $this;
305
    }
306
 
307
    /**
308
     * Set the priority of this message.
309
     *
310
     * The value is an integer where 1 is the highest priority and 5 is the lowest.
311
     *
312
     * @param  int  $level
313
     * @return $this
314
     */
315
    public function priority($level)
316
    {
317
        $this->priority = $level;
318
 
319
        return $this;
320
    }
321
 
322
    /**
323
     * Get the data array for the mail message.
324
     *
325
     * @return array
326
     */
327
    public function data()
328
    {
329
        return array_merge($this->toArray(), $this->viewData);
330
    }
331
 
332
    /**
333
     * Parse the multi-address array into the necessary format.
334
     *
335
     * @param  array  $value
336
     * @return array
337
     */
338
    protected function parseAddresses($value)
339
    {
340
        return collect($value)->map(function ($address, $name) {
341
            return [$address, is_numeric($name) ? null : $name];
342
        })->values()->all();
343
    }
344
 
345
    /**
346
     * Determine if the given "address" is actually an array of addresses.
347
     *
348
     * @param  mixed  $address
349
     * @return bool
350
     */
351
    protected function arrayOfAddresses($address)
352
    {
353
        return is_iterable($address) || $address instanceof Arrayable;
354
    }
355
 
356
    /**
357
     * Render the mail notification message into an HTML string.
358
     *
359
     * @return \Illuminate\Support\HtmlString
360
     */
361
    public function render()
362
    {
363
        if (isset($this->view)) {
364
            return Container::getInstance()->make('mailer')->render(
365
                $this->view, $this->data()
366
            );
367
        }
368
 
369
        $markdown = Container::getInstance()->make(Markdown::class);
370
 
371
        return $markdown->theme($this->theme ?: $markdown->getTheme())
372
                ->render($this->markdown, $this->data());
373
    }
374
 
375
    /**
376
     * Register a callback to be called with the Symfony message instance.
377
     *
378
     * @param  callable  $callback
379
     * @return $this
380
     */
381
    public function withSymfonyMessage($callback)
382
    {
383
        $this->callbacks[] = $callback;
384
 
385
        return $this;
386
    }
387
}