Message.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Mail;
  27. use OCP\Mail\IAttachment;
  28. use OCP\Mail\IEMailTemplate;
  29. use OCP\Mail\IMessage;
  30. use Swift_Message;
  31. /**
  32. * Class Message provides a wrapper around SwiftMail
  33. *
  34. * @package OC\Mail
  35. */
  36. class Message implements IMessage {
  37. /** @var Swift_Message */
  38. private $swiftMessage;
  39. /**
  40. * @param Swift_Message $swiftMessage
  41. */
  42. public function __construct(Swift_Message $swiftMessage) {
  43. $this->swiftMessage = $swiftMessage;
  44. }
  45. /**
  46. * @param IAttachment $attachment
  47. * @return $this
  48. * @since 13.0.0
  49. */
  50. public function attach(IAttachment $attachment): IMessage {
  51. /** @var Attachment $attachment */
  52. $this->swiftMessage->attach($attachment->getSwiftAttachment());
  53. return $this;
  54. }
  55. /**
  56. * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
  57. * FIXME: Remove this once SwiftMailer supports IDN
  58. *
  59. * @param array $addresses Array of mail addresses, key will get converted
  60. * @return array Converted addresses if `idn_to_ascii` exists
  61. */
  62. protected function convertAddresses(array $addresses): array {
  63. if (!function_exists('idn_to_ascii')) {
  64. return $addresses;
  65. }
  66. $convertedAddresses = [];
  67. foreach($addresses as $email => $readableName) {
  68. if(!is_numeric($email)) {
  69. list($name, $domain) = explode('@', $email, 2);
  70. $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
  71. $convertedAddresses[$name.'@'.$domain] = $readableName;
  72. } else {
  73. list($name, $domain) = explode('@', $readableName, 2);
  74. $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
  75. $convertedAddresses[$email] = $name.'@'.$domain;
  76. }
  77. }
  78. return $convertedAddresses;
  79. }
  80. /**
  81. * Set the from address of this message.
  82. *
  83. * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
  84. *
  85. * @param array $addresses Example: array('sender@domain.org', 'other@domain.org' => 'A name')
  86. * @return $this
  87. */
  88. public function setFrom(array $addresses): IMessage {
  89. $addresses = $this->convertAddresses($addresses);
  90. $this->swiftMessage->setFrom($addresses);
  91. return $this;
  92. }
  93. /**
  94. * Get the from address of this message.
  95. *
  96. * @return array
  97. */
  98. public function getFrom(): array {
  99. return $this->swiftMessage->getFrom();
  100. }
  101. /**
  102. * Set the Reply-To address of this message
  103. *
  104. * @param array $addresses
  105. * @return $this
  106. */
  107. public function setReplyTo(array $addresses): IMessage {
  108. $addresses = $this->convertAddresses($addresses);
  109. $this->swiftMessage->setReplyTo($addresses);
  110. return $this;
  111. }
  112. /**
  113. * Returns the Reply-To address of this message
  114. *
  115. * @return string
  116. */
  117. public function getReplyTo(): string {
  118. return $this->swiftMessage->getReplyTo();
  119. }
  120. /**
  121. * Set the to addresses of this message.
  122. *
  123. * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
  124. * @return $this
  125. */
  126. public function setTo(array $recipients): IMessage {
  127. $recipients = $this->convertAddresses($recipients);
  128. $this->swiftMessage->setTo($recipients);
  129. return $this;
  130. }
  131. /**
  132. * Get the to address of this message.
  133. *
  134. * @return array
  135. */
  136. public function getTo(): array {
  137. return $this->swiftMessage->getTo();
  138. }
  139. /**
  140. * Set the CC recipients 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 setCc(array $recipients): IMessage {
  146. $recipients = $this->convertAddresses($recipients);
  147. $this->swiftMessage->setCc($recipients);
  148. return $this;
  149. }
  150. /**
  151. * Get the cc address of this message.
  152. *
  153. * @return array
  154. */
  155. public function getCc(): array {
  156. return $this->swiftMessage->getCc();
  157. }
  158. /**
  159. * Set the BCC recipients of this message.
  160. *
  161. * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
  162. * @return $this
  163. */
  164. public function setBcc(array $recipients): IMessage {
  165. $recipients = $this->convertAddresses($recipients);
  166. $this->swiftMessage->setBcc($recipients);
  167. return $this;
  168. }
  169. /**
  170. * Get the Bcc address of this message.
  171. *
  172. * @return array
  173. */
  174. public function getBcc(): array {
  175. return $this->swiftMessage->getBcc();
  176. }
  177. /**
  178. * Set the subject of this message.
  179. *
  180. * @param string $subject
  181. * @return IMessage
  182. */
  183. public function setSubject(string $subject): IMessage {
  184. $this->swiftMessage->setSubject($subject);
  185. return $this;
  186. }
  187. /**
  188. * Get the from subject of this message.
  189. *
  190. * @return string
  191. */
  192. public function getSubject(): string {
  193. return $this->swiftMessage->getSubject();
  194. }
  195. /**
  196. * Set the plain-text body of this message.
  197. *
  198. * @param string $body
  199. * @return $this
  200. */
  201. public function setPlainBody(string $body): IMessage {
  202. $this->swiftMessage->setBody($body);
  203. return $this;
  204. }
  205. /**
  206. * Get the plain body of this message.
  207. *
  208. * @return string
  209. */
  210. public function getPlainBody(): string {
  211. return $this->swiftMessage->getBody();
  212. }
  213. /**
  214. * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
  215. *
  216. * @param string $body
  217. * @return $this
  218. */
  219. public function setHtmlBody($body) {
  220. $this->swiftMessage->addPart($body, 'text/html');
  221. return $this;
  222. }
  223. /**
  224. * Get's the underlying SwiftMessage
  225. * @return Swift_Message
  226. */
  227. public function getSwiftMessage(): Swift_Message {
  228. return $this->swiftMessage;
  229. }
  230. /**
  231. * @param string $body
  232. * @param string $contentType
  233. * @return $this
  234. */
  235. public function setBody($body, $contentType) {
  236. $this->swiftMessage->setBody($body, $contentType);
  237. return $this;
  238. }
  239. /**
  240. * @param IEMailTemplate $emailTemplate
  241. * @return $this
  242. */
  243. public function useTemplate(IEMailTemplate $emailTemplate): IMessage {
  244. $this->setSubject($emailTemplate->renderSubject());
  245. $this->setPlainBody($emailTemplate->renderText());
  246. $this->setHtmlBody($emailTemplate->renderHtml());
  247. return $this;
  248. }
  249. }