Skip to content

Commit 875711e

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

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
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.

lib/public/Util.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -314,38 +314,6 @@ public static function getDefaultEmailAddress(string $user_part): string {
314314
return $user_part . '@localhost.localdomain';
315315
}
316316

317-
/**
318-
* Returns the email address for the given user
319-
* @param IUser|null $user
320-
* @param string $fallback_user_part the fallback address part
321-
* @return string the email address
322-
*
323-
* If mail providers are enabled, uses the specified user's email address.
324-
* If disabled or the user has no valid email, falls back to the system default.
325-
* @since 31.0.12
326-
*/
327-
public static function getEmailAddressForUser(?IUser $user, string $fallback_user_part): string {
328-
if ($user === null) {
329-
return self::getDefaultEmailAddress($fallback_user_part);
330-
}
331-
332-
$config = \OCP\Server::get(serviceName: IConfig::class);
333-
334-
$mailProvidersEnabled = $config->getAppValue('core', 'mail_providers_enabled', '0') === '1';
335-
336-
if ($mailProvidersEnabled) {
337-
$userEmail = $user->getEMailAddress();
338-
339-
if ($userEmail !== null) {
340-
$emailValidator = \OCP\Server::get(IEmailValidator::class);
341-
if ($emailValidator->isValid($userEmail)) {
342-
return $userEmail;
343-
}
344-
}
345-
}
346-
return self::getDefaultEmailAddress($fallback_user_part);
347-
}
348-
349317
/**
350318
* Converts string to int of float depending on if it fits an int
351319
* @param numeric-string|float|int $number numeric string

0 commit comments

Comments
 (0)