feat(sharebymail): Use MailManager API for notification sender email

Signed-off-by: Abhinav Ohri <abhinavohri13@gmail.com>
This commit is contained in:
Abhinav Ohri 2025-12-18 18:35:48 +05:30
parent 021e8f784d
commit 875711e6b9
2 changed files with 16 additions and 33 deletions

View file

@ -39,6 +39,8 @@ use OCP\Share\IShare;
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 @@ class ShareByMailProvider extends DefaultShareProvider implements IShareProvider
public function __construct(
private IConfig $config,
private IAppConfig $appConfig,
private IDBConnection $dbConnection,
private ISecureRandom $secureRandom,
private IUserManager $userManager,
@ -72,6 +75,7 @@ class ShareByMailProvider extends DefaultShareProvider implements IShareProvider
private IEventDispatcher $eventDispatcher,
private IShareManager $shareManager,
private IEmailValidator $emailValidator,
private IMailManager $mailManager,
) {
}
@ -322,6 +326,7 @@ class ShareByMailProvider extends DefaultShareProvider implements IShareProvider
$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 @@ class ShareByMailProvider extends DefaultShareProvider implements IShareProvider
]
);
}
$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.

View file

@ -314,38 +314,6 @@ class Util {
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