-
Notifications
You must be signed in to change notification settings - Fork 11.2k
/
Copy pathEnvelope.php
369 lines (325 loc) · 8.85 KB
/
Envelope.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
namespace Illuminate\Mail\Mailables;
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Conditionable;
class Envelope
{
use Conditionable;
/**
* The address sending the message.
*
* @var \Illuminate\Mail\Mailables\Address|string|null
*/
public $from;
/**
* The recipients of the message.
*
* @var array
*/
public $to;
/**
* The recipients receiving a copy of the message.
*
* @var array
*/
public $cc;
/**
* The recipients receiving a blind copy of the message.
*
* @var array
*/
public $bcc;
/**
* The recipients that should be replied to.
*
* @var array
*/
public $replyTo;
/**
* The subject of the message.
*
* @var string|null
*/
public $subject;
/**
* The message's tags.
*
* @var array
*/
public $tags = [];
/**
* The message's meta data.
*
* @var array
*/
public $metadata = [];
/**
* The message's Symfony Message customization callbacks.
*
* @var array
*/
public $using = [];
/**
* Create a new message envelope instance.
*
* @param \Illuminate\Mail\Mailables\Address|string|null $from
* @param array $to
* @param array $cc
* @param array $bcc
* @param array $replyTo
* @param string|null $subject
* @param array $tags
* @param array $metadata
* @param \Closure|array $using
* @return void
*
* @named-arguments-supported
*/
public function __construct(Address|string $from = null, $to = [], $cc = [], $bcc = [], $replyTo = [], string $subject = null, array $tags = [], array $metadata = [], Closure|array $using = [])
{
$this->from = is_string($from) ? new Address($from) : $from;
$this->to = $this->normalizeAddresses($to);
$this->cc = $this->normalizeAddresses($cc);
$this->bcc = $this->normalizeAddresses($bcc);
$this->replyTo = $this->normalizeAddresses($replyTo);
$this->subject = $subject;
$this->tags = $tags;
$this->metadata = $metadata;
$this->using = Arr::wrap($using);
}
/**
* Normalize the given array of addresses.
*
* @param array $addresses
* @return array
*/
protected function normalizeAddresses($addresses)
{
return collect($addresses)->map(function ($address) {
return is_string($address) ? new Address($address) : $address;
})->all();
}
/**
* Specify who the message will be "from".
*
* @param \Illuminate\Mail\Mailables\Address|string $address
* @param string|null $name
* @return $this
*/
public function from(Address|string $address, $name = null)
{
$this->from = is_string($address) ? new Address($address, $name) : $address;
return $this;
}
/**
* Add a "to" recipient to the message envelope.
*
* @param \Illuminate\Mail\Mailables\Address|array|string $address
* @param string|null $name
* @return $this
*/
public function to(Address|array|string $address, $name = null)
{
$this->to = array_merge($this->to, $this->normalizeAddresses(
is_string($name) ? [new Address($address, $name)] : Arr::wrap($address),
));
return $this;
}
/**
* Add a "cc" recipient to the message envelope.
*
* @param \Illuminate\Mail\Mailables\Address|array|string $address
* @param string|null $name
* @return $this
*/
public function cc(Address|array|string $address, $name = null)
{
$this->cc = array_merge($this->cc, $this->normalizeAddresses(
is_string($name) ? [new Address($address, $name)] : Arr::wrap($address),
));
return $this;
}
/**
* Add a "bcc" recipient to the message envelope.
*
* @param \Illuminate\Mail\Mailables\Address|array|string $address
* @param string|null $name
* @return $this
*/
public function bcc(Address|array|string $address, $name = null)
{
$this->bcc = array_merge($this->bcc, $this->normalizeAddresses(
is_string($name) ? [new Address($address, $name)] : Arr::wrap($address),
));
return $this;
}
/**
* Add a "reply to" recipient to the message envelope.
*
* @param \Illuminate\Mail\Mailables\Address|array|string $address
* @param string|null $name
* @return $this
*/
public function replyTo(Address|array|string $address, $name = null)
{
$this->replyTo = array_merge($this->replyTo, $this->normalizeAddresses(
is_string($name) ? [new Address($address, $name)] : Arr::wrap($address),
));
return $this;
}
/**
* Set the subject of the message.
*
* @param string $subject
* @return $this
*/
public function subject(string $subject)
{
$this->subject = $subject;
return $this;
}
/**
* Add "tags" to the message.
*
* @param array $tags
* @return $this
*/
public function tags(array $tags)
{
$this->tags = array_merge($this->tags, $tags);
return $this;
}
/**
* Add a "tag" to the message.
*
* @param string $tag
* @return $this
*/
public function tag(string $tag)
{
$this->tags[] = $tag;
return $this;
}
/**
* Add metadata to the message.
*
* @param string $key
* @param string|int $value
* @return $this
*/
public function metadata(string $key, string|int $value)
{
$this->metadata[$key] = $value;
return $this;
}
/**
* Add a Symfony Message customization callback to the message.
*
* @param \Closure $callback
* @return $this
*/
public function using(Closure $callback)
{
$this->using[] = $callback;
return $this;
}
/**
* Determine if the message is from the given address.
*
* @param string $address
* @param string|null $name
* @return bool
*/
public function isFrom(string $address, string $name = null)
{
if (is_null($name)) {
return $this->from->address === $address;
}
return $this->from->address === $address &&
$this->from->name === $name;
}
/**
* Determine if the message has the given address as a recipient.
*
* @param string $address
* @param string|null $name
* @return bool
*/
public function hasTo(string $address, string $name = null)
{
return $this->hasRecipient($this->to, $address, $name);
}
/**
* Determine if the message has the given address as a "cc" recipient.
*
* @param string $address
* @param string|null $name
* @return bool
*/
public function hasCc(string $address, string $name = null)
{
return $this->hasRecipient($this->cc, $address, $name);
}
/**
* Determine if the message has the given address as a "bcc" recipient.
*
* @param string $address
* @param string|null $name
* @return bool
*/
public function hasBcc(string $address, string $name = null)
{
return $this->hasRecipient($this->bcc, $address, $name);
}
/**
* Determine if the message has the given address as a "reply to" recipient.
*
* @param string $address
* @param string|null $name
* @return bool
*/
public function hasReplyTo(string $address, string $name = null)
{
return $this->hasRecipient($this->replyTo, $address, $name);
}
/**
* Determine if the message has the given recipient.
*
* @param array $recipients
* @param string $address
* @param string|null $name
* @return bool
*/
protected function hasRecipient(array $recipients, string $address, ?string $name = null)
{
return collect($recipients)->contains(function ($recipient) use ($address, $name) {
if (is_null($name)) {
return $recipient->address === $address;
}
return $recipient->address === $address &&
$recipient->name === $name;
});
}
/**
* Determine if the message has the given subject.
*
* @param string $subject
* @return bool
*/
public function hasSubject(string $subject)
{
return $this->subject === $subject;
}
/**
* Determine if the message has the given metadata.
*
* @param string $key
* @param string $value
* @return bool
*/
public function hasMetadata(string $key, string $value)
{
return isset($this->metadata[$key]) && (string) $this->metadata[$key] === $value;
}
}