diff options
author | Lukas Reschke <lukas@owncloud.com> | 2015-04-10 17:21:52 +0200 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2015-04-10 17:30:07 +0200 |
commit | e3ad99d2523e8614b6d84765fc16104b86e9730a (patch) | |
tree | ff91239dbd59dc3953e6999222b1e0a97207d735 /lib/private/share/mailnotifications.php | |
parent | a881218fc25c1f650e7b381e3acf2007fdc45c38 (diff) | |
download | nextcloud-server-e3ad99d2523e8614b6d84765fc16104b86e9730a.tar.gz nextcloud-server-e3ad99d2523e8614b6d84765fc16104b86e9730a.zip |
Add "Reply-To" support to sharing mails and refactor code
Diffstat (limited to 'lib/private/share/mailnotifications.php')
-rw-r--r-- | lib/private/share/mailnotifications.php | 160 |
1 files changed, 100 insertions, 60 deletions
diff --git a/lib/private/share/mailnotifications.php b/lib/private/share/mailnotifications.php index 68f2e66a098..1c15b6e3e1d 100644 --- a/lib/private/share/mailnotifications.php +++ b/lib/private/share/mailnotifications.php @@ -6,6 +6,7 @@ * @author Robin McCorkell <rmccorkell@karoshi.org.uk> * @author Thomas Müller <thomas.mueller@tmit.eu> * @author Tom Needham <tom@owncloud.com> + * @author Lukas Reschke <lukas@owncloud.com> * * @copyright Copyright (c) 2015, ownCloud, Inc. * @license AGPL-3.0 @@ -27,47 +28,59 @@ namespace OC\Share; use DateTime; +use OCP\IConfig; +use OCP\IL10N; +use OCP\Mail\IMailer; +use OCP\ILogger; +use OCP\Defaults; +/** + * Class MailNotifications + * + * @package OC\Share + */ class MailNotifications { - /** - * sender userId - * @var null|string - */ - private $senderId; - - /** - * sender email address - * @var string - */ - private $from; - - /** - * @var string - */ + /** @var string sender userId */ + private $userId; + /** @var string sender email address */ + private $replyTo; + /** @var string */ private $senderDisplayName; - - /** - * @var \OC_L10N - */ + /** @var IL10N */ private $l; + /** @var IConfig */ + private $config; + /** @var IMailer */ + private $mailer; + /** @var Defaults */ + private $defaults; + /** @var ILogger */ + private $logger; /** - * - * @param string $sender user id (if nothing is set we use the currently logged-in user) + * @param string $uid user id + * @param IConfig $config + * @param IL10N $l10n + * @param IMailer $mailer + * @param ILogger $logger + * @param Defaults $defaults */ - public function __construct($sender = null) { - $this->l = \OC::$server->getL10N('lib'); - - $this->senderId = $sender; - - $this->from = \OCP\Util::getDefaultEmailAddress('sharing-noreply'); - if ($this->senderId) { - $this->from = \OCP\Config::getUserValue($this->senderId, 'settings', 'email', $this->from); - $this->senderDisplayName = \OCP\User::getDisplayName($this->senderId); - } else { - $this->senderDisplayName = \OCP\User::getDisplayName(); - } + public function __construct($uid, + IConfig $config, + IL10N $l10n, + IMailer $mailer, + ILogger $logger, + Defaults $defaults) { + $this->l = $l10n; + $this->userId = $uid; + $this->config = $config; + $this->mailer = $mailer; + $this->logger = $logger; + $this->defaults = $defaults; + + $this->replyTo = $this->config->getUserValue($this->userId, 'settings', 'email', null); + $this->senderDisplayName = \OCP\User::getDisplayName($this->userId); } /** @@ -79,12 +92,11 @@ class MailNotifications { * @return array list of user to whom the mail send operation failed */ public function sendInternalShareMail($recipientList, $itemSource, $itemType) { - - $noMail = array(); + $noMail = []; foreach ($recipientList as $recipient) { $recipientDisplayName = \OCP\User::getDisplayName($recipient); - $to = \OC::$server->getConfig()->getUserValue($recipient, 'settings', 'email', ''); + $to = $this->config->getUserValue($recipient, 'settings', 'email', ''); if ($to === '') { $noMail[] = $recipientDisplayName; @@ -100,7 +112,7 @@ class MailNotifications { $date = new DateTime($items[0]['expiration']); $expiration = $date->getTimestamp(); } catch (\Exception $e) { - \OCP\Util::writeLog('sharing', "Couldn't read date: " . $e->getMessage(), \OCP\Util::ERROR); + $this->logger->error("Couldn't read date: ".$e->getMessage(), ['app' => 'sharing']); } } @@ -119,13 +131,29 @@ class MailNotifications { $link = \OCP\Util::linkToAbsolute('files', 'index.php', $args); - list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration); + list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration); // send it out now try { - \OCP\Util::sendMail($to, $recipientDisplayName, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail); + $message = $this->mailer->createMessage(); + $message->setSubject($subject); + $message->setTo([$to => $recipientDisplayName]); + $message->setHtmlBody($htmlBody); + $message->setPlainBody($textBody); + $message->setFrom([ + \OCP\Util::getDefaultEmailAddress('sharing-noreply') => + (string)$this->l->t('%s via %s', [ + $this->senderDisplayName, + $this->defaults->getName() + ]), + ]); + if(!is_null($this->replyTo)) { + $message->setReplyTo([$this->replyTo]); + } + + $this->mailer->send($message); } catch (\Exception $e) { - \OCP\Util::writeLog('sharing', "Can't send mail to inform the user about an internal share: " . $e->getMessage() , \OCP\Util::ERROR); + $this->logger->error("Can't send mail to inform the user about an internal share: ".$e->getMessage(), ['app' => 'sharing']); $noMail[] = $recipientDisplayName; } } @@ -144,19 +172,31 @@ class MailNotifications { * @return array $result of failed recipients */ public function sendLinkShareMail($recipient, $filename, $link, $expiration) { - $subject = (string)$this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename)); - list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration); - $rs = explode(' ', $recipient); - $failed = array(); - foreach ($rs as $r) { - try { - \OCP\Util::sendMail($r, $r, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail); - } catch (\Exception $e) { - \OCP\Util::writeLog('sharing', "Can't send mail with public link to $r: " . $e->getMessage(), \OCP\Util::ERROR); - $failed[] = $r; + $subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]); + list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration); + + try { + $message = $this->mailer->createMessage(); + $message->setSubject($subject); + $message->setTo([$recipient]); + $message->setHtmlBody($htmlBody); + $message->setPlainBody($textBody); + $message->setFrom([ + \OCP\Util::getDefaultEmailAddress('sharing-noreply') => + (string)$this->l->t('%s via %s', [ + $this->senderDisplayName, + $this->defaults->getName() + ]), + ]); + if(!is_null($this->replyTo)) { + $message->setReplyTo([$this->replyTo]); } + + return $this->mailer->send($message); + } catch (\Exception $e) { + $this->logger->error("Can't send mail with public link to $recipient: ".$e->getMessage(), ['app' => 'sharing']); + return [$recipient]; } - return $failed; } /** @@ -169,23 +209,23 @@ class MailNotifications { */ private function createMailBody($filename, $link, $expiration) { - $formatedDate = $expiration ? $this->l->l('date', $expiration) : null; + $formattedDate = $expiration ? $this->l->l('date', $expiration) : null; $html = new \OC_Template("core", "mail", ""); $html->assign ('link', $link); $html->assign ('user_displayname', $this->senderDisplayName); $html->assign ('filename', $filename); - $html->assign('expiration', $formatedDate); + $html->assign('expiration', $formattedDate); $htmlMail = $html->fetchPage(); - $alttext = new \OC_Template("core", "altmail", ""); - $alttext->assign ('link', $link); - $alttext->assign ('user_displayname', $this->senderDisplayName); - $alttext->assign ('filename', $filename); - $alttext->assign('expiration', $formatedDate); - $alttextMail = $alttext->fetchPage(); + $plainText = new \OC_Template("core", "altmail", ""); + $plainText->assign ('link', $link); + $plainText->assign ('user_displayname', $this->senderDisplayName); + $plainText->assign ('filename', $filename); + $plainText->assign('expiration', $formattedDate); + $plainTextMail = $plainText->fetchPage(); - return array($htmlMail, $alttextMail); + return [$htmlMail, $plainTextMail]; } } |