You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
7.4 KiB

3 years ago
  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. if (!is_numeric($email)) {
  73. list($name, $domain) = explode('@', $email, 2);
  74. $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
  75. $convertedAddresses[$name.'@'.$domain] = $readableName;
  76. } else {
  77. list($name, $domain) = explode('@', $readableName, 2);
  78. $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
  79. $convertedAddresses[$email] = $name.'@'.$domain;
  80. }
  81. }
  82. return $convertedAddresses;
  83. }
  84. /**
  85. * Set the from address of this message.
  86. *
  87. * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
  88. *
  89. * @param array $addresses Example: array('sender@domain.org', 'other@domain.org' => 'A name')
  90. * @return $this
  91. */
  92. public function setFrom(array $addresses): IMessage {
  93. $addresses = $this->convertAddresses($addresses);
  94. $this->swiftMessage->setFrom($addresses);
  95. return $this;
  96. }
  97. /**
  98. * Get the from address of this message.
  99. *
  100. * @return array
  101. */
  102. public function getFrom(): array {
  103. return $this->swiftMessage->getFrom() ?? [];
  104. }
  105. /**
  106. * Set the Reply-To address of this message
  107. *
  108. * @param array $addresses
  109. * @return $this
  110. */
  111. public function setReplyTo(array $addresses): IMessage {
  112. $addresses = $this->convertAddresses($addresses);
  113. $this->swiftMessage->setReplyTo($addresses);
  114. return $this;
  115. }
  116. /**
  117. * Returns the Reply-To address of this message
  118. *
  119. * @return string
  120. */
  121. public function getReplyTo(): string {
  122. return $this->swiftMessage->getReplyTo();
  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. $recipients = $this->convertAddresses($recipients);
  132. $this->swiftMessage->setTo($recipients);
  133. return $this;
  134. }
  135. /**
  136. * Get the to address of this message.
  137. *
  138. * @return array
  139. */
  140. public function getTo(): array {
  141. return $this->swiftMessage->getTo() ?? [];
  142. }
  143. /**
  144. * Set the CC recipients of this message.
  145. *
  146. * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
  147. * @return $this
  148. */
  149. public function setCc(array $recipients): IMessage {
  150. $recipients = $this->convertAddresses($recipients);
  151. $this->swiftMessage->setCc($recipients);
  152. return $this;
  153. }
  154. /**
  155. * Get the cc address of this message.
  156. *
  157. * @return array
  158. */
  159. public function getCc(): array {
  160. return $this->swiftMessage->getCc() ?? [];
  161. }
  162. /**
  163. * Set the BCC recipients of this message.
  164. *
  165. * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
  166. * @return $this
  167. */
  168. public function setBcc(array $recipients): IMessage {
  169. $recipients = $this->convertAddresses($recipients);
  170. $this->swiftMessage->setBcc($recipients);
  171. return $this;
  172. }
  173. /**
  174. * Get the Bcc address of this message.
  175. *
  176. * @return array
  177. */
  178. public function getBcc(): array {
  179. return $this->swiftMessage->getBcc() ?? [];
  180. }
  181. /**
  182. * Set the subject of this message.
  183. *
  184. * @param string $subject
  185. * @return IMessage
  186. */
  187. public function setSubject(string $subject): IMessage {
  188. $this->swiftMessage->setSubject($subject);
  189. return $this;
  190. }
  191. /**
  192. * Get the from subject of this message.
  193. *
  194. * @return string
  195. */
  196. public function getSubject(): string {
  197. return $this->swiftMessage->getSubject();
  198. }
  199. /**
  200. * Set the plain-text body of this message.
  201. *
  202. * @param string $body
  203. * @return $this
  204. */
  205. public function setPlainBody(string $body): IMessage {
  206. $this->swiftMessage->setBody($body);
  207. return $this;
  208. }
  209. /**
  210. * Get the plain body of this message.
  211. *
  212. * @return string
  213. */
  214. public function getPlainBody(): string {
  215. return $this->swiftMessage->getBody();
  216. }
  217. /**
  218. * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
  219. *
  220. * @param string $body
  221. * @return $this
  222. */
  223. public function setHtmlBody($body) {
  224. if (!$this->plainTextOnly) {
  225. $this->swiftMessage->addPart($body, 'text/html');
  226. }
  227. return $this;
  228. }
  229. /**
  230. * Get's the underlying SwiftMessage
  231. * @param Swift_Message $swiftMessage
  232. */
  233. public function setSwiftMessage(Swift_Message $swiftMessage): void {
  234. $this->swiftMessage = $swiftMessage;
  235. }
  236. /**
  237. * Get's the underlying SwiftMessage
  238. * @return Swift_Message
  239. */
  240. public function getSwiftMessage(): Swift_Message {
  241. return $this->swiftMessage;
  242. }
  243. /**
  244. * @param string $body
  245. * @param string $contentType
  246. * @return $this
  247. */
  248. public function setBody($body, $contentType) {
  249. if (!$this->plainTextOnly || $contentType !== 'text/html') {
  250. $this->swiftMessage->setBody($body, $contentType);
  251. }
  252. return $this;
  253. }
  254. /**
  255. * @param IEMailTemplate $emailTemplate
  256. * @return $this
  257. */
  258. public function useTemplate(IEMailTemplate $emailTemplate): IMessage {
  259. $this->setSubject($emailTemplate->renderSubject());
  260. $this->setPlainBody($emailTemplate->renderText());
  261. if (!$this->plainTextOnly) {
  262. $this->setHtmlBody($emailTemplate->renderHtml());
  263. }
  264. return $this;
  265. }
  266. }