1
0

Attachment.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. private ?string $body;
  36. private ?string $name;
  37. private ?string $contentType;
  38. private ?string $path;
  39. public function __construct(
  40. ?string $body,
  41. ?string $name,
  42. ?string $contentType,
  43. ?string $path = null
  44. ) {
  45. $this->body = $body;
  46. $this->name = $name;
  47. $this->contentType = $contentType;
  48. $this->path = $path;
  49. }
  50. /**
  51. * @param string $filename
  52. * @return $this
  53. * @since 13.0.0
  54. */
  55. public function setFilename(string $filename): IAttachment {
  56. $this->name = $filename;
  57. return $this;
  58. }
  59. /**
  60. * @param string $contentType
  61. * @return $this
  62. * @since 13.0.0
  63. */
  64. public function setContentType(string $contentType): IAttachment {
  65. $this->contentType = $contentType;
  66. return $this;
  67. }
  68. /**
  69. * @param string $body
  70. * @return $this
  71. * @since 13.0.0
  72. */
  73. public function setBody(string $body): IAttachment {
  74. $this->body = $body;
  75. return $this;
  76. }
  77. public function attach(Email $symfonyEmail): void {
  78. if ($this->path !== null) {
  79. $symfonyEmail->attachFromPath($this->path, $this->name, $this->contentType);
  80. } else {
  81. $symfonyEmail->attach($this->body, $this->name, $this->contentType);
  82. }
  83. }
  84. }