Attachment.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Mail;
  8. use OCP\Mail\IAttachment;
  9. use Symfony\Component\Mime\Email;
  10. /**
  11. * Class Attachment
  12. *
  13. * @package OC\Mail
  14. * @since 13.0.0
  15. */
  16. class Attachment implements IAttachment {
  17. public function __construct(
  18. private ?string $body,
  19. private ?string $name,
  20. private ?string $contentType,
  21. private ?string $path = null
  22. ) {
  23. }
  24. /**
  25. * @return $this
  26. * @since 13.0.0
  27. */
  28. public function setFilename(string $filename): IAttachment {
  29. $this->name = $filename;
  30. return $this;
  31. }
  32. /**
  33. * @return $this
  34. * @since 13.0.0
  35. */
  36. public function setContentType(string $contentType): IAttachment {
  37. $this->contentType = $contentType;
  38. return $this;
  39. }
  40. /**
  41. * @return $this
  42. * @since 13.0.0
  43. */
  44. public function setBody(string $body): IAttachment {
  45. $this->body = $body;
  46. return $this;
  47. }
  48. public function attach(Email $symfonyEmail): void {
  49. if ($this->path !== null) {
  50. $symfonyEmail->attachFromPath($this->path, $this->name, $this->contentType);
  51. } else {
  52. $symfonyEmail->attach($this->body, $this->name, $this->contentType);
  53. }
  54. }
  55. }