Skip to content

Commit aea084d

Browse files
committed
feat(sharebymail): Use MailManager API for notification sender email
Signed-off-by: Abhinav Ohri <[email protected]>
1 parent 021e8f7 commit aea084d

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

apps/sharebymail/lib/ShareByMailProvider.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
use OCP\Share\IShareProviderWithNotification;
4040
use OCP\Util;
4141
use Psr\Log\LoggerInterface;
42+
use OCP\IAppConfig;
43+
use OCP\Mail\Provider\IManager as IMailManager;
4244

4345
/**
4446
* Class ShareByMail
@@ -57,6 +59,7 @@ public function identifier(): string {
5759

5860
public function __construct(
5961
private IConfig $config,
62+
private IAppConfig $appConfig,
6063
private IDBConnection $dbConnection,
6164
private ISecureRandom $secureRandom,
6265
private IUserManager $userManager,
@@ -72,6 +75,7 @@ public function __construct(
7275
private IEventDispatcher $eventDispatcher,
7376
private IShareManager $shareManager,
7477
private IEmailValidator $emailValidator,
78+
private IMailManager $mailManager,
7579
) {
7680
}
7781

@@ -322,6 +326,7 @@ protected function sendEmail(IShare $share, array $emails): void {
322326

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

327332
$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [
@@ -380,7 +385,17 @@ protected function sendEmail(IShare $share, array $emails): void {
380385
]
381386
);
382387
}
383-
$message->setFrom([Util::getEmailAddressForUser($initiatorUser, $instanceName) => $senderName]);
388+
$fromAddress = Util::getDefaultEmailAddress(user_part: $instanceName);
389+
$mailProvidersEnabled = $this->appConfig->getValueBool('core', 'mail_providers_enabled');
390+
if ($mailProvidersEnabled && $this->mailManager->has()) {
391+
if ($initiatorEmail !== null) {
392+
$service = $this->mailManager->findServiceByAddress($initiator, $initiatorEmail);
393+
if ($service !== null) {
394+
$fromAddress = $service->getPrimaryAddress()->getAddress();
395+
}
396+
}
397+
}
398+
$message->setFrom([$fromAddress => $senderName]);
384399

385400
// The "Reply-To" is set to the sharer if an mail address is configured
386401
// also the default footer contains a "Do not reply" which needs to be adjusted.

0 commit comments

Comments
 (0)