Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions apps/sharebymail/lib/ShareByMailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,6 @@ protected function sendMailNotification($filename,
\DateTime $expiration = null) {
$initiatorUser = $this->userManager->get($initiator);
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
$subject = (string)$this->l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename));

$message = $this->mailer->createMessage();

$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [
Expand All @@ -392,6 +390,7 @@ protected function sendMailNotification($filename,
'shareWith' => $shareWith,
]);

$emailTemplate->setSubject($this->l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)));
$emailTemplate->addHeader();
$emailTemplate->addHeading($this->l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false);
$text = $this->l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]);
Expand Down Expand Up @@ -428,9 +427,7 @@ protected function sendMailNotification($filename,
$emailTemplate->addFooter();
}

$message->setSubject($subject);
$message->setPlainBody($emailTemplate->renderText());
$message->setHtmlBody($emailTemplate->renderHtml());
$message->useTemplate($emailTemplate);
$this->mailer->send($message);
}

Expand All @@ -455,7 +452,6 @@ protected function sendPassword(IShare $share, $password) {
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;

$subject = (string)$this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName]);
$plainBodyPart = $this->l->t("%s shared »%s« with you.\nYou should have already received a separate mail with a link to access it.\n", [$initiatorDisplayName, $filename]);
$htmlBodyPart = $this->l->t('%s shared »%s« with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]);

Expand All @@ -468,6 +464,8 @@ protected function sendPassword(IShare $share, $password) {
'initiatorEmail' => $initiatorEmailAddress,
'shareWith' => $shareWith,
]);

$emailTemplate->setSubject($this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName]));
$emailTemplate->addHeader();
$emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
$emailTemplate->addBodyText($htmlBodyPart, $plainBodyPart);
Expand All @@ -491,9 +489,7 @@ protected function sendPassword(IShare $share, $password) {
}

$message->setTo([$shareWith]);
$message->setSubject($subject);
$message->setBody($emailTemplate->renderText(), 'text/plain');
$message->setHtmlBody($emailTemplate->renderHtml());
$message->useTemplate($emailTemplate);
$this->mailer->send($message);

$this->createPasswordSendActivity($share, $shareWith, false);
Expand Down Expand Up @@ -524,7 +520,6 @@ protected function sendPasswordToOwner(IShare $share, $password) {
);
}

$subject = (string)$this->l->t('Password to access »%s« shared with %s', [$filename, $shareWith]);
$bodyPart = $this->l->t("You just shared »%s« with %s. The share was already send to the recipient. Due to the security policies defined by the administrator of %s each share needs to be protected by password and it is not allowed to send the password directly to the recipient. Therefore you need to forward the password manually to the recipient.", [$filename, $shareWith, $this->defaults->getName()]);

$message = $this->mailer->createMessage();
Expand All @@ -536,6 +531,7 @@ protected function sendPasswordToOwner(IShare $share, $password) {
'shareWith' => $shareWith,
]);

$emailTemplate->setSubject($this->l->t('Password to access »%s« shared with %s', [$filename, $shareWith]));
$emailTemplate->addHeader();
$emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
$emailTemplate->addBodyText($bodyPart);
Expand All @@ -547,9 +543,7 @@ protected function sendPasswordToOwner(IShare $share, $password) {
$message->setFrom([$initiatorEMailAddress => $initiatorDisplayName]);
}
$message->setTo([$initiatorEMailAddress => $initiatorDisplayName]);
$message->setSubject($subject);
$message->setBody($emailTemplate->renderText(), 'text/plain');
$message->setHtmlBody($emailTemplate->renderHtml());
$message->useTemplate($emailTemplate);
$this->mailer->send($message);

