Message.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Mail;
  9. use OCP\Mail\Headers\AutoSubmitted;
  10. use OCP\Mail\IAttachment;
  11. use OCP\Mail\IEMailTemplate;
  12. use OCP\Mail\IMessage;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\Mime\Email;
  15. use Symfony\Component\Mime\Exception\InvalidArgumentException;
  16. use Symfony\Component\Mime\Exception\RfcComplianceException;
  17. /**
  18. * Class Message provides a wrapper around Symfony\Component\Mime\Email (Used to be around SwiftMail)
  19. *
  20. * @package OC\Mail
  21. */
  22. class Message implements IMessage {
  23. private array $to = [];
  24. private array $from = [];
  25. private array $replyTo = [];
  26. private array $cc = [];
  27. private array $bcc = [];
  28. public function __construct(
  29. private Email $symfonyEmail,
  30. private bool $plainTextOnly,
  31. ) {
  32. }
  33. /**
  34. * @since 13.0.0
  35. * @return $this
  36. */
  37. public function attach(IAttachment $attachment): IMessage {
  38. /** @var Attachment $attachment */
  39. $attachment->attach($this->symfonyEmail);
  40. return $this;
  41. }
  42. /**
  43. * Can be used to "attach content inline" as message parts with specific MIME type and encoding.
  44. * {@inheritDoc}
  45. * @since 26.0.0
  46. */
  47. public function attachInline(string $body, string $name, ?string $contentType = null): IMessage {
  48. # To be sure this works with iCalendar messages, we encode with 8bit instead of
  49. # quoted-printable encoding. We save the current encoder, replace the current
  50. # encoder with an 8bit encoder and after we've finished, we reset the encoder
  51. # to the previous one. Originally intended to be added after the message body,
  52. # as it is curently unknown if all mail clients handle this properly if added
  53. # before.
  54. $this->symfonyEmail->embed($body, $name, $contentType);
  55. return $this;
  56. }
  57. /**
  58. * Converts the [['displayName' => 'email'], ['displayName2' => 'email2']] arrays to valid Adresses
  59. *
  60. * @param array $addresses Array of mail addresses
  61. * @return Address[]
  62. * @throws RfcComplianceException|InvalidArgumentException
  63. */
  64. protected function convertAddresses(array $addresses): array {
  65. $convertedAddresses = [];
  66. if (empty($addresses)) {
  67. return [];
  68. }
  69. array_walk($addresses, function ($readableName, $email) use (&$convertedAddresses) {
  70. if (is_numeric($email)) {
  71. $convertedAddresses[] = new Address($readableName);
  72. } else {
  73. $convertedAddresses[] = new Address($email, $readableName);
  74. }
  75. });
  76. return $convertedAddresses;
  77. }
  78. /**
  79. * Set the from address of this message.
  80. *
  81. * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
  82. *
  83. * @param array $addresses Example: array('sender@domain.org', 'other@domain.org' => 'A name')
  84. * @return $this
  85. */
  86. public function setFrom(array $addresses): IMessage {
  87. $this->from = $addresses;
  88. return $this;
  89. }
  90. /**
  91. * Get the from address of this message.
  92. */
  93. public function getFrom(): array {
  94. return $this->from;
  95. }
  96. /**
  97. * Set the Reply-To address of this message
  98. * @return $this
  99. */
  100. public function setReplyTo(array $addresses): IMessage {
  101. $this->replyTo = $addresses;
  102. return $this;
  103. }
  104. /**
  105. * Returns the Reply-To address of this message
  106. */
  107. public function getReplyTo(): array {
  108. return $this->replyTo;
  109. }
  110. /**
  111. * Set the to addresses of this message.
  112. *
  113. * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
  114. * @return $this
  115. */
  116. public function setTo(array $recipients): IMessage {
  117. $this->to = $recipients;
  118. return $this;
  119. }
  120. /**
  121. * Get the to address of this message.
  122. */
  123. public function getTo(): array {
  124. return $this->to;
  125. }
  126. /**
  127. * Set the CC recipients of this message.
  128. *
  129. * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
  130. * @return $this
  131. */
  132. public function setCc(array $recipients): IMessage {
  133. $this->cc = $recipients;
  134. return $this;
  135. }
  136. /**
  137. * Get the cc address of this message.
  138. */
  139. public function getCc(): array {
  140. return $this->cc;
  141. }
  142. /**
  143. * Set the BCC recipients of this message.
  144. *
  145. * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
  146. * @return $this
  147. */
  148. public function setBcc(array $recipients): IMessage {
  149. $this->bcc = $recipients;
  150. return $this;
  151. }
  152. /**
  153. * Get the Bcc address of this message.
  154. */
  155. public function getBcc(): array {
  156. return $this->bcc;
  157. }
  158. /**
  159. * @return $this
  160. */
  161. public function setSubject(string $subject): IMessage {
  162. $this->symfonyEmail->subject($subject);
  163. return $this;
  164. }
  165. /**
  166. * Get the from subject of this message.
  167. */
  168. public function getSubject(): string {
  169. return $this->symfonyEmail->getSubject() ?? '';
  170. }
  171. /**
  172. * @return $this
  173. */
  174. public function setPlainBody(string $body): IMessage {
  175. $this->symfonyEmail->text($body);
  176. return $this;
  177. }
  178. /**
  179. * Get the plain body of this message.
  180. */
  181. public function getPlainBody(): string {
  182. /** @var string $body */
  183. $body = $this->symfonyEmail->getTextBody() ?? '';
  184. return $body;
  185. }
  186. /**
  187. * @return $this
  188. */
  189. public function setHtmlBody(string $body): IMessage {
  190. if (!$this->plainTextOnly) {
  191. $this->symfonyEmail->html($body);
  192. }
  193. return $this;
  194. }
  195. /**
  196. * Set the underlying Email instance
  197. */
  198. public function setSymfonyEmail(Email $symfonyEmail): void {
  199. $this->symfonyEmail = $symfonyEmail;
  200. }
  201. /**
  202. * Get the underlying Email instance
  203. */
  204. public function getSymfonyEmail(): Email {
  205. return $this->symfonyEmail;
  206. }
  207. /**
  208. * @return $this
  209. */
  210. public function setBody(string $body, string $contentType): IMessage {
  211. if (!$this->plainTextOnly || $contentType !== 'text/html') {
  212. if ($contentType === 'text/html') {
  213. $this->symfonyEmail->html($body);
  214. } else {
  215. $this->symfonyEmail->text($body);
  216. }
  217. }
  218. return $this;
  219. }
  220. /**
  221. * Set the recipients on the symphony email
  222. *
  223. * Since
  224. *
  225. * setTo
  226. * setFrom
  227. * setReplyTo
  228. * setCc
  229. * setBcc
  230. *
  231. * could throw a \Symfony\Component\Mime\Exception\RfcComplianceException
  232. * or a \Symfony\Component\Mime\Exception\InvalidArgumentException
  233. * we wrap the calls here. We then have the validation errors all in one place and can
  234. * throw shortly before \OC\Mail\Mailer::send
  235. *
  236. * @throws InvalidArgumentException|RfcComplianceException
  237. */
  238. public function setRecipients(): void {
  239. $this->symfonyEmail->to(...$this->convertAddresses($this->getTo()));
  240. $this->symfonyEmail->from(...$this->convertAddresses($this->getFrom()));
  241. $this->symfonyEmail->replyTo(...$this->convertAddresses($this->getReplyTo()));
  242. $this->symfonyEmail->cc(...$this->convertAddresses($this->getCc()));
  243. $this->symfonyEmail->bcc(...$this->convertAddresses($this->getBcc()));
  244. }
  245. /**
  246. * @return $this
  247. */
  248. public function useTemplate(IEMailTemplate $emailTemplate): IMessage {
  249. $this->setSubject($emailTemplate->renderSubject());
  250. $this->setPlainBody($emailTemplate->renderText());
  251. if (!$this->plainTextOnly) {
  252. $this->setHtmlBody($emailTemplate->renderHtml());
  253. }
  254. return $this;
  255. }
  256. /**
  257. * Add the Auto-Submitted header to the email, preventing most automated
  258. * responses to automated messages.
  259. *
  260. * @param AutoSubmitted::VALUE_* $value (one of AutoSubmitted::VALUE_NO, AutoSubmitted::VALUE_AUTO_GENERATED, AutoSubmitted::VALUE_AUTO_REPLIED)
  261. * @return $this
  262. */
  263. public function setAutoSubmitted(string $value): IMessage {
  264. $headers = $this->symfonyEmail->getHeaders();
  265. if ($headers->has(AutoSubmitted::HEADER)) {
  266. // if the header already exsists, remove it.
  267. // the value can be modified with some implementations
  268. // of the interface \Swift_Mime_Header, however the
  269. // interface doesn't, and this makes the static-code
  270. // analysis unhappy.
  271. // @todo check if symfony mailer can modify the autosubmitted header
  272. $headers->remove(AutoSubmitted::HEADER);
  273. }
  274. $headers->addTextHeader(AutoSubmitted::HEADER, $value);
  275. return $this;
  276. }
  277. /**
  278. * Get the current value of the Auto-Submitted header. Defaults to "no"
  279. * which is equivalent to the header not existing at all
  280. */
  281. public function getAutoSubmitted(): string {
  282. $headers = $this->symfonyEmail->getHeaders();
  283. return $headers->has(AutoSubmitted::HEADER) ?
  284. $headers->get(AutoSubmitted::HEADER)->getBodyAsString() : AutoSubmitted::VALUE_NO;
  285. }
  286. }