Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Mail;
4
 
5
use Illuminate\Contracts\Mail\Attachable;
6
use Illuminate\Support\Str;
7
use Illuminate\Support\Traits\ForwardsCalls;
8
use Symfony\Component\Mime\Address;
9
use Symfony\Component\Mime\Email;
10
 
11
/**
12
 * @mixin \Symfony\Component\Mime\Email
13
 */
14
class Message
15
{
16
    use ForwardsCalls;
17
 
18
    /**
19
     * The Symfony Email instance.
20
     *
21
     * @var \Symfony\Component\Mime\Email
22
     */
23
    protected $message;
24
 
25
    /**
26
     * CIDs of files embedded in the message.
27
     *
28
     * @deprecated Will be removed in a future Laravel version.
29
     *
30
     * @var array
31
     */
32
    protected $embeddedFiles = [];
33
 
34
    /**
35
     * Create a new message instance.
36
     *
37
     * @param  \Symfony\Component\Mime\Email  $message
38
     * @return void
39
     */
40
    public function __construct(Email $message)
41
    {
42
        $this->message = $message;
43
    }
44
 
45
    /**
46
     * Add a "from" address to the message.
47
     *
48
     * @param  string|array  $address
49
     * @param  string|null  $name
50
     * @return $this
51
     */
52
    public function from($address, $name = null)
53
    {
54
        is_array($address)
55
            ? $this->message->from(...$address)
56
            : $this->message->from(new Address($address, (string) $name));
57
 
58
        return $this;
59
    }
60
 
61
    /**
62
     * Set the "sender" of the message.
63
     *
64
     * @param  string|array  $address
65
     * @param  string|null  $name
66
     * @return $this
67
     */
68
    public function sender($address, $name = null)
69
    {
70
        is_array($address)
71
            ? $this->message->sender(...$address)
72
            : $this->message->sender(new Address($address, (string) $name));
73
 
74
        return $this;
75
    }
76
 
77
    /**
78
     * Set the "return path" of the message.
79
     *
80
     * @param  string  $address
81
     * @return $this
82
     */
83
    public function returnPath($address)
84
    {
85
        $this->message->returnPath($address);
86
 
87
        return $this;
88
    }
89
 
90
    /**
91
     * Add a recipient to the message.
92
     *
93
     * @param  string|array  $address
94
     * @param  string|null  $name
95
     * @param  bool  $override
96
     * @return $this
97
     */
98
    public function to($address, $name = null, $override = false)
99
    {
100
        if ($override) {
101
            is_array($address)
102
                ? $this->message->to(...$address)
103
                : $this->message->to(new Address($address, (string) $name));
104
 
105
            return $this;
106
        }
107
 
108
        return $this->addAddresses($address, $name, 'To');
109
    }
110
 
111
    /**
112
     * Remove all "to" addresses from the message.
113
     *
114
     * @return $this
115
     */
116
    public function forgetTo()
117
    {
118
        if ($header = $this->message->getHeaders()->get('To')) {
119
            $this->addAddressDebugHeader('X-To', $this->message->getTo());
120
 
121
            $header->setAddresses([]);
122
        }
123
 
124
        return $this;
125
    }
126
 
127
    /**
128
     * Add a carbon copy to the message.
129
     *
130
     * @param  string|array  $address
131
     * @param  string|null  $name
132
     * @param  bool  $override
133
     * @return $this
134
     */
135
    public function cc($address, $name = null, $override = false)
136
    {
137
        if ($override) {
138
            is_array($address)
139
                ? $this->message->cc(...$address)
140
                : $this->message->cc(new Address($address, (string) $name));
141
 
142
            return $this;
143
        }
144
 
145
        return $this->addAddresses($address, $name, 'Cc');
146
    }
147
 
148
    /**
149
     * Remove all carbon copy addresses from the message.
150
     *
151
     * @return $this
152
     */
153
    public function forgetCc()
154
    {
155
        if ($header = $this->message->getHeaders()->get('Cc')) {
156
            $this->addAddressDebugHeader('X-Cc', $this->message->getCC());
157
 
158
            $header->setAddresses([]);
159
        }
160
 
161
        return $this;
162
    }
163
 
164
    /**
165
     * Add a blind carbon copy to the message.
166
     *
167
     * @param  string|array  $address
168
     * @param  string|null  $name
169
     * @param  bool  $override
170
     * @return $this
171
     */
172
    public function bcc($address, $name = null, $override = false)
173
    {
174
        if ($override) {
175
            is_array($address)
176
                ? $this->message->bcc(...$address)
177
                : $this->message->bcc(new Address($address, (string) $name));
178
 
179
            return $this;
180
        }
181
 
182
        return $this->addAddresses($address, $name, 'Bcc');
183
    }
184
 
185
    /**
186
     * Remove all of the blind carbon copy addresses from the message.
187
     *
188
     * @return $this
189
     */
190
    public function forgetBcc()
191
    {
192
        if ($header = $this->message->getHeaders()->get('Bcc')) {
193
            $this->addAddressDebugHeader('X-Bcc', $this->message->getBcc());
194
 
195
            $header->setAddresses([]);
196
        }
197
 
198
        return $this;
199
    }
200
 
201
    /**
202
     * Add a "reply to" address to the message.
203
     *
204
     * @param  string|array  $address
205
     * @param  string|null  $name
206
     * @return $this
207
     */
208
    public function replyTo($address, $name = null)
209
    {
210
        return $this->addAddresses($address, $name, 'ReplyTo');
211
    }
212
 
213
    /**
214
     * Add a recipient to the message.
215
     *
216
     * @param  string|array  $address
217
     * @param  string  $name
218
     * @param  string  $type
219
     * @return $this
220
     */
221
    protected function addAddresses($address, $name, $type)
222
    {
223
        if (is_array($address)) {
224
            $type = lcfirst($type);
225
 
226
            $addresses = collect($address)->map(function ($address, $key) {
227
                if (is_string($key) && is_string($address)) {
228
                    return new Address($key, $address);
229
                }
230
 
231
                if (is_array($address)) {
232
                    return new Address($address['email'] ?? $address['address'], $address['name'] ?? null);
233
                }
234
 
235
                if (is_null($address)) {
236
                    return new Address($key);
237
                }
238
 
239
                return $address;
240
            })->all();
241
 
242
            $this->message->{"{$type}"}(...$addresses);
243
        } else {
244
            $this->message->{"add{$type}"}(new Address($address, (string) $name));
245
        }
246
 
247
        return $this;
248
    }
249
 
250
    /**
251
     * Add an address debug header for a list of recipients.
252
     *
253
     * @param  string  $header
254
     * @param  \Symfony\Component\Mime\Address[]  $addresses
255
     * @return $this
256
     */
257
    protected function addAddressDebugHeader(string $header, array $addresses)
258
    {
259
        $this->message->getHeaders()->addTextHeader(
260
            $header,
261
            implode(', ', array_map(fn ($a) => $a->toString(), $addresses)),
262
        );
263
 
264
        return $this;
265
    }
266
 
267
    /**
268
     * Set the subject of the message.
269
     *
270
     * @param  string  $subject
271
     * @return $this
272
     */
273
    public function subject($subject)
274
    {
275
        $this->message->subject($subject);
276
 
277
        return $this;
278
    }
279
 
280
    /**
281
     * Set the message priority level.
282
     *
283
     * @param  int  $level
284
     * @return $this
285
     */
286
    public function priority($level)
287
    {
288
        $this->message->priority($level);
289
 
290
        return $this;
291
    }
292
 
293
    /**
294
     * Attach a file to the message.
295
     *
296
     * @param  string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment  $file
297
     * @param  array  $options
298
     * @return $this
299
     */
300
    public function attach($file, array $options = [])
301
    {
302
        if ($file instanceof Attachable) {
303
            $file = $file->toMailAttachment();
304
        }
305
 
306
        if ($file instanceof Attachment) {
307
            return $file->attachTo($this);
308
        }
309
 
310
        $this->message->attachFromPath($file, $options['as'] ?? null, $options['mime'] ?? null);
311
 
312
        return $this;
313
    }
314
 
315
    /**
316
     * Attach in-memory data as an attachment.
317
     *
621 lars 318
     * @param  string|resource  $data
148 lars 319
     * @param  string  $name
320
     * @param  array  $options
321
     * @return $this
322
     */
323
    public function attachData($data, $name, array $options = [])
324
    {
325
        $this->message->attach($data, $name, $options['mime'] ?? null);
326
 
327
        return $this;
328
    }
329
 
330
    /**
331
     * Embed a file in the message and get the CID.
332
     *
333
     * @param  string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment  $file
334
     * @return string
335
     */
336
    public function embed($file)
337
    {
338
        if ($file instanceof Attachable) {
339
            $file = $file->toMailAttachment();
340
        }
341
 
342
        if ($file instanceof Attachment) {
343
            return $file->attachWith(
344
                function ($path) use ($file) {
345
                    $cid = $file->as ?? Str::random();
346
 
347
                    $this->message->embedFromPath($path, $cid, $file->mime);
348
 
349
                    return "cid:{$cid}";
350
                },
351
                function ($data) use ($file) {
352
                    $this->message->embed($data(), $file->as, $file->mime);
353
 
354
                    return "cid:{$file->as}";
355
                }
356
            );
357
        }
358
 
359
        $cid = Str::random(10);
360
 
361
        $this->message->embedFromPath($file, $cid);
362
 
363
        return "cid:$cid";
364
    }
365
 
366
    /**
367
     * Embed in-memory data in the message and get the CID.
368
     *
621 lars 369
     * @param  string|resource  $data
148 lars 370
     * @param  string  $name
371
     * @param  string|null  $contentType
372
     * @return string
373
     */
374
    public function embedData($data, $name, $contentType = null)
375
    {
376
        $this->message->embed($data, $name, $contentType);
377
 
378
        return "cid:$name";
379
    }
380
 
381
    /**
382
     * Get the underlying Symfony Email instance.
383
     *
384
     * @return \Symfony\Component\Mime\Email
385
     */
386
    public function getSymfonyMessage()
387
    {
388
        return $this->message;
389
    }
390
 
391
    /**
392
     * Dynamically pass missing methods to the Symfony instance.
393
     *
394
     * @param  string  $method
395
     * @param  array  $parameters
396
     * @return mixed
397
     */
398
    public function __call($method, $parameters)
399
    {
400
        return $this->forwardDecoratedCallTo($this->message, $method, $parameters);
401
    }
402
}