13
13
14
14
use Symfony \Component \Mime \Exception \LogicException ;
15
15
use Symfony \Component \Mime \Part \AbstractPart ;
16
+ use Symfony \Component \Mime \Part \BodyFile ;
16
17
use Symfony \Component \Mime \Part \DataPart ;
17
18
use Symfony \Component \Mime \Part \Multipart \AlternativePart ;
18
19
use Symfony \Component \Mime \Part \Multipart \MixedPart ;
@@ -326,25 +327,15 @@ public function getHtmlCharset(): ?string
326
327
*/
327
328
public function attach ($ body , string $ name = null , string $ contentType = null ): static
328
329
{
329
- if (!\is_string ($ body ) && !\is_resource ($ body )) {
330
- throw new \TypeError (sprintf ('The body must be a string or a resource (got "%s"). ' , get_debug_type ($ body )));
331
- }
332
-
333
- $ this ->cachedBody = null ;
334
- $ this ->attachments [] = ['body ' => $ body , 'name ' => $ name , 'content-type ' => $ contentType , 'inline ' => false ];
335
-
336
- return $ this ;
330
+ return $ this ->attachPart (new DataPart ($ body , $ name , $ contentType ));
337
331
}
338
332
339
333
/**
340
334
* @return $this
341
335
*/
342
336
public function attachFromPath (string $ path , string $ name = null , string $ contentType = null ): static
343
337
{
344
- $ this ->cachedBody = null ;
345
- $ this ->attachments [] = ['path ' => $ path , 'name ' => $ name , 'content-type ' => $ contentType , 'inline ' => false ];
346
-
347
- return $ this ;
338
+ return $ this ->attachPart (new DataPart (new BodyFile ($ path ), $ name , $ contentType ));
348
339
}
349
340
350
341
/**
@@ -354,25 +345,15 @@ public function attachFromPath(string $path, string $name = null, string $conten
354
345
*/
355
346
public function embed ($ body , string $ name = null , string $ contentType = null ): static
356
347
{
357
- if (!\is_string ($ body ) && !\is_resource ($ body )) {
358
- throw new \TypeError (sprintf ('The body must be a string or a resource (got "%s"). ' , get_debug_type ($ body )));
359
- }
360
-
361
- $ this ->cachedBody = null ;
362
- $ this ->attachments [] = ['body ' => $ body , 'name ' => $ name , 'content-type ' => $ contentType , 'inline ' => true ];
363
-
364
- return $ this ;
348
+ return $ this ->attachPart ((new DataPart ($ body , $ name , $ contentType ))->asInline ());
365
349
}
366
350
367
351
/**
368
352
* @return $this
369
353
*/
370
354
public function embedFromPath (string $ path , string $ name = null , string $ contentType = null ): static
371
355
{
372
- $ this ->cachedBody = null ;
373
- $ this ->attachments [] = ['path ' => $ path , 'name ' => $ name , 'content-type ' => $ contentType , 'inline ' => true ];
374
-
375
- return $ this ;
356
+ return $ this ->attachPart ((new DataPart (new BodyFile ($ path ), $ name , $ contentType ))->asInline ());
376
357
}
377
358
378
359
/**
@@ -381,22 +362,17 @@ public function embedFromPath(string $path, string $name = null, string $content
381
362
public function attachPart (DataPart $ part ): static
382
363
{
383
364
$ this ->cachedBody = null ;
384
- $ this ->attachments [] = [ ' part ' => $ part] ;
365
+ $ this ->attachments [] = $ part ;
385
366
386
367
return $ this ;
387
368
}
388
369
389
370
/**
390
- * @return array| DataPart[]
371
+ * @return DataPart[]
391
372
*/
392
373
public function getAttachments (): array
393
374
{
394
- $ parts = [];
395
- foreach ($ this ->attachments as $ attachment ) {
396
- $ parts [] = $ this ->createDataPart ($ attachment );
397
- }
398
-
399
- return $ parts ;
375
+ return $ this ->attachments ;
400
376
}
401
377
402
378
public function getBody (): AbstractPart
@@ -502,35 +478,23 @@ private function prepareParts(): ?array
502
478
}
503
479
504
480
$ otherParts = $ relatedParts = [];
505
- foreach ($ this ->attachments as $ attachment ) {
506
- $ part = $ this ->createDataPart ($ attachment );
507
- if (isset ($ attachment ['part ' ])) {
508
- $ attachment ['name ' ] = $ part ->getName ();
509
- }
510
-
511
- $ related = false ;
481
+ foreach ($ this ->attachments as $ part ) {
512
482
foreach ($ names as $ name ) {
513
- if ($ name !== $ attachment [ ' name ' ] ) {
483
+ if ($ name !== $ part -> getName () ) {
514
484
continue ;
515
485
}
516
486
if (isset ($ relatedParts [$ name ])) {
517
487
continue 2 ;
518
488
}
519
- $ part -> setDisposition ( ' inline ' );
489
+
520
490
$ html = str_replace ('cid: ' .$ name , 'cid: ' .$ part ->getContentId (), $ html , $ count );
521
- if ($ count ) {
522
- $ related = true ;
523
- }
524
- $ part ->setName ($ part ->getContentId ());
491
+ $ relatedParts [$ name ] = $ part ;
492
+ $ part ->setName ($ part ->getContentId ())->asInline ();
525
493
526
- break ;
494
+ continue 2 ;
527
495
}
528
496
529
- if ($ related ) {
530
- $ relatedParts [$ attachment ['name ' ]] = $ part ;
531
- } else {
532
- $ otherParts [] = $ part ;
533
- }
497
+ $ otherParts [] = $ part ;
534
498
}
535
499
if (null !== $ htmlPart ) {
536
500
$ htmlPart = new TextPart ($ html , $ this ->htmlCharset , 'html ' );
@@ -539,24 +503,6 @@ private function prepareParts(): ?array
539
503
return [$ htmlPart , $ otherParts , array_values ($ relatedParts )];
540
504
}
541
505
542
- private function createDataPart (array $ attachment ): DataPart
543
- {
544
- if (isset ($ attachment ['part ' ])) {
545
- return $ attachment ['part ' ];
546
- }
547
-
548
- if (isset ($ attachment ['body ' ])) {
549
- $ part = new DataPart ($ attachment ['body ' ], $ attachment ['name ' ] ?? null , $ attachment ['content-type ' ] ?? null );
550
- } else {
551
- $ part = DataPart::fromPath ($ attachment ['path ' ] ?? '' , $ attachment ['name ' ] ?? null , $ attachment ['content-type ' ] ?? null );
552
- }
553
- if ($ attachment ['inline ' ]) {
554
- $ part ->asInline ();
555
- }
556
-
557
- return $ part ;
558
- }
559
-
560
506
/**
561
507
* @return $this
562
508
*/
@@ -606,12 +552,6 @@ public function __serialize(): array
606
552
$ this ->html = (new TextPart ($ this ->html ))->getBody ();
607
553
}
608
554
609
- foreach ($ this ->attachments as $ i => $ attachment ) {
610
- if (isset ($ attachment ['body ' ]) && \is_resource ($ attachment ['body ' ])) {
611
- $ this ->attachments [$ i ]['body ' ] = (new TextPart ($ attachment ['body ' ]))->getBody ();
612
- }
613
- }
614
-
615
555
return [$ this ->text , $ this ->textCharset , $ this ->html , $ this ->htmlCharset , $ this ->attachments , parent ::__serialize ()];
616
556
}
617
557
0 commit comments