You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MailNotifications.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Vincent Petry <pvince81@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 recipient about public link share
  86. *
  87. * @param string $recipient recipient email address
  88. * @param string $filename the shared file
  89. * @param string $link the public link
  90. * @param int $expiration expiration date (timestamp)
  91. * @return string[] $result of failed recipients
  92. */
  93. public function sendLinkShareMail($recipient, $filename, $link, $expiration) {
  94. $subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]);
  95. list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration);
  96. $recipient = str_replace([', ', '; ', ',', ';', ' '], ',', $recipient);
  97. $recipients = explode(',', $recipient);
  98. try {
  99. $message = $this->mailer->createMessage();
  100. $message->setSubject($subject);
  101. $message->setTo($recipients);
  102. $message->setHtmlBody($htmlBody);
  103. $message->setPlainBody($textBody);
  104. $message->setFrom([
  105. Util::getDefaultEmailAddress('sharing-noreply') =>
  106. (string)$this->l->t('%s via %s', [
  107. $this->senderDisplayName,
  108. $this->defaults->getName()
  109. ]),
  110. ]);
  111. if(!is_null($this->replyTo)) {
  112. $message->setReplyTo([$this->replyTo]);
  113. }
  114. return $this->mailer->send($message);
  115. } catch (\Exception $e) {
  116. $this->logger->error("Can't send mail with public link to $recipient: ".$e->getMessage(), ['app' => 'sharing']);
  117. return [$recipient];
  118. }
  119. }
  120. /**
  121. * create mail body for plain text and html mail
  122. *
  123. * @param string $filename the shared file
  124. * @param string $link link to the shared file
  125. * @param int $expiration expiration date (timestamp)
  126. * @param string $prefix prefix of mail template files
  127. * @return array an array of the html mail body and the plain text mail body
  128. */
  129. private function createMailBody($filename, $link, $expiration, $prefix = '') {
  130. $formattedDate = $expiration ? $this->l->l('date', $expiration) : null;
  131. $html = new \OC_Template('core', $prefix . 'mail', '');
  132. $html->assign ('link', $link);
  133. $html->assign ('user_displayname', $this->senderDisplayName);
  134. $html->assign ('filename', $filename);
  135. $html->assign('expiration', $formattedDate);
  136. $htmlMail = $html->fetchPage();
  137. $plainText = new \OC_Template('core', $prefix . 'altmail', '');
  138. $plainText->assign ('link', $link);
  139. $plainText->assign ('user_displayname', $this->senderDisplayName);
  140. $plainText->assign ('filename', $filename);
  141. $plainText->assign('expiration', $formattedDate);
  142. $plainTextMail = $plainText->fetchPage();
  143. return [$htmlMail, $plainTextMail];
  144. }
  145. }