message.php 5.4 KB

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