1
0

Attachment.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Mail;
  26. use OCP\Mail\IAttachment;
  27. use Symfony\Component\Mime\Email;
  28. /**
  29. * Class Attachment
  30. *
  31. * @package OC\Mail
  32. * @since 13.0.0
  33. */
  34. class Attachment implements IAttachment {
  35. public function __construct(
  36. private ?string $body,
  37. private ?string $name,
  38. private ?string $contentType,
  39. private ?string $path = null
  40. ) {
  41. }
  42. /**
  43. * @return $this
  44. * @since 13.0.0
  45. */
  46. public function setFilename(string $filename): IAttachment {
  47. $this->name = $filename;
  48. return $this;
  49. }
  50. /**
  51. * @return $this
  52. * @since 13.0.0
  53. */
  54. public function setContentType(string $contentType): IAttachment {
  55. $this->contentType = $contentType;
  56. return $this;
  57. }
  58. /**
  59. * @return $this
  60. * @since 13.0.0
  61. */
  62. public function setBody(string $body): IAttachment {
  63. $this->body = $body;
  64. return $this;
  65. }
  66. public function attach(Email $symfonyEmail): void {
  67. if ($this->path !== null) {
  68. $symfonyEmail->attachFromPath($this->path, $this->name, $this->contentType);
  69. } else {
  70. $symfonyEmail->attach($this->body, $this->name, $this->contentType);
  71. }
  72. }
  73. }