1313
1414use Symfony \Component \Mime \Exception \LogicException ;
1515use Symfony \Component \Mime \Part \AbstractPart ;
16+ use Symfony \Component \Mime \Part \BodyFile ;
1617use Symfony \Component \Mime \Part \DataPart ;
1718use Symfony \Component \Mime \Part \Multipart \AlternativePart ;
1819use Symfony \Component \Mime \Part \Multipart \MixedPart ;
@@ -326,25 +327,15 @@ public function getHtmlCharset(): ?string
326327 */
327328 public function attach ($ body , string $ name = null , string $ contentType = null ): static
328329 {
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 ));
337331 }
338332
339333 /**
340334 * @return $this
341335 */
342336 public function attachFromPath (string $ path , string $ name = null , string $ contentType = null ): static
343337 {
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 ));
348339 }
349340
350341 /**
@@ -354,25 +345,15 @@ public function attachFromPath(string $path, string $name = null, string $conten
354345 */
355346 public function embed ($ body , string $ name = null , string $ contentType = null ): static
356347 {
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 ());
365349 }
366350
367351 /**
368352 * @return $this
369353 */
370354 public function embedFromPath (string $ path , string $ name = null , string $ contentType = null ): static
371355 {
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 ());
376357 }
377358
378359 /**
@@ -381,22 +362,17 @@ public function embedFromPath(string $path, string $name = null, string $content
381362 public function attachPart (DataPart $ part ): static
382363 {
383364 $ this ->cachedBody = null ;
384- $ this ->attachments [] = [ ' part ' => $ part] ;
365+ $ this ->attachments [] = $ part ;
385366
386367 return $ this ;
387368 }
388369
389370 /**
390- * @return array| DataPart[]
371+ * @return DataPart[]
391372 */
392373 public function getAttachments (): array
393374 {
394- $ parts = [];
395- foreach ($ this ->attachments as $ attachment ) {
396- $ parts [] = $ this ->createDataPart ($ attachment );
397- }
398-
399- return $ parts ;
375+ return $ this ->attachments ;
400376 }
401377
402378 public function getBody (): AbstractPart
@@ -502,35 +478,23 @@ private function prepareParts(): ?array
502478 }
503479
504480 $ 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 ) {
512482 foreach ($ names as $ name ) {
513- if ($ name !== $ attachment [ ' name ' ] ) {
483+ if ($ name !== $ part -> getName () ) {
514484 continue ;
515485 }
516486 if (isset ($ relatedParts [$ name ])) {
517487 continue 2 ;
518488 }
519- $ part -> setDisposition ( ' inline ' );
489+
520490 $ 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 ();
525493
526- break ;
494+ continue 2 ;
527495 }
528496
529- if ($ related ) {
530- $ relatedParts [$ attachment ['name ' ]] = $ part ;
531- } else {
532- $ otherParts [] = $ part ;
533- }
497+ $ otherParts [] = $ part ;
534498 }
535499 if (null !== $ htmlPart ) {
536500 $ htmlPart = new TextPart ($ html , $ this ->htmlCharset , 'html ' );
@@ -539,24 +503,6 @@ private function prepareParts(): ?array
539503 return [$ htmlPart , $ otherParts , array_values ($ relatedParts )];
540504 }
541505
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-
560506 /**
561507 * @return $this
562508 */
@@ -606,12 +552,6 @@ public function __serialize(): array
606552 $ this ->html = (new TextPart ($ this ->html ))->getBody ();
607553 }
608554
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-
615555 return [$ this ->text , $ this ->textCharset , $ this ->html , $ this ->htmlCharset , $ this ->attachments , parent ::__serialize ()];
616556 }
617557
0 commit comments