Message.php 9.2 KB

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