$this->createPasswordSendActivity($share, $shareWith, true);
Expand Down
40 changes: 8 additions & 32 deletions apps/sharebymail/tests/ShareByMailProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,26 +835,14 @@ public function testSendMailNotificationWithSameUserAndUserEmail() {
->expects($this->once())
->method('addFooter')
->with('UnitTestCloud - Testing like 1990');
$message
->expects($this->once())
->method('setSubject')
->willReturn('Mrs. Owner User shared »file.txt« with you');
$template
->expects($this->once())
->method('renderText')
->willReturn('Text Render');
$message
->expects($this->once())
->method('setPlainBody')
->with('Text Render');
$template
->expects($this->once())
->method('renderHtml')
->willReturn('HTML Render');
->method('setSubject')
->with('Mrs. Owner User shared »file.txt« with you');
$message
->expects($this->once())
->method('setHtmlBody')
->with('HTML Render');
->method('useTemplate')
->with($template);
$this->mailer
->expects($this->once())
->method('send')
Expand Down Expand Up @@ -936,26 +924,14 @@ public function testSendMailNotificationWithDifferentUserAndNoUserEmail() {
->expects($this->once())
->method('addFooter')
->with('');
$message
->expects($this->once())
->method('setSubject')
->willReturn('Mr. Initiator User shared »file.txt« with you');
$template
->expects($this->once())
->method('renderText')
->willReturn('Text Render');
$message
->expects($this->once())
->method('setPlainBody')
->with('Text Render');
$template
->expects($this->once())
->method('renderHtml')
->willReturn('HTML Render');
->method('setSubject')
->with('Mr. Initiator User shared »file.txt« with you');
$message
->expects($this->once())
->method('setHtmlBody')
->with('HTML Render');
->method('useTemplate')
->with($template);
$this->mailer
->expects($this->once())
->method('send')
Expand Down
5 changes: 2 additions & 3 deletions core/Controller/LostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ protected function sendEmail($input) {
'link' => $link,
]);

$emailTemplate->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()]));
$emailTemplate->addHeader();
$emailTemplate->addHeading($this->l10n->t('Password reset'));

