diff --git a/module/VuFind/src/VuFind/Auth/EmailAuthenticator.php b/module/VuFind/src/VuFind/Auth/EmailAuthenticator.php index 43fecab7b60..4239c470027 100644 --- a/module/VuFind/src/VuFind/Auth/EmailAuthenticator.php +++ b/module/VuFind/src/VuFind/Auth/EmailAuthenticator.php @@ -32,6 +32,7 @@ use Laminas\Http\PhpEnvironment\RemoteAddress; use Laminas\Http\Request; use Laminas\View\Renderer\PhpRenderer; +use VuFind\Config\Feature\EmailSettingsTrait; use VuFind\Db\Service\AuthHashServiceInterface; use VuFind\Exception\Auth as AuthException; use VuFind\Validator\CsrfInterface; @@ -51,6 +52,7 @@ class EmailAuthenticator implements \VuFind\I18n\Translator\TranslatorAwareInterface { use \VuFind\I18n\Translator\TranslatorAwareTrait; + use EmailSettingsTrait; /** * How long a login request is considered to be valid (seconds) @@ -141,9 +143,7 @@ public function sendAuthenticationLink( $viewParams['title'] = $this->config->Site->title; $message = $this->viewRenderer->render($template, $viewParams); - $from = !empty($this->config->Mail->user_email_in_from) - ? $email - : ($this->config->Mail->default_from ?? $this->config->Site->email); + $from = $this->getEmailSenderAddress($this->config, $email); $subject = $this->translator->translate($subject); $subject = str_replace('%%title%%', $viewParams['title'], $subject); diff --git a/module/VuFind/src/VuFind/Auth/LoginTokenManager.php b/module/VuFind/src/VuFind/Auth/LoginTokenManager.php index cf08244a749..e2a5d43378b 100644 --- a/module/VuFind/src/VuFind/Auth/LoginTokenManager.php +++ b/module/VuFind/src/VuFind/Auth/LoginTokenManager.php @@ -37,6 +37,7 @@ use Laminas\Log\LoggerAwareInterface; use Laminas\Session\SessionManager; use Laminas\View\Renderer\RendererInterface; +use VuFind\Config\Feature\EmailSettingsTrait; use VuFind\Cookie\CookieManager; use VuFind\Db\Entity\UserEntityInterface; use VuFind\Db\Service\LoginTokenServiceInterface; @@ -60,6 +61,7 @@ */ class LoginTokenManager implements LoggerAwareInterface, TranslatorAwareInterface { + use EmailSettingsTrait; use LoggerAwareTrait; use TranslatorAwareTrait; @@ -377,7 +379,7 @@ protected function sendLoginTokenWarningEmail(UserEntityInterface $user) try { $this->mailer->send( $toAddr, - $this->config->Mail->default_from ?? $this->config->Site->email, + $this->getEmailSenderAddress($this->config), $this->translate($subject, ['%%title%%' => $title]), $message ); diff --git a/module/VuFind/src/VuFind/Config/Feature/EmailSettingsTrait.php b/module/VuFind/src/VuFind/Config/Feature/EmailSettingsTrait.php new file mode 100644 index 00000000000..47c8d3630d7 --- /dev/null +++ b/module/VuFind/src/VuFind/Config/Feature/EmailSettingsTrait.php @@ -0,0 +1,62 @@ + + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org Main Site + */ + +namespace VuFind\Config\Feature; + +use Laminas\Config\Config; + +/** + * Trait providing email settings + * + * @category VuFind + * @package Config + * @author Ere Maijala + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org Main Site + */ +trait EmailSettingsTrait +{ + /** + * Get sender email address + * + * @param array|Config $config VuFind configuration + * @param ?string $userEmail User's own email address that is used if permitted by settings + * + * @return string + */ + protected function getEmailSenderAddress(array|Config $config, ?string $userEmail = null): string + { + if ($config instanceof Config) { + $config = $config->toArray(); + } + return (null !== $userEmail && ($config['Mail']['user_email_in_from'] ?? false)) + ? $userEmail + : $config['Mail']['default_from'] ?? $config['Site']['email']; + } +} diff --git a/module/VuFind/src/VuFind/Controller/AbstractBase.php b/module/VuFind/src/VuFind/Controller/AbstractBase.php index 06f6a2ac56e..aa15c7d4a91 100644 --- a/module/VuFind/src/VuFind/Controller/AbstractBase.php +++ b/module/VuFind/src/VuFind/Controller/AbstractBase.php @@ -36,6 +36,7 @@ use Laminas\ServiceManager\ServiceLocatorInterface; use Laminas\Uri\Http; use Laminas\View\Model\ViewModel; +use VuFind\Config\Feature\EmailSettingsTrait; use VuFind\Controller\Feature\AccessPermissionInterface; use VuFind\Db\Entity\UserEntityInterface; use VuFind\Exception\Auth as AuthException; @@ -77,6 +78,7 @@ */ class AbstractBase extends AbstractActionController implements AccessPermissionInterface, TranslatorAwareInterface { + use EmailSettingsTrait; use GetServiceTrait; use TranslatorAwareTrait; @@ -266,7 +268,7 @@ protected function createEmailViewModel($params = null, $defaultSubject = null) // Fail if we're missing a from and the form element is disabled: if ($view->disableFrom) { if (empty($view->from)) { - $view->from = $config->Mail->default_from ?? $config->Site->email; + $view->from = $this->getEmailSenderAddress($config); } if (empty($view->from)) { throw new \Exception('Unable to determine email from address'); diff --git a/module/VuFind/src/VuFind/Controller/AlmaController.php b/module/VuFind/src/VuFind/Controller/AlmaController.php index 564fffa4df2..0f72c2e20c6 100644 --- a/module/VuFind/src/VuFind/Controller/AlmaController.php +++ b/module/VuFind/src/VuFind/Controller/AlmaController.php @@ -379,7 +379,7 @@ protected function sendSetPasswordEmail(UserEntityInterface $user, $config) // Send the email $this->getService(\VuFind\Mailer\Mailer::class)->send( $user->getEmail(), - $config->Mail->default_from ?? $config->Site->email, + $this->getEmailSenderAddress($config), $this->translate( 'new_user_welcome_subject', ['%%library%%' => $config->Site->title] diff --git a/module/VuFind/src/VuFind/Controller/MyResearchController.php b/module/VuFind/src/VuFind/Controller/MyResearchController.php index f67ac19a43b..f2a4e7e4c03 100644 --- a/module/VuFind/src/VuFind/Controller/MyResearchController.php +++ b/module/VuFind/src/VuFind/Controller/MyResearchController.php @@ -1782,7 +1782,7 @@ protected function sendRecoveryEmail(UserEntityInterface $user, $config) ); $this->getService(Mailer::class)->send( $user->getEmail(), - $config->Mail->default_from ?? $config->Site->email, + $this->getEmailSenderAddress($config), $this->translate('recovery_email_subject'), $message ); @@ -1843,7 +1843,7 @@ protected function sendChangeNotificationEmail($user, $newEmail) // address; if they have a pending address change, use that. $this->getService(Mailer::class)->send( $user->getEmail(), - $config->Mail->default_from ?? $config->Site->email, + $this->getEmailSenderAddress($config), $this->translate('change_notification_email_subject'), $message ); @@ -1893,7 +1893,7 @@ protected function sendVerificationEmail($user, $change = false) $to = ($pending = $user->getPendingEmail()) ? $pending : $user->getEmail(); $this->getService(Mailer::class)->send( $to, - $config->Mail->default_from ?? $config->Site->email, + $this->getEmailSenderAddress($config), $this->translate('verification_email_subject'), $message ); diff --git a/module/VuFind/src/VuFind/Log/LoggerFactory.php b/module/VuFind/src/VuFind/Log/LoggerFactory.php index 954f7fe4b91..a911be3c47b 100644 --- a/module/VuFind/src/VuFind/Log/LoggerFactory.php +++ b/module/VuFind/src/VuFind/Log/LoggerFactory.php @@ -36,6 +36,7 @@ use Laminas\ServiceManager\Factory\FactoryInterface; use Psr\Container\ContainerExceptionInterface as ContainerException; use Psr\Container\ContainerInterface; +use VuFind\Config\Feature\EmailSettingsTrait; use function count; use function is_array; @@ -54,6 +55,8 @@ */ class LoggerFactory implements FactoryInterface { + use EmailSettingsTrait; + /** * Configure database writers. * @@ -112,7 +115,7 @@ protected function addEmailWriters( // use smtp $mailer = $container->get(\VuFind\Mailer\Mailer::class); $msg = $mailer->getNewMessage() - ->addFrom($config->Mail->default_from ?? $config->Site->email) + ->addFrom($this->getEmailSenderAddress($config)) ->addTo($email) ->setSubject('VuFind Log Message'); diff --git a/module/VuFindConsole/src/VuFindConsole/Command/ScheduledSearch/NotifyCommand.php b/module/VuFindConsole/src/VuFindConsole/Command/ScheduledSearch/NotifyCommand.php index 704a6328c01..18e2fdfd772 100644 --- a/module/VuFindConsole/src/VuFindConsole/Command/ScheduledSearch/NotifyCommand.php +++ b/module/VuFindConsole/src/VuFindConsole/Command/ScheduledSearch/NotifyCommand.php @@ -37,6 +37,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use VuFind\Config\Feature\EmailSettingsTrait; use VuFind\Crypt\SecretCalculator; use VuFind\Db\Entity\SearchEntityInterface; use VuFind\Db\Entity\UserEntityInterface; @@ -64,6 +65,7 @@ )] class NotifyCommand extends Command implements TranslatorAwareInterface { + use EmailSettingsTrait; use \VuFind\I18n\Translator\TranslatorAwareTrait; use \VuFind\I18n\Translator\LanguageInitializerTrait; @@ -409,7 +411,7 @@ protected function sendEmail($user, $message) { $subject = $this->mainConfig->Site->title . ': ' . $this->translate('Scheduled Alert Results'); - $from = $this->config->Mail->default_from ?? $this->mainConfig->Site->email; + $from = $this->getEmailSenderAddress($this->mainConfig); $to = $user->getEmail(); try { $this->mailer->send($to, $from, $subject, $message);