Message.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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\IAttachment;
  32. use OCP\Mail\IEMailTemplate;
  33. use OCP\Mail\IMessage;
  34. use Swift_Message;
  35. /**
  36. * Class Message provides a wrapper around SwiftMail
  37. *
  38. * @package OC\Mail
  39. */
  40. class Message implements IMessage {
  41. /** @var Swift_Message */
  42. private $swiftMessage;
  43. /** @var bool */
  44. private $plainTextOnly;
  45. public function __construct(Swift_Message $swiftMessage, bool $plainTextOnly) {
  46. $this->swiftMessage = $swiftMessage;
  47. $this->plainTextOnly = $plainTextOnly;
  48. }
  49. /**
  50. * @param IAttachment $attachment
  51. * @return $this
  52. * @since 13.0.0
  53. */
  54. public function attach(IAttachment $attachment): IMessage {
  55. /** @var Attachment $attachment */
  56. $this->swiftMessage->attach($attachment->getSwiftAttachment());
  57. return $this;
  58. }
  59. /**
  60. * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
  61. * FIXME: Remove this once SwiftMailer supports IDN
  62. *
  63. * @param array $addresses Array of mail addresses, key will get converted
  64. * @return array Converted addresses if `idn_to_ascii` exists
  65. */
  66. protected function convertAddresses(array $addresses): array {
  67. if (!function_exists('idn_to_ascii') || !defined('INTL_IDNA_VARIANT_UTS46')) {
  68. return $addresses;
  69. }
  70. $convertedAddresses = [];
  71. foreach ($addresses as $email => $readableName) {
  72. $parsableEmail = is_numeric($email) ? $readableName : $email;
  73. if (strpos($parsableEmail, '@') === false) {
  74. $convertedAddresses[$parsableEmail] = $readableName;
  75. continue;
  76. }
  77. [$name, $domain] = explode('@', $parsableEmail, 2);
  78. $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
  79. if (is_numeric($email)) {
  80. $convertedAddresses[] = $name . '@' . $domain;
  81. } else {
  82. $convertedAddresses[$name . '@' . $domain] = $readableName;
  83. }
  84. }
  85. return $convertedAddresses;
  86. }
  87. /**
  88. * Set the from address of this message.
  89. *
  90. * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
  91. *
  92. * @param array $addresses Example: array('sender@domain.org', 'other@domain.org' => 'A name')
  93. * @return $this
  94. */
  95. public function setFrom(array $addresses): IMessage {
  96. $addresses = $this->convertAddresses($addresses);
  97. $this->swiftMessage->setFrom($addresses);
  98. return $this;
  99. }
  100. /**
  101. * Get the from address of this message.
  102. *
  103. * @return array
  104. */
  105. public function getFrom(): array {
  106. return $this->swiftMessage->getFrom() ?? [];
  107. }
  108. /**
  109. * Set the Reply-To address of this message
  110. *
  111. * @param array $addresses
  112. * @return $this
  113. */
  114. public function setReplyTo(array $addresses): IMessage {
  115. $addresses = $this->convertAddresses($addresses);
  116. $this->swiftMessage->setReplyTo($addresses);
  117. return $this;
  118. }
  119. /**
  120. * Returns the Reply-To address of this message
  121. *
  122. * @return string
  123. */
  124. public function getReplyTo(): string {
  125. return $this->swiftMessage->getReplyTo();
  126. }
  127. /**
  128. * Set the to addresses of this message.
  129. *
  130. * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
  131. * @return $this
  132. */
  133. public function setTo(array $recipients): IMessage {
  134. $recipients = $this->convertAddresses($recipients);
  135. $this->swiftMessage->setTo($recipients);
  136. return $this;
  137. }
  138. /**
  139. * Get the to address of this message.
  140. *
  141. * @return array
  142. */
  143. public function getTo(): array {
  144. return $this->swiftMessage->getTo() ?? [];
  145. }
  146. /**
  147. * Set the CC recipients of this message.
  148. *
  149. * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
  150. * @return $this
  151. */
  152. public function setCc(array $recipients): IMessage {
  153. $recipients = $this->convertAddresses($recipients);
  154. $this->swiftMessage->setCc($recipients);
  155. return $this;
  156. }
  157. /**
  158. * Get the cc address of this message.
  159. *
  160. * @return array
  161. */
  162. public function getCc(): array {
  163. return $this->swiftMessage->getCc() ?? [];
  164. }
  165. /**
  166. * Set the BCC recipients of this message.
  167. *
  168. * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
  169. * @return $this
  170. */
  171. public function setBcc(array $recipients): IMessage {
  172. $recipients = $this->convertAddresses($recipients);
  173. $this->swiftMessage->setBcc($recipients);
  174. return $this;
  175. }
  176. /**
  177. * Get the Bcc address of this message.
  178. *
  179. * @return array
  180. */
  181. public function getBcc(): array {
  182. return $this->swiftMessage->getBcc() ?? [];
  183. }
  184. /**
  185. * Set the subject of this message.
  186. *
  187. * @param string $subject
  188. * @return IMessage
  189. */
  190. public function setSubject(string $subject): IMessage {
  191. $this->swiftMessage->setSubject($subject);
  192. return $this;
  193. }
  194. /**
  195. * Get the from subject of this message.
  196. *
  197. * @return string
  198. */
  199. public function getSubject(): string {
  200. return $this->swiftMessage->getSubject();
  201. }
  202. /**
  203. * Set the plain-text body of this message.
  204. *
  205. * @param string $body
  206. * @return $this
  207. */
  208. public function setPlainBody(string $body): IMessage {
  209. $this->swiftMessage->setBody($body);
  210. return $this;
  211. }
  212. /**
  213. * Get the plain body of this message.
  214. *
  215. * @return string
  216. */
  217. public function getPlainBody(): string {
  218. return $this->swiftMessage->getBody();
  219. }
  220. /**
  221. * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
  222. *
  223. * @param string $body
  224. * @return $this
  225. */
  226. public function setHtmlBody($body) {
  227. if (!$this->plainTextOnly) {
  228. $this->swiftMessage->addPart($body, 'text/html');
  229. }
  230. return $this;
  231. }
  232. /**
  233. * Get's the underlying SwiftMessage
  234. * @param Swift_Message $swiftMessage
  235. */
  236. public function setSwiftMessage(Swift_Message $swiftMessage): void {
  237. $this->swiftMessage = $swiftMessage;
  238. }
  239. /**
  240. * Get's the underlying SwiftMessage
  241. * @return Swift_Message
  242. */
  243. public function getSwiftMessage(): Swift_Message {
  244. return $this->swiftMessage;
  245. }
  246. /**
  247. * @param string $body
  248. * @param string $contentType
  249. * @return $this
  250. */
  251. public function setBody($body, $contentType) {
  252. if (!$this->plainTextOnly || $contentType !== 'text/html') {
  253. $this->swiftMessage->setBody($body, $contentType);
  254. }
  255. return $this;
  256. }
  257. /**
  258. * @param IEMailTemplate $emailTemplate
  259. * @return $this
  260. */
  261. public function useTemplate(IEMailTemplate $emailTemplate): IMessage {
  262. $this->setSubject($emailTemplate->renderSubject());
  263. $this->setPlainBody($emailTemplate->renderText());
  264. if (!$this->plainTextOnly) {
  265. $this->setHtmlBody($emailTemplate->renderHtml());
  266. }
  267. return $this;
  268. }
  269. }