Skip to content

Commit

Permalink
Merge pull request #30813 from owncloud/stable10-share-link-email-emb…
Browse files Browse the repository at this point in the history
…edded-note

[stable10] Embed personal note in link share email
  • Loading branch information
DeepDiver1975 authored Mar 19, 2018
2 parents 30dc271 + 828d968 commit d386050
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 26 deletions.
33 changes: 17 additions & 16 deletions core/ajax/share.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,26 @@ function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
break;

case 'email':
$emailBody = null;

if (isset($_POST['emailBody'])) {
$emailBody = trim((string)$_POST['emailBody']);
}

// read and filter post variables
$filter = new MailNotificationFilter([
'link' => $_POST['link'],
'file' => $_POST['file'],
'toAddress' => $_POST['toaddress'],
'expiration' => $_POST['expiration']
'toAddress' => $_POST['toAddress'],
'expiration' => $_POST['expiration'],
'personalNote' => $emailBody
]);

// read post variables
$link = (string)$_POST['link'];
$file = (string)$_POST['file'];
$toAddress = (string)$_POST['toAddress'];
$options = array();
$emailBody = null;

if (isset($_POST['emailBody'])) {
$emailBody = trim((string)$_POST['emailBody']);
}

if (isset($_POST['bccSelf']) && $_POST['bccSelf'] === 'true') {
$options['bcc'] = \OC::$server->getUserSession()->getUser()->getEMailAddress();
Expand Down Expand Up @@ -207,16 +208,16 @@ function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
}
}

$subject = (string)$l10n->t('%s shared »%s« with you', [$sendingUser->getDisplayName(), $filter->getFile()]);
if ($emailBody === null || $emailBody === '') {
list($htmlBody, $textBody) = $mailNotification->createMailBody($file, $link, $expiration);
} else {
$htmlBody = null;
$textBody = strip_tags($emailBody);
if ($emailBody !== null || $emailBody !== '') {
$emailBody = strip_tags($emailBody);
}

$result = $mailNotification->sendLinkShareMailFromBody($toAddress, $subject, $htmlBody, $textBody, $options);

$result = $mailNotification->sendLinkShareMail(
$filter->getToAddress(),
$filter->getFile(),
$filter->getLink(),
$filter->getExpirationDate(),
$filter->getPersonalNote()
);
if(empty($result)) {
// Get the token from the link
$linkParts = explode('/', $link);
Expand Down
5 changes: 5 additions & 0 deletions core/templates/altmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
print_unescaped($l->t("The share will expire on %s.", [$_['expiration']]));
print_unescaped("\n\n");
}
if (isset($_['personal_note'])) {
// TRANSLATORS personal note in share notification email
print_unescaped($l->t("Personal note from the sender: %s.", [$_['personal_note']]));
print_unescaped("\n\n");
}
// TRANSLATORS term at the end of a mail
p($l->t("Cheers!"));
?>
Expand Down
6 changes: 6 additions & 0 deletions core/templates/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
p($l->t("The share will expire on %s.", [$_['expiration']]));
print_unescaped('<br><br>');
}

if (isset($_['personal_note'])) {
// TRANSLATORS personal note in share notification email
p($l->t("Personal note from the sender: %s.", [$_['personal_note']]));
print_unescaped('<br><br>');
}
// TRANSLATORS term at the end of a mail
p($l->t('Cheers!'));
?>
Expand Down
14 changes: 14 additions & 0 deletions lib/private/Share/Filters/MailNotificationFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public function __construct($data = []) {
['name' => 'StripTags'],
],
],
'personalNote' => [
'required' => false,
'filters' => [
['name' => 'StringTrim'],
['name' => 'StripTags'],
],
],
'expirationDate' => [
'required' => true,
'filters' => [
Expand Down Expand Up @@ -108,4 +115,11 @@ public function getToAddress() {
public function getExpirationDate() {
return $this->inputFilter->getValue('expirationDate');
}

/**
* @return string The filtered personal note
*/
public function getPersonalNote() {
return $this->inputFilter->getValue('personalNote');
}
}
27 changes: 17 additions & 10 deletions lib/private/Share/MailNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function sendInternalShareMail($recipientList, $itemSource, $itemType) {
['fileId' => $items[0]['item_source']]
);

list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, 'internal');
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, null, 'internal');

