Message.php 5.9 KB

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