-
Notifications
You must be signed in to change notification settings - Fork 27
/
Message.php
executable file
·412 lines (350 loc) · 9.86 KB
/
Message.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
namespace Illuminate\Mail;
use Illuminate\Contracts\Mail\Attachable;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;
/**
* @mixin \Symfony\Component\Mime\Email
*/
class Message
{
use ForwardsCalls;
/**
* The Symfony Email instance.
*
* @var \Symfony\Component\Mime\Email
*/
protected $message;
/**
* CIDs of files embedded in the message.
*
* @deprecated Will be removed in a future Laravel version.
*
* @var array
*/
protected $embeddedFiles = [];
/**
* Create a new message instance.
*
* @param \Symfony\Component\Mime\Email $message
* @return void
*/
public function __construct(Email $message)
{
$this->message = $message;
}
/**
* Add a "from" address to the message.
*
* @param string|array $address
* @param string|null $name
* @return $this
*/
public function from($address, $name = null)
{
is_array($address)
? $this->message->from(...$address)
: $this->message->from(new Address($address, (string) $name));
return $this;
}
/**
* Set the "sender" of the message.
*
* @param string|array $address
* @param string|null $name
* @return $this
*/
public function sender($address, $name = null)
{
is_array($address)
? $this->message->sender(...$address)
: $this->message->sender(new Address($address, (string) $name));
return $this;
}
/**
* Set the "return path" of the message.
*
* @param string $address
* @return $this
*/
public function returnPath($address)
{
$this->message->returnPath($address);
return $this;
}
/**
* Add a recipient to the message.
*
* @param string|array $address
* @param string|null $name
* @param bool $override
* @return $this
*/
public function to($address, $name = null, $override = false)
{
if ($override) {
is_array($address)
? $this->message->to(...$address)
: $this->message->to(new Address($address, (string) $name));
return $this;
}
return $this->addAddresses($address, $name, 'To');
}
/**
* Remove all "to" addresses from the message.
*
* @return $this
*/
public function forgetTo()
{
if ($header = $this->message->getHeaders()->get('To')) {
$this->addAddressDebugHeader('X-To', $this->message->getTo());
$header->setAddresses([]);
}
return $this;
}
/**
* Add a carbon copy to the message.
*
* @param string|array $address
* @param string|null $name
* @param bool $override
* @return $this
*/
public function cc($address, $name = null, $override = false)
{
if ($override) {
is_array($address)
? $this->message->cc(...$address)
: $this->message->cc(new Address($address, (string) $name));
return $this;
}
return $this->addAddresses($address, $name, 'Cc');
}
/**
* Remove all carbon copy addresses from the message.
*
* @return $this
*/
public function forgetCc()
{
if ($header = $this->message->getHeaders()->get('Cc')) {
$this->addAddressDebugHeader('X-Cc', $this->message->getCC());
$header->setAddresses([]);
}
return $this;
}
/**
* Add a blind carbon copy to the message.
*
* @param string|array $address
* @param string|null $name
* @param bool $override
* @return $this
*/
public function bcc($address, $name = null, $override = false)
{
if ($override) {
is_array($address)
? $this->message->bcc(...$address)
: $this->message->bcc(new Address($address, (string) $name));
return $this;
}
return $this->addAddresses($address, $name, 'Bcc');
}
/**
* Remove all of the blind carbon copy addresses from the message.
*
* @return $this
*/
public function forgetBcc()
{
if ($header = $this->message->getHeaders()->get('Bcc')) {
$this->addAddressDebugHeader('X-Bcc', $this->message->getBcc());
$header->setAddresses([]);
}
return $this;
}
/**
* Add a "reply to" address to the message.
*
* @param string|array $address
* @param string|null $name
* @return $this
*/
public function replyTo($address, $name = null)
{
return $this->addAddresses($address, $name, 'ReplyTo');
}
/**
* Add a recipient to the message.
*
* @param string|array $address
* @param string $name
* @param string $type
* @return $this
*/
protected function addAddresses($address, $name, $type)
{
if (is_array($address)) {
$type = lcfirst($type);
$addresses = collect($address)->map(function ($address, $key) {
if (is_string($key) && is_string($address)) {
return new Address($key, $address);
}
if (is_array($address)) {
return new Address($address['email'] ?? $address['address'], $address['name'] ?? null);
}
if (is_null($address)) {
return new Address($key);
}
return $address;
})->all();
$this->message->{"{$type}"}(...$addresses);
} else {
$this->message->{"add{$type}"}(new Address($address, (string) $name));
}
return $this;
}
/**
* Add an address debug header for a list of recipients.
*
* @param string $header
* @param \Symfony\Component\Mime\Address[] $addresses
* @return $this
*/
protected function addAddressDebugHeader(string $header, array $addresses)
{
$this->message->getHeaders()->addTextHeader(
$header,
implode(', ', array_map(fn ($a) => $a->toString(), $addresses)),
);
return $this;
}
/**
* Set the subject of the message.
*
* @param string $subject
* @return $this
*/
public function subject($subject)
{
$this->message->subject($subject);
return $this;
}
/**
* Set the message priority level.
*
* @param int $level
* @return $this
*/
public function priority($level)
{
$this->message->priority($level);
return $this;
}
/**
* Attach a file to the message.
*
* @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file
* @param array $options
* @return $this
*/
public function attach($file, array $options = [])
{
if ($file instanceof Attachable) {
$file = $file->toMailAttachment();
}
if ($file instanceof Attachment) {
return $file->attachTo($this);
}
$this->message->attachFromPath($file, $options['as'] ?? null, $options['mime'] ?? null);
return $this;
}
/**
* Attach in-memory data as an attachment.
*
* @param string|resource $data
* @param string $name
* @param array $options
* @return $this
*/
public function attachData($data, $name, array $options = [])
{
$this->message->attach($data, $name, $options['mime'] ?? null);
return $this;
}
/**
* Embed a file in the message and get the CID.
*
* @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file
* @return string
*/
public function embed($file)
{
if ($file instanceof Attachable) {
$file = $file->toMailAttachment();
}
if ($file instanceof Attachment) {
return $file->attachWith(
function ($path) use ($file) {
$cid = $file->as ?? Str::random();
$this->message->addPart(
(new DataPart(new File($path), $cid, $file->mime))->asInline()
);
return "cid:{$cid}";
},
function ($data) use ($file) {
$this->message->addPart(
(new DataPart($data(), $file->as, $file->mime))->asInline()
);
return "cid:{$file->as}";
}
);
}
$cid = Str::random(10);
$this->message->addPart(
(new DataPart(new File($file), $cid))->asInline()
);
return "cid:$cid";
}
/**
* Embed in-memory data in the message and get the CID.
*
* @param string|resource $data
* @param string $name
* @param string|null $contentType
* @return string
*/
public function embedData($data, $name, $contentType = null)
{
$this->message->addPart(
(new DataPart($data, $name, $contentType))->asInline()
);
return "cid:$name";
}
/**
* Get the underlying Symfony Email instance.
*
* @return \Symfony\Component\Mime\Email
*/
public function getSymfonyMessage()
{
return $this->message;
}
/**
* Dynamically pass missing methods to the Symfony instance.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->forwardDecoratedCallTo($this->message, $method, $parameters);
}
}