Skip to content
Draft
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
120 changes: 75 additions & 45 deletions apps/sharebymail/lib/ShareByMailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\HintException;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
Expand All @@ -27,6 +28,7 @@
use OCP\IUserManager;
use OCP\Mail\IEmailValidator;
use OCP\Mail\IMailer;
use OCP\Mail\Provider\Address;
use OCP\Security\Events\GenerateSecurePasswordEvent;
use OCP\Security\IHasher;
use OCP\Security\ISecureRandom;
Expand All @@ -39,6 +41,8 @@
use OCP\Share\IShareProviderWithNotification;
use OCP\Util;
use Psr\Log\LoggerInterface;
use OCP\Mail\Provider\IManager as IMailManager;
use OCP\Mail\Provider\IMessageSend;

/**
* Class ShareByMail
Expand All @@ -56,14 +60,16 @@ public function identifier(): string {
}

public function __construct(
private IAppConfig $appConfig,
private IConfig $config,
private IDBConnection $dbConnection,
private ISecureRandom $secureRandom,
private IUserManager $userManager,
private IRootFolder $rootFolder,
private IL10N $l,
private LoggerInterface $logger,
private IMailer $mailer,
private IMailer $systemMailer,
private IMailManager $mailManager,
private IURLGenerator $urlGenerator,
private IManager $activityManager,
private SettingsManager $settingsManager,
Expand Down Expand Up @@ -325,9 +331,9 @@ protected function sendEmail(IShare $share, array $emails): void {

$initiatorUser = $this->userManager->get($initiator);
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
$message = $this->mailer->createMessage();
$initiatorEmail = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;

$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [
$emailTemplate = $this->systemMailer->createEMailTemplate('sharebymail.RecipientNotification', [
'filename' => $filename,
'link' => $link,
'initiator' => $initiatorDisplayName,
Expand Down Expand Up @@ -363,47 +369,71 @@ protected function sendEmail(IShare $share, array $emails): void {
$link
);

// If multiple recipients are given, we send the mail to all of them
if (count($emails) > 1) {
// We do not want to expose the email addresses of the other recipients
$message->setBcc($emails);
$instanceName = $this->defaults->getName();

// Add footer - adjust "Do not reply" text if reply-to will be set
if ($initiatorUser && $this->settingsManager->replyToInitiator() && $initiatorEmail !== null) {
$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
} else {
$message->setTo($emails);
$emailTemplate->addFooter();
}

// The "From" contains the sharers name
$instanceName = $this->defaults->getName();
$senderName = $instanceName;
if ($this->settingsManager->replyToInitiator()) {
$senderName = $this->l->t(
'%1$s via %2$s',
[
$initiatorDisplayName,
$instanceName
]
);
// Try to send via the user's personal mail service
$mailService = null;
if ($this->appConfig->getValueBool('core', 'mail_providers_enabled', true)
&& $initiatorUser instanceof IUser
&& $initiatorEmail !== null) {
$mailService = $this->mailManager->findServiceByAddress($initiatorUser->getUID(), $initiatorEmail);
}
$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);

// The "Reply-To" is set to the sharer if an mail address is configured
// also the default footer contains a "Do not reply" which needs to be adjusted.
if ($initiatorUser && $this->settingsManager->replyToInitiator()) {
$initiatorEmail = $initiatorUser->getEMailAddress();
if ($initiatorEmail !== null) {
$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
} else {
$emailTemplate->addFooter();
// use personal mail service if available
if ($mailService instanceof IMessageSend) {
foreach ($emails as $email) {
$message = $mailService->initiateMessage();
$message->setFrom(
new Address($initiatorEmail, $initiatorDisplayName)
);
$message->setTo(new Address($email));
$message->setSubject($emailTemplate->renderSubject());
$message->setBodyPlain($emailTemplate->renderText());
$message->setBodyHtml($emailTemplate->renderHtml());
$mailService->sendMessage($message);
}
} else {
$emailTemplate->addFooter();
}
// Fall back to system mailer
else {
$senderName = $instanceName;
if ($this->settingsManager->replyToInitiator()) {
$senderName = $this->l->t(
'%1$s via %2$s',
[
$initiatorDisplayName,
$instanceName
]
);
}

$message->useTemplate($emailTemplate);
$failedRecipients = $this->mailer->send($message);
if (!empty($failedRecipients)) {
$this->logger->error('Share notification mail could not be sent to: ' . implode(', ', $failedRecipients));
return;
$message = $this->systemMailer->createMessage();

if (count($emails) > 1) {
$message->setBcc($emails);
} else {
$message->setTo($emails);
}

$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);

if ($initiatorUser && $this->settingsManager->replyToInitiator() && $initiatorEmail !== null) {
$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
}

$message->useTemplate($emailTemplate);
$failed = $this->systemMailer->send($message);

if (!empty($failed)) {
$this->logger->error('Share notification mail could not be sent to: ' . implode(', ', $failed));
return;
}
}
}

Expand Down Expand Up @@ -435,9 +465,9 @@ protected function sendPassword(IShare $share, string $password, array $emails):
$plainBodyPart = $this->l->t('%1$s shared %2$s with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]);
$htmlBodyPart = $this->l->t('%1$s shared %2$s with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]);

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

$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientPasswordNotification', [
$emailTemplate = $this->systemMailer->createEMailTemplate('sharebymail.RecipientPasswordNotification', [
'filename' => $filename,
'password' => $password,
'initiator' => $initiatorDisplayName,
Expand Down Expand Up @@ -496,7 +526,7 @@ protected function sendPassword(IShare $share, string $password, array $emails):
}

$message->useTemplate($emailTemplate);
$failedRecipients = $this->mailer->send($message);
$failedRecipients = $this->systemMailer->send($message);
if (!empty($failedRecipients)) {
$this->logger->error('Share password mail could not be sent to: ' . implode(', ', $failedRecipients));
return false;
Expand All @@ -521,9 +551,9 @@ protected function sendNote(IShare $share): void {
$plainHeading = $this->l->t('%1$s shared %2$s with you and wants to add:', [$initiatorDisplayName, $filename]);
$htmlHeading = $this->l->t('%1$s shared %2$s with you and wants to add', [$initiatorDisplayName, $filename]);

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

$emailTemplate = $this->mailer->createEMailTemplate('shareByMail.sendNote');
$emailTemplate = $this->systemMailer->createEMailTemplate('shareByMail.sendNote');

$emailTemplate->setSubject($this->l->t('%s added a note to a file shared with you', [$initiatorDisplayName]));
$emailTemplate->addHeader();
Expand Down Expand Up @@ -559,7 +589,7 @@ protected function sendNote(IShare $share): void {

$message->setTo([$recipient]);
$message->useTemplate($emailTemplate);
$this->mailer->send($message);
$this->systemMailer->send($message);
}

/**
Expand All @@ -583,8 +613,8 @@ protected function sendPasswordToOwner(IShare $share, string $password): bool {

$bodyPart = $this->l->t('You just shared %1$s with %2$s. The share was already sent to the recipient. Due to the security policies defined by the administrator of %3$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();
$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.OwnerPasswordNotification', [
$message = $this->systemMailer->createMessage();
$emailTemplate = $this->systemMailer->createEMailTemplate('sharebymail.OwnerPasswordNotification', [
'filename' => $filename,
'password' => $password,
'initiator' => $initiatorDisplayName,
Expand Down Expand Up @@ -621,7 +651,7 @@ protected function sendPasswordToOwner(IShare $share, string $password): bool {
$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
$message->setTo([$initiatorEMailAddress => $initiatorDisplayName]);
$message->useTemplate($emailTemplate);
$this->mailer->send($message);
$this->systemMailer->send($message);

$this->createPasswordSendActivity($share, $shareWith, true);

Expand Down
Loading
Loading