@@ -326,25 +326,15 @@ public function getHtmlCharset(): ?string
326326 */
327327 public function attach ($ body , string $ name = null , string $ contentType = null ): static
328328 {
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 ;
329+ return $ this ->attachPart (new DataPart ($ body , $ name , $ contentType ));
337330 }
338331
339332 /**
340333 * @return $this
341334 */
342335 public function attachFromPath (string $ path , string $ name = null , string $ contentType = null ): static
343336 {
344- $ this ->cachedBody = null ;
345- $ this ->attachments [] = ['path ' => $ path , 'name ' => $ name , 'content-type ' => $ contentType , 'inline ' => false ];
346-
347- return $ this ;
337+ return $ this ->attachPart (new DataPart ('file:// ' .$ path , $ name , $ contentType ));
348338 }
349339
350340 /**
@@ -354,25 +344,15 @@ public function attachFromPath(string $path, string $name = null, string $conten
354344 */
355345 public function embed ($ body , string $ name = null , string $ contentType = null ): static
356346 {
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 ;
347+ return $ this ->attachPart ((new DataPart ($ body , $ name , $ contentType ))->asInline ());
365348 }
366349
367350 /**
368351 * @return $this
369352 */
370353 public function embedFromPath (string $ path , string $ name = null , string $ contentType = null ): static
371354 {
372- $ this ->cachedBody = null ;
373- $ this ->attachments [] = ['path ' => $ path , 'name ' => $ name , 'content-type ' => $ contentType , 'inline ' => true ];
374-
375- return $ this ;
355+ return $ this ->attachPart ((new DataPart ('file:// ' .$ path , $ name , $ contentType ))->asInline ());
376356 }
377357
378358 /**
@@ -381,22 +361,17 @@ public function embedFromPath(string $path, string $name = null, string $content
381361 public function attachPart (DataPart $ part ): static
382362 {
383363 $ this ->cachedBody = null ;
384- $ this ->attachments [] = [ ' part ' => $ part] ;
364+ $ this ->attachments [] = $ part ;
385365
386366 return $ this ;
387367 }
388368
389369 /**
390- * @return array| DataPart[]
370+ * @return DataPart[]
391371 */
392372 public function getAttachments (): array
393373 {
394- $ parts = [];
395- foreach ($ this ->attachments as $ attachment ) {
396- $ parts [] = $ this ->createDataPart ($ attachment );
397- }
398-
399- return $ parts ;
374+ return $ this ->attachments ;
400375 }
401376
402377 public function getBody (): AbstractPart
@@ -502,35 +477,23 @@ private function prepareParts(): ?array
502477 }
503478
504479 $ 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 ;
480+ foreach ($ this ->attachments as $ part ) {
512481 foreach ($ names as $ name ) {
513- if ($ name !== $ attachment [ ' name ' ] ) {
482+ if ($ name !== $ part -> getName () ) {
514483 continue ;
515484 }
516485 if (isset ($ relatedParts [$ name ])) {
517486 continue 2 ;
518487 }
519- $ part -> setDisposition ( ' inline ' );
488+
520489 $ html = str_replace ('cid: ' .$ name , 'cid: ' .$ part ->getContentId (), $ html , $ count );
521- if ($ count ) {
522- $ related = true ;
523- }
524- $ part ->setName ($ part ->getContentId ());
490+ $ relatedParts [$ name ] = $ part ;
491+ $ part ->setName ($ part ->getContentId ())->asInline ();
525492
526- break ;
493+ continue 2 ;
527494 }
528495
529- if ($ related ) {
530- $ relatedParts [$ attachment ['name ' ]] = $ part ;
531- } else {
532- $ otherParts [] = $ part ;
533- }
496+ $ otherParts [] = $ part ;
534497 }
535498 if (null !== $ htmlPart ) {
536499 $ htmlPart = new TextPart ($ html , $ this ->htmlCharset , 'html ' );
@@ -539,24 +502,6 @@ private function prepareParts(): ?array
539502 return [$ htmlPart , $ otherParts , array_values ($ relatedParts )];
540503 }
541504
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-
560505 /**
561506 * @return $this
562507 */
@@ -606,12 +551,6 @@ public function __serialize(): array
606551 $ this ->html = (new TextPart ($ this ->html ))->getBody ();
607552 }
608553
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-
615554 return [$ this ->text , $ this ->textCharset , $ this ->html , $ this ->htmlCharset , $ this ->attachments , parent ::__serialize ()];
616555 }
617556
0 commit comments