mailnotifications.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author scolebrook <scolebrook@mac.com>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Tom Needham <tom@owncloud.com>
  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\Share;
  31. use DateTime;
  32. use OCP\IL10N;
  33. use OCP\IURLGenerator;
  34. use OCP\IUser;
  35. use OCP\Mail\IMailer;
  36. use OCP\ILogger;
  37. use OCP\Defaults;
  38. use OCP\Util;
  39. /**
  40. * Class MailNotifications
  41. *
  42. * @package OC\Share
  43. */
  44. class MailNotifications {
  45. /** @var IUser sender userId */
  46. private $user;
  47. /** @var string sender email address */
  48. private $replyTo;
  49. /** @var string */
  50. private $senderDisplayName;
  51. /** @var IL10N */
  52. private $l;
  53. /** @var IMailer */
  54. private $mailer;
  55. /** @var Defaults */
  56. private $defaults;
  57. /** @var ILogger */
  58. private $logger;
  59. /** @var IURLGenerator */
  60. private $urlGenerator;
  61. /**
  62. * @param IUser $user
  63. * @param IL10N $l10n
  64. * @param IMailer $mailer
  65. * @param ILogger $logger
  66. * @param Defaults $defaults
  67. * @param IURLGenerator $urlGenerator
  68. */
  69. public function __construct(IUser $user,
  70. IL10N $l10n,
  71. IMailer $mailer,
  72. ILogger $logger,
  73. Defaults $defaults,
  74. IURLGenerator $urlGenerator) {
  75. $this->l = $l10n;
  76. $this->user = $user;
  77. $this->mailer = $mailer;
  78. $this->logger = $logger;
  79. $this->defaults = $defaults;
  80. $this->urlGenerator = $urlGenerator;
  81. $this->replyTo = $this->user->getEMailAddress();
  82. $this->senderDisplayName = $this->user->getDisplayName();
  83. }
  84. /**
  85. * inform users if a file was shared with them
  86. *
  87. * @param IUser[] $recipientList list of recipients
  88. * @param string $itemSource shared item source
  89. * @param string $itemType shared item type
  90. * @return array list of user to whom the mail send operation failed
  91. */
  92. public function sendInternalShareMail($recipientList, $itemSource, $itemType) {
  93. $noMail = [];
  94. foreach ($recipientList as $recipient) {
  95. $recipientDisplayName = $recipient->getDisplayName();
  96. $to = $recipient->getEMailAddress();
  97. if ($to === '') {
  98. $noMail[] = $recipientDisplayName;
  99. continue;
  100. }
  101. $items = $this->getItemSharedWithUser($itemSource, $itemType, $recipient);
  102. $filename = trim($items[0]['file_target'], '/');
  103. $subject = (string) $this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename));
  104. $expiration = null;
  105. if (isset($items[0]['expiration'])) {
  106. try {
  107. $date = new DateTime($items[0]['expiration']);
  108. $expiration = $date->getTimestamp();
  109. } catch (\Exception $e) {
  110. $this->logger->error("Couldn't read date: ".$e->getMessage(), ['app' => 'sharing']);
  111. }
  112. }
  113. // Link to folder, or root folder if a file
  114. if ($itemType === 'folder') {
  115. $args = array(
  116. 'dir' => $filename,
  117. );
  118. } else if (strpos($filename, '/')) {
  119. $args = array(
  120. 'dir' => '/' . dirname($filename),
  121. 'scrollto' => basename($filename),
  122. );
  123. } else {
  124. $args = array(
  125. 'dir' => '/',
  126. 'scrollto' => $filename,
  127. );
  128. }
  129. $link = $this->urlGenerator->linkToRouteAbsolute(
  130. 'files.view.index',
  131. $args
  132. );
  133. list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, 'internal');
  134. // send it out now
  135. try {
  136. $message = $this->mailer->createMessage();
  137. $message->setSubject($subject);
  138. $message->setTo([$to => $recipientDisplayName]);
  139. $message->setHtmlBody($htmlBody);
  140. $message->setPlainBody($textBody);
  141. $message->setFrom([
  142. Util::getDefaultEmailAddress('sharing-noreply') =>
  143. (string)$this->l->t('%s via %s', [
  144. $this->senderDisplayName,
  145. $this->defaults->getName()
  146. ]),
  147. ]);
  148. if(!is_null($this->replyTo)) {
  149. $message->setReplyTo([$this->replyTo]);
  150. }
  151. $this->mailer->send($message);
  152. } catch (\Exception $e) {
  153. $this->logger->error("Can't send mail to inform the user about an internal share: ".$e->getMessage(), ['app' => 'sharing']);
  154. $noMail[] = $recipientDisplayName;
  155. }
  156. }
  157. return $noMail;
  158. }
  159. /**
  160. * inform recipient about public link share
  161. *
  162. * @param string $recipient recipient email address
  163. * @param string $filename the shared file
  164. * @param string $link the public link
  165. * @param int $expiration expiration date (timestamp)
  166. * @return string[] $result of failed recipients
  167. */
  168. public function sendLinkShareMail($recipient, $filename, $link, $expiration) {
  169. $subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]);
  170. list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration);
  171. $recipient = str_replace([', ', '; ', ',', ';', ' '], ',', $recipient);
  172. $recipients = explode(',', $recipient);
  173. try {
  174. $message = $this->mailer->createMessage();
  175. $message->setSubject($subject);
  176. $message->setTo($recipients);
  177. $message->setHtmlBody($htmlBody);
  178. $message->setPlainBody($textBody);
  179. $message->setFrom([
  180. Util::getDefaultEmailAddress('sharing-noreply') =>
  181. (string)$this->l->t('%s via %s', [
  182. $this->senderDisplayName,
  183. $this->defaults->getName()
  184. ]),
  185. ]);
  186. if(!is_null($this->replyTo)) {
  187. $message->setReplyTo([$this->replyTo]);
  188. }
  189. return $this->mailer->send($message);
  190. } catch (\Exception $e) {
  191. $this->logger->error("Can't send mail with public link to $recipient: ".$e->getMessage(), ['app' => 'sharing']);
  192. return [$recipient];
  193. }
  194. }
  195. /**
  196. * create mail body for plain text and html mail
  197. *
  198. * @param string $filename the shared file
  199. * @param string $link link to the shared file
  200. * @param int $expiration expiration date (timestamp)
  201. * @param string $prefix prefix of mail template files
  202. * @return array an array of the html mail body and the plain text mail body
  203. */
  204. private function createMailBody($filename, $link, $expiration, $prefix = '') {
  205. $formattedDate = $expiration ? $this->l->l('date', $expiration) : null;
  206. $html = new \OC_Template('core', $prefix . 'mail', '');
  207. $html->assign ('link', $link);
  208. $html->assign ('user_displayname', $this->senderDisplayName);
  209. $html->assign ('filename', $filename);
  210. $html->assign('expiration', $formattedDate);
  211. $htmlMail = $html->fetchPage();
  212. $plainText = new \OC_Template('core', $prefix . 'altmail', '');
  213. $plainText->assign ('link', $link);
  214. $plainText->assign ('user_displayname', $this->senderDisplayName);
  215. $plainText->assign ('filename', $filename);
  216. $plainText->assign('expiration', $formattedDate);
  217. $plainTextMail = $plainText->fetchPage();
  218. return [$htmlMail, $plainTextMail];
  219. }
  220. /**
  221. * @param string $itemSource
  222. * @param string $itemType
  223. * @param IUser $recipient
  224. * @return array
  225. */
  226. protected function getItemSharedWithUser($itemSource, $itemType, $recipient) {
  227. return Share::getItemSharedWithUser($itemType, $itemSource, $recipient->getUID());
  228. }
  229. }