1
0

Message.php 9.1 KB

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