Expand All @@ -327,10 +328,8 @@ protected function sendEmail($input) {
try {
$message = $this->mailer->createMessage();
$message->setTo([$email => $user->getUID()]);
$message->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()]));
$message->setPlainBody($emailTemplate->renderText());
$message->setHtmlBody($emailTemplate->renderHtml());
$message->setFrom([$this->from => $this->defaults->getName()]);
$message->useTemplate($emailTemplate);
$this->mailer->send($message);
} catch (\Exception $e) {
throw new \Exception($this->l10n->t(
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
'OCP\\Lockdown\\ILockdownManager' => $baseDir . '/lib/public/Lockdown/ILockdownManager.php',
'OCP\\Mail\\IEMailTemplate' => $baseDir . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IMailer' => $baseDir . '/lib/public/Mail/IMailer.php',
'OCP\\Mail\\IMessage' => $baseDir . '/lib/public/Mail/IMessage.php',
'OCP\\Migration\\BigIntMigration' => $baseDir . '/lib/public/Migration/BigIntMigration.php',
'OCP\\Migration\\IMigrationStep' => $baseDir . '/lib/public/Migration/IMigrationStep.php',
'OCP\\Migration\\IOutput' => $baseDir . '/lib/public/Migration/IOutput.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Lockdown\\ILockdownManager' => __DIR__ . '/../../..' . '/lib/public/Lockdown/ILockdownManager.php',
'OCP\\Mail\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IMailer' => __DIR__ . '/../../..' . '/lib/public/Mail/IMailer.php',
'OCP\\Mail\\IMessage' => __DIR__ . '/../../..' . '/lib/public/Mail/IMessage.php',
'OCP\\Migration\\BigIntMigration' => __DIR__ . '/../../..' . '/lib/public/Migration/BigIntMigration.php',
'OCP\\Migration\\IMigrationStep' => __DIR__ . '/../../..' . '/lib/public/Migration/IMigrationStep.php',
'OCP\\Migration\\IOutput' => __DIR__ . '/../../..' . '/lib/public/Migration/IOutput.php',
Expand Down
20 changes: 20 additions & 0 deletions lib/private/Mail/EMailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class EMailTemplate implements IEMailTemplate {
/** @var array */
protected $data;

/** @var string */
protected $subject = '';
/** @var string */
protected $htmlBody = '';
/** @var string */
Expand Down Expand Up @@ -358,6 +360,15 @@ public function __construct(Defaults $themingDefaults,
$this->data = $data;
}

/**
* Sets the subject of the email
*
* @param string $subject
*/
public function setSubject($subject) {
$this->subject = $subject;
}

/**
* Adds a header to the email
*/
Expand Down Expand Up @@ -595,6 +606,15 @@ public function addFooter($text = '') {
$this->plainBody .= str_replace('<br>', PHP_EOL, $text);
}

/**
* Returns the rendered email subject as string
*
* @return string
*/
public function renderSubject() {
return $this->subject;
}

/**
* Returns the rendered HTML email as string
*
Expand Down
7 changes: 4 additions & 3 deletions lib/private/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
use OCP\ILogger;
use OCP\Mail\IMessage;

/**
* Class Mailer provides some basic functions to create a mail message that can be used in combination with
Expand Down Expand Up @@ -84,7 +85,7 @@ public function __construct(IConfig $config,
/**
* Creates a new message object that can be passed to send()
*
* @return Message
* @return IMessage
*/
public function createMessage() {
return new Message(new \Swift_Message());
Expand Down Expand Up @@ -124,13 +125,13 @@ public function createEMailTemplate($emailId, array $data = []) {
* Send the specified message. Also sets the from address to the value defined in config.php
* if no-one has been passed.
*
* @param Message $message Message to send
* @param IMessage|Message $message Message to send
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMessage is fully enough here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because we use methods of the non public api in here ;)

* @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and
* therefore should be considered
* @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address
* has been supplied.)
*/
public function send(Message $message) {
public function send(IMessage $message) {
$debugMode = $this->config->getSystemValue('mail_smtpdebug', false);

if (empty($message->getFrom())) {
Expand Down
15 changes: 14 additions & 1 deletion lib/private/Mail/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@

namespace OC\Mail;

use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMessage;
use Swift_Message;

/**
* Class Message provides a wrapper around SwiftMail
*
* @package OC\Mail
*/
class Message {
class Message implements IMessage {
/** @var Swift_Message */
private $swiftMessage;

Expand Down Expand Up @@ -250,4 +252,15 @@ public function setBody($body, $contentType) {
$this->swiftMessage->setBody($body, $contentType);
return $this;
}

/**
* @param IEMailTemplate $emailTemplate
* @return $this
*/
public function useTemplate(IEMailTemplate $emailTemplate) {
$this->setSubject($emailTemplate->renderSubject());
$this->setPlainBody($emailTemplate->renderText());
$this->setHtmlBody($emailTemplate->renderHtml());
return $this;
}
}
6 changes: 2 additions & 4 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,6 @@ protected function sendMailNotification(IL10N $l,
\DateTime $expiration = null) {
$initiatorUser = $this->userManager->get($initiator);
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
$subject = $l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename));

$message = $this->mailer->createMessage();

Expand All @@ -714,6 +713,7 @@ protected function sendMailNotification(IL10N $l,
'shareWith' => $shareWith,
]);

$emailTemplate->setSubject($l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)));
$emailTemplate->addHeader();
$emailTemplate->addHeading($l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false);
$text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]);
Expand Down Expand Up @@ -750,9 +750,7 @@ protected function sendMailNotification(IL10N $l,
$emailTemplate->addFooter();
}

$message->setSubject($subject);
$message->setPlainBody($emailTemplate->renderText());
$message->setHtmlBody($emailTemplate->renderHtml());
$message->useTemplate($emailTemplate);
$this->mailer->send($message);
}

Expand Down
18 changes: 18 additions & 0 deletions lib/public/Mail/IEMailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
*/
interface IEMailTemplate {

/**
* Sets the subject of the email
*
* @param string $subject
*
* @since 13.0.0
*/
public function setSubject($subject);

/**
* Adds a header to the email
*
Expand Down Expand Up @@ -130,6 +139,15 @@ public function addBodyButton($text, $url, $plainText = '');
*/
public function addFooter($text = '');

/**
* Returns the rendered email subject as string
*
* @return string
*
* @since 13.0.0
*/
public function renderSubject();

/**
* Returns the rendered HTML email as string
*
Expand Down
9 changes: 4 additions & 5 deletions lib/public/Mail/IMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

namespace OCP\Mail;
use OC\Mail\Message;

/**
* Class IMailer provides some basic functions to create a mail message that can be used in combination with
Expand All @@ -34,7 +33,7 @@
* $mailer = \OC::$server->getMailer();
* $message = $mailer->createMessage();
* $message->setSubject('Your Subject');
* $message->setFrom(['[email protected]' => 'ownCloud Notifier']);
* $message->setFrom(['[email protected]' => 'Nextcloud Notifier']);
* $message->setTo(['[email protected]' => 'Recipient']);
* $message->setPlainBody('The message text');
* $message->setHtmlBody('The <strong>message</strong> text');
Expand All @@ -49,7 +48,7 @@ interface IMailer {
/**
* Creates a new message object that can be passed to send()
*
* @return Message
* @return IMessage
* @since 8.1.0
*/
public function createMessage();
Expand All @@ -68,14 +67,14 @@ public function createEMailTemplate($emailId, array $data = []);
* Send the specified message. Also sets the from address to the value defined in config.php
* if no-one has been passed.
*
* @param Message $message Message to send
* @param IMessage $message Message to send
* @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and
* therefore should be considered
* @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address
* has been supplied.)
* @since 8.1.0
*/
public function send(Message $message);
public function send(IMessage $message);

/**
* Checks if an e-mail address is valid
Expand Down
Loading