Skip to content

Commit 021e8f7

Browse files
committed
feat(sharebymail): use initiator's email as sender if mail providers enabled
Signed-off-by: Abhinav Ohri <[email protected]>
1 parent 0edaff9 commit 021e8f7

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

apps/sharebymail/lib/ShareByMailProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ protected function sendEmail(IShare $share, array $emails): void {
380380
]
381381
);
382382
}
383-
$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
383+
$message->setFrom([Util::getEmailAddressForUser($initiatorUser, $instanceName) => $senderName]);
384384

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

lib/public/Util.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,38 @@ 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+
317349
/**
318350
* Converts string to int of float depending on if it fits an int
319351
* @param numeric-string|float|int $number numeric string

0 commit comments

Comments
 (0)