From 021e8f784d49f9d9b4b6ab14bca27deba4250fae Mon Sep 17 00:00:00 2001 From: Abhinav Ohri Date: Sun, 14 Dec 2025 19:41:16 +0530 Subject: [PATCH 1/2] feat(sharebymail): use initiator's email as sender if mail providers enabled Signed-off-by: Abhinav Ohri --- apps/sharebymail/lib/ShareByMailProvider.php | 2 +- lib/public/Util.php | 32 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index 16e3068b15b90..c31a6ada1ffcb 100644 --- a/apps/sharebymail/lib/ShareByMailProvider.php +++ b/apps/sharebymail/lib/ShareByMailProvider.php @@ -380,7 +380,7 @@ protected function sendEmail(IShare $share, array $emails): void { ] ); } - $message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]); + $message->setFrom([Util::getEmailAddressForUser($initiatorUser, $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. diff --git a/lib/public/Util.php b/lib/public/Util.php index 70a862880f19d..f33ac488ed4c1 100644 --- a/lib/public/Util.php +++ b/lib/public/Util.php @@ -314,6 +314,38 @@ public static function getDefaultEmailAddress(string $user_part): string { return $user_part . '@localhost.localdomain'; } + /** + * Returns the email address for the given user + * @param IUser|null $user + * @param string $fallback_user_part the fallback address part + * @return string the email address + * + * If mail providers are enabled, uses the specified user's email address. + * If disabled or the user has no valid email, falls back to the system default. + * @since 31.0.12 + */ + public static function getEmailAddressForUser(?IUser $user, string $fallback_user_part): string { + if ($user === null) { + return self::getDefaultEmailAddress($fallback_user_part); + } + + $config = \OCP\Server::get(serviceName: IConfig::class); + + $mailProvidersEnabled = $config->getAppValue('core', 'mail_providers_enabled', '0') === '1'; + + if ($mailProvidersEnabled) { + $userEmail = $user->getEMailAddress(); + + if ($userEmail !== null) { + $emailValidator = \OCP\Server::get(IEmailValidator::class); + if ($emailValidator->isValid($userEmail)) { + return $userEmail; + } + } + } + return self::getDefaultEmailAddress($fallback_user_part); + } + /** * Converts string to int of float depending on if it fits an int * @param numeric-string|float|int $number numeric string From 875711e6b93bd14b72d98e77f83d7983170a4ec2 Mon Sep 17 00:00:00 2001 From: Abhinav Ohri Date: Thu, 18 Dec 2025 18:35:48 +0530 Subject: [PATCH 2/2] feat(sharebymail): Use MailManager API for notification sender email Signed-off-by: Abhinav Ohri --- apps/sharebymail/lib/ShareByMailProvider.php | 17 ++++++++++- lib/public/Util.php | 32 -------------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index c31a6ada1ffcb..49f7549cb3324 100644 --- a/apps/sharebymail/lib/ShareByMailProvider.php +++ b/apps/sharebymail/lib/ShareByMailProvider.php @@ -39,6 +39,8 @@ use OCP\Share\IShareProviderWithNotification; use OCP\Util; use Psr\Log\LoggerInterface; +use OCP\IAppConfig; +use OCP\Mail\Provider\IManager as IMailManager; /** * Class ShareByMail @@ -57,6 +59,7 @@ public function identifier(): string { public function __construct( private IConfig $config, + private IAppConfig $appConfig, private IDBConnection $dbConnection, private ISecureRandom $secureRandom, private IUserManager $userManager, @@ -72,6 +75,7 @@ public function __construct( private IEventDispatcher $eventDispatcher, private IShareManager $shareManager, private IEmailValidator $emailValidator, + private IMailManager $mailManager, ) { } @@ -322,6 +326,7 @@ protected function sendEmail(IShare $share, array $emails): void { $initiatorUser = $this->userManager->get($initiator); $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; + $initiatorEmail = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null; $message = $this->mailer->createMessage(); $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [ @@ -380,7 +385,17 @@ protected function sendEmail(IShare $share, array $emails): void { ] ); } - $message->setFrom([Util::getEmailAddressForUser($initiatorUser, $instanceName) => $senderName]); + $fromAddress = Util::getDefaultEmailAddress(user_part: $instanceName); + $mailProvidersEnabled = $this->appConfig->getValueBool('core', 'mail_providers_enabled'); + if ($mailProvidersEnabled && $this->mailManager->has()) { + if ($initiatorEmail !== null) { + $service = $this->mailManager->findServiceByAddress($initiator, $initiatorEmail); + if ($service !== null) { + $fromAddress = $service->getPrimaryAddress()->getAddress(); + } + } + } + $message->setFrom([$fromAddress => $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. diff --git a/lib/public/Util.php b/lib/public/Util.php index f33ac488ed4c1..70a862880f19d 100644 --- a/lib/public/Util.php +++ b/lib/public/Util.php @@ -314,38 +314,6 @@ public static function getDefaultEmailAddress(string $user_part): string { return $user_part . '@localhost.localdomain'; } - /** - * Returns the email address for the given user - * @param IUser|null $user - * @param string $fallback_user_part the fallback address part - * @return string the email address - * - * If mail providers are enabled, uses the specified user's email address. - * If disabled or the user has no valid email, falls back to the system default. - * @since 31.0.12 - */ - public static function getEmailAddressForUser(?IUser $user, string $fallback_user_part): string { - if ($user === null) { - return self::getDefaultEmailAddress($fallback_user_part); - } - - $config = \OCP\Server::get(serviceName: IConfig::class); - - $mailProvidersEnabled = $config->getAppValue('core', 'mail_providers_enabled', '0') === '1'; - - if ($mailProvidersEnabled) { - $userEmail = $user->getEMailAddress(); - - if ($userEmail !== null) { - $emailValidator = \OCP\Server::get(IEmailValidator::class); - if ($emailValidator->isValid($userEmail)) { - return $userEmail; - } - } - } - return self::getDefaultEmailAddress($fallback_user_part); - } - /** * Converts string to int of float depending on if it fits an int * @param numeric-string|float|int $number numeric string