namespace OCA\ShareByMail;
+use OC\HintException;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\ILogger;
+use OCP\IURLGenerator;
use OCP\IUserManager;
+use OCP\Mail\IMailer;
use OCP\Security\ISecureRandom;
use OC\Share20\Share;
use OCP\Share\IShare;
/** @var IL10N */
private $l;
+ /** @var IMailer */
+ private $mailer;
+
+ /** @var IURLGenerator */
+ private $urlGenerator;
+
/**
* Return the identifier of this provider.
*
* @param IRootFolder $rootFolder
* @param IL10N $l
* @param ILogger $logger
+ * @param IMailer $mailer
+ * @param IURLGenerator $urlGenerator
*/
public function __construct(
IDBConnection $connection,
IUserManager $userManager,
IRootFolder $rootFolder,
IL10N $l,
- ILogger $logger
+ ILogger $logger,
+ IMailer $mailer,
+ IURLGenerator $urlGenerator
) {
$this->dbConnection = $connection;
$this->secureRandom = $secureRandom;
$this->rootFolder = $rootFolder;
$this->l = $l;
$this->logger = $logger;
+ $this->mailer = $mailer;
+ $this->urlGenerator = $urlGenerator;
}
/**
* @throws \Exception
*/
private function createMailShare(IShare $share) {
- $token = $this->generateToken();
+ $share->setToken($this->generateToken());
$shareId = $this->addShareToDB(
$share->getNodeId(),
$share->getNodeType(),
$share->getSharedBy(),
$share->getShareOwner(),
$share->getPermissions(),
- $token
+ $share->getToken()
);
try {
- // TODO: Send email with public link
+ $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
+ ['token' => $share->getToken()]);
+ $this->sendMailNotification($share->getNode()->getName(),
+ $link,
+ $share->getShareOwner(),
+ $share->getSharedBy(), $share->getSharedWith());
+ } catch (HintException $hintException) {
+ $this->logger->error('Failed to send share by mail: ' . $hintException->getMessage());
+ $this->removeShareFromTable($shareId);
+ throw $hintException;
} catch (\Exception $e) {
- $this->logger->error('Failed to notify remote server of federated share, removing share (' . $e->getMessage() . ')');
+ $this->logger->error('Failed to send share by mail: ' . $e->getMessage());
$this->removeShareFromTable($shareId);
- throw $e;
+ throw new HintException('Failed to send share by mail',
+ $this->l->t('Failed to send share by E-mail'));
}
return $shareId;
}
+ private function sendMailNotification($filename, $link, $owner, $initiator, $shareWith) {
+ if ($owner === $initiator) {
+ $subject = (string)$this->l->t('%s shared »%s« with you', array($owner, $filename));
+ } else {
+ $subject = (string)$this->l->t('%s shared »%s« with you on behalf of %s', array($owner, $filename, $initiator));
+ }
+
+ $message = $this->mailer->createMessage();
+ $htmlBody = $this->createMailBody('mail', $filename, $link, $owner, $initiator);
+ $textBody = $this->createMailBody('altmail', $filename, $link, $owner, $initiator);
+ $message->setTo([$shareWith]);
+ $message->setSubject($subject);
+ $message->setBody($textBody, 'text/plain');
+ $message->setHtmlBody($htmlBody);
+ $this->mailer->send($message);
+
+ }
+
+ /**
+ * create mail body
+ *
+ * @param $filename
+ * @param $link
+ * @param $owner
+ * @param $initiator
+ * @return string plain text mail
+ * @throws HintException
+ */
+ protected function createMailBody($template, $filename, $link, $owner, $initiator) {
+
+ $mailBodyTemplate = new \OC_Template('sharebymail', $template, '');
+ $mailBodyTemplate->assign ('filename', $filename);
+ $mailBodyTemplate->assign ('link', $link);
+ $mailBodyTemplate->assign ('owner', $owner);
+ $mailBodyTemplate->assign ('initiator', $initiator);
+ $mailBodyTemplate->assign ('onBehalfOf', $initiator !== $owner);
+ $mailBody = $mailBodyTemplate->fetchPage();
+
+ if (is_string($mailBody)) {
+ return $mailBody;
+ }
+
+ throw new HintException('Failed to create the E-mail',
+ $this->l->t('Failed to create the E-mail'));
+ }
+
/**
* generate share token
*
--- /dev/null
+<?php
+/**
+ * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+/** @var OC_Theme $theme */
+/** @var array $_ */
+if ($_['onBehalfOf']) {
+ print_unescaped($l->t("Hey there,\n\n%s shared »%s« with you on behalf of %s.\n\n%s\n\n", [$_['owner'], $_['filename'], $_['initiator'], $_['link']]));
+} else {
+ print_unescaped($l->t("Hey there,\n\n%s shared »%s« with you.\n\n%s\n\n", [$_['owner'], $_['filename'], $_['link']]));
+}
+// TRANSLATORS term at the end of a mail
+p($l->t("Cheers!"));
+print_unescaped("\n");
+?>
+
+--
+<?php p($theme->getName() . ' - ' . $theme->getSlogan()); ?>
+<?php print_unescaped("\n".$theme->getBaseUrl());
--- /dev/null
+<?php
+/**
+ * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+/** @var OC_Theme $theme */
+/** @var array $_ */
+?>
+
+<table cellspacing="0" cellpadding="0" border="0" width="100%">
+ <tr><td>
+ <table cellspacing="0" cellpadding="0" border="0" width="600px">
+ <tr>
+ <td bgcolor="<?php p($theme->getMailHeaderColor());?>" width="20px"> </td>
+ <td bgcolor="<?php p($theme->getMailHeaderColor());?>">
+ <img src="<?php p(\OC::$server->getURLGenerator()->getAbsoluteURL(image_path('', 'logo-mail.gif'))); ?>" alt="<?php p($theme->getName()); ?>"/>
+ </td>
+ </tr>
+ <tr><td colspan="2"> </td></tr>
+ <tr>
+ <td width="20px"> </td>
+ <td style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">
+ <?php
+ if ($_['onBehalfOf']) {
+ print_unescaped($l->t('Hey there,<br><br>%s shared <a href="%s">%s</a> with you on behalf of %s.<br><br>', [$_['owner'], $_['link'], $_['filename'], $_['initiator']]));
+ } else {
+ print_unescaped($l->t('Hey there,<br><br>%s shared <a href="%s">%s</a> with you.<br><br>', [$_['owner'], $_['link'], $_['filename']]));
+ }
+ // TRANSLATORS term at the end of a mail
+ p($l->t('Cheers!'));
+ ?>
+ </td>
+ </tr>
+ <tr><td colspan="2"> </td></tr>
+ <tr>
+ <td width="20px"> </td>
+ <td style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">--<br>
+ <?php p($theme->getName()); ?> -
+ <?php p($theme->getSlogan()); ?>
+ <br><a href="<?php p($theme->getBaseUrl()); ?>"><?php p($theme->getBaseUrl());?></a>
+ </td>
+ </tr>
+ <tr>
+ <td colspan="2"> </td>
+ </tr>
+ </table>
+ </td></tr>
+</table>