Message.php 8.6 KB

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