1
0

Message.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. /**
  188. * Set the subject of this message.
  189. *
  190. * @return $this
  191. */
  192. public function setSubject(string $subject): IMessage {
  193. $this->symfonyEmail->subject($subject);
  194. return $this;
  195. }
  196. /**
  197. * Get the from subject of this message.
  198. */
  199. public function getSubject(): string {
  200. return $this->symfonyEmail->getSubject() ?? '';
  201. }
  202. /**
  203. * Set the plain-text body of this message.
  204. * @return $this
  205. */
  206. public function setPlainBody(string $body): IMessage {
  207. $this->symfonyEmail->text($body);
  208. return $this;
  209. }
  210. /**
  211. * Get the plain body of this message.
  212. */
  213. public function getPlainBody(): string {
  214. /** @var string $body */
  215. $body = $this->symfonyEmail->getTextBody() ?? '';
  216. return $body;
  217. }
  218. /**
  219. * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
  220. * @return $this
  221. */
  222. public function setHtmlBody(string $body): IMessage {
  223. if (!$this->plainTextOnly) {
  224. $this->symfonyEmail->html($body);
  225. }
  226. return $this;
  227. }
  228. /**
  229. * Set the underlying Email intance
  230. */
  231. public function setSymfonyEmail(Email $symfonyEmail): void {
  232. $this->symfonyEmail = $symfonyEmail;
  233. }
  234. /**
  235. * Get the underlying Email instance
  236. */
  237. public function getSymfonyEmail(): Email {
  238. return $this->symfonyEmail;
  239. }
  240. /**
  241. * @return $this
  242. */
  243. public function setBody(string $body, string $contentType): IMessage {
  244. if (!$this->plainTextOnly || $contentType !== 'text/html') {
  245. if ($contentType === 'text/html') {
  246. $this->symfonyEmail->html($body);
  247. } else {
  248. $this->symfonyEmail->text($body);
  249. }
  250. }
  251. return $this;
  252. }
  253. /**
  254. * Set the recipients on the symphony email
  255. *
  256. * Since
  257. *
  258. * setTo
  259. * setFrom
  260. * setReplyTo
  261. * setCc
  262. * setBcc
  263. *
  264. * could throw a \Symfony\Component\Mime\Exception\RfcComplianceException
  265. * or a \Symfony\Component\Mime\Exception\InvalidArgumentException
  266. * we wrap the calls here. We then have the validation errors all in one place and can
  267. * throw shortly before \OC\Mail\Mailer::send
  268. *
  269. * @return void
  270. * @throws InvalidArgumentException|RfcComplianceException
  271. */
  272. public function setRecipients() {
  273. $this->symfonyEmail->to(...$this->convertAddresses($this->getTo()));
  274. $this->symfonyEmail->from(...$this->convertAddresses($this->getFrom()));
  275. $this->symfonyEmail->replyTo(...$this->convertAddresses($this->getReplyTo()));
  276. $this->symfonyEmail->cc(...$this->convertAddresses($this->getCc()));
  277. $this->symfonyEmail->bcc(...$this->convertAddresses($this->getBcc()));
  278. }
  279. /**
  280. * @return $this
  281. */
  282. public function useTemplate(IEMailTemplate $emailTemplate): IMessage {
  283. $this->setSubject($emailTemplate->renderSubject());
  284. $this->setPlainBody($emailTemplate->renderText());
  285. if (!$this->plainTextOnly) {
  286. $this->setHtmlBody($emailTemplate->renderHtml());
  287. }
  288. return $this;
  289. }
  290. /**
  291. * Add the Auto-Submitted header to the email, preventing most automated
  292. * responses to automated messages.
  293. *
  294. * @param AutoSubmitted::VALUE_* $value (one of AutoSubmitted::VALUE_NO, AutoSubmitted::VALUE_AUTO_GENERATED, AutoSubmitted::VALUE_AUTO_REPLIED)
  295. * @return $this
  296. */
  297. public function setAutoSubmitted(string $value): IMessage {
  298. $headers = $this->symfonyEmail->getHeaders();
  299. if ($headers->has(AutoSubmitted::HEADER)) {
  300. // if the header already exsists, remove it.
  301. // the value can be modified with some implementations
  302. // of the interface \Swift_Mime_Header, however the
  303. // interface doesn't, and this makes the static-code
  304. // analysis unhappy.
  305. // @todo check if symfony mailer can modify the autosubmitted header
  306. $headers->remove(AutoSubmitted::HEADER);
  307. }
  308. $headers->addTextHeader(AutoSubmitted::HEADER, $value);
  309. return $this;
  310. }
  311. /**
  312. * Get the current value of the Auto-Submitted header. Defaults to "no"
  313. * which is equivalent to the header not existing at all
  314. *
  315. * @return string
  316. */
  317. public function getAutoSubmitted(): string {
  318. $headers = $this->symfonyEmail->getHeaders();
  319. return $headers->has(AutoSubmitted::HEADER) ?
  320. $headers->get(AutoSubmitted::HEADER)->getBodyAsString() : AutoSubmitted::VALUE_NO;
  321. }
  322. }