1
0

Message.php 8.8 KB

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