// send it out now
try {
Expand Down Expand Up @@ -169,9 +169,9 @@ public function sendInternalShareMail($recipientList, $itemSource, $itemType) {

}

public function sendLinkShareMail($recipient, $filename, $link, $expiration) {
public function sendLinkShareMail($recipient, $filename, $link, $expiration, $personalNote = null) {
$subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]);
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration);
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, $personalNote);

return $this->sendLinkShareMailFromBody($recipient, $subject, $htmlBody, $textBody);
}
Expand Down Expand Up @@ -230,24 +230,31 @@ public function sendLinkShareMailFromBody($recipient, $subject, $htmlBody, $text
* @param string $filename the shared file
* @param string $link link to the shared file
* @param int $expiration expiration date (timestamp)
* @param string $personalNote optional personal note
* @param string $prefix prefix of mail template files
* @return array an array of the html mail body and the plain text mail body
*/
public function createMailBody($filename, $link, $expiration, $prefix = '') {
public function createMailBody($filename, $link, $expiration, $personalNote = null, $prefix = '') {
$formattedDate = $expiration ? $this->l->l('date', $expiration) : null;

$html = new \OC_Template('core', $prefix . 'mail', '');
$html->assign ('link', $link);
$html->assign ('user_displayname', $this->senderDisplayName);
$html->assign ('filename', $filename);
$html->assign('link', $link);
$html->assign('user_displayname', $this->senderDisplayName);
$html->assign('filename', $filename);
$html->assign('expiration', $formattedDate);
if ($personalNote !== null && $personalNote !== '') {
$html->assign('personal_note', $personalNote);
}
$htmlMail = $html->fetchPage();

$plainText = new \OC_Template('core', $prefix . 'altmail', '');
$plainText->assign ('link', $link);
$plainText->assign ('user_displayname', $this->senderDisplayName);
$plainText->assign ('filename', $filename);
$plainText->assign('link', $link);
$plainText->assign('user_displayname', $this->senderDisplayName);
$plainText->assign('filename', $filename);
$plainText->assign('expiration', $formattedDate);
if ($personalNote !== null && $personalNote !== '') {
$plainText->assign('personal_note', $personalNote);
}
$plainTextMail = $plainText->fetchPage();

return [$htmlMail, $plainTextMail];
Expand Down
49 changes: 49 additions & 0 deletions tests/lib/Share/MailNotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,55 @@ public function testSendLinkShareMailWithoutReplyTo() {
$this->assertSame([], $mailNotifications->sendLinkShareMail('lukas@owncloud.com', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
}

public function testSendLinkShareMailPersonalNote() {
$message = $this->getMockBuilder('\OC\Mail\Message')
->disableOriginalConstructor()->getMock();

$message
->expects($this->once())
->method('setSubject')
->with('TestUser shared »MyFile« with you');
$message
->expects($this->once())
->method('setTo')
->with(['lukas@owncloud.com']);
$message
->expects($this->once())
->method('setHtmlBody')
->with($this->stringContains('personal note'));
$message
->expects($this->once())
->method('setPlainBody')
->with($this->stringContains('personal note'));

$message
->expects($this->once())
->method('setFrom')
->with([Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser via UnitTestCloud']);

$this->mailer
->expects($this->once())
->method('createMessage')
->will($this->returnValue($message));

$this->mailer
->expects($this->once())
->method('send')
->with($message)
->will($this->returnValue([]));

$mailNotifications = new MailNotifications(
$this->user,
$this->l10n,
$this->mailer,
$this->logger,
$this->defaults,
$this->urlGenerator
);

$this->assertSame([], $mailNotifications->sendLinkShareMail('lukas@owncloud.com', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600, 'personal note'));
}

public function dataSendLinkShareMailWithReplyTo() {
return [
['lukas@owncloud.com', ['lukas@owncloud.com']],
Expand Down

0 comments on commit d386050

Please sign in to comment.