Skip to content

Commit

Permalink
Add a trait for getting sender address.
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala committed Oct 3, 2024
1 parent 995b05e commit aa02ed1
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 11 deletions.
6 changes: 3 additions & 3 deletions module/VuFind/src/VuFind/Auth/EmailAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion module/VuFind/src/VuFind/Auth/LoginTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -60,6 +61,7 @@
*/
class LoginTokenManager implements LoggerAwareInterface, TranslatorAwareInterface
{
use EmailSettingsTrait;
use LoggerAwareTrait;
use TranslatorAwareTrait;

Expand Down Expand Up @@ -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
);
Expand Down
62 changes: 62 additions & 0 deletions module/VuFind/src/VuFind/Config/Feature/EmailSettingsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* Trait providing email settings
*
* PHP version 8
*
* Copyright (C) The National Library of Finland 2024.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Config
* @author Ere Maijala <[email protected]>
* @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 <[email protected]>
* @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'];
}
}
4 changes: 3 additions & 1 deletion module/VuFind/src/VuFind/Controller/AbstractBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,6 +78,7 @@
*/
class AbstractBase extends AbstractActionController implements AccessPermissionInterface, TranslatorAwareInterface
{
use EmailSettingsTrait;
use GetServiceTrait;
use TranslatorAwareTrait;

Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion module/VuFind/src/VuFind/Controller/AlmaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions module/VuFind/src/VuFind/Controller/MyResearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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
);
Expand Down
5 changes: 4 additions & 1 deletion module/VuFind/src/VuFind/Log/LoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,6 +55,8 @@
*/
class LoggerFactory implements FactoryInterface
{
use EmailSettingsTrait;

/**
* Configure database writers.
*
Expand Down Expand Up @@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,6 +65,7 @@
)]
class NotifyCommand extends Command implements TranslatorAwareInterface
{
use EmailSettingsTrait;
use \VuFind\I18n\Translator\TranslatorAwareTrait;
use \VuFind\I18n\Translator\LanguageInitializerTrait;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit aa02ed1

Please sign in to comment.