diff --git a/config/vufind/config.ini b/config/vufind/config.ini index 5bc3467b7ba4536d38b7bf5791408dc80cb3fd74..b1d5dc0da2f5de29c4c9058255dd3100cdb0434d 100644 --- a/config/vufind/config.ini +++ b/config/vufind/config.ini @@ -528,6 +528,8 @@ disable_from = false ; From field override. Setting this allows keeping the "from" field in email forms ; but will only use it as a reply-to address. The address defined here is used as the ; actual "from" address. +; Note: If a feature explicitly sets a different reply-to address (for example, +; Feedback forms), the original from address will NOT override that reply-to value. ;override_from = "no-reply@myuniversity.edu" ; Being a special case of mail message, sending record results via SMS ("Text this") diff --git a/module/VuFind/src/VuFind/Controller/FeedbackController.php b/module/VuFind/src/VuFind/Controller/FeedbackController.php index 41cd71c232e6c98b250d59b0ead0251314333e52..7ff54f2412c48f33de65619d0706ee989636f2d9 100644 --- a/module/VuFind/src/VuFind/Controller/FeedbackController.php +++ b/module/VuFind/src/VuFind/Controller/FeedbackController.php @@ -149,9 +149,6 @@ class FeedbackController extends AbstractBase ) { try { $mailer = $this->serviceLocator->get(\VuFind\Mailer\Mailer::class); - if ($replyToEmail) { - $mailer->setFromAddressOverride(''); - } $mailer->send( new Address($recipientEmail, $recipientName), new Address($senderEmail, $senderName), diff --git a/module/VuFind/src/VuFind/Mailer/Mailer.php b/module/VuFind/src/VuFind/Mailer/Mailer.php index 193cb5823cac9a835d16c8ae789d60cc6f8fe74c..ad096ba9a5431ccc1d917dd5d47d22e767326418 100644 --- a/module/VuFind/src/VuFind/Mailer/Mailer.php +++ b/module/VuFind/src/VuFind/Mailer/Mailer.php @@ -186,7 +186,11 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface if (!empty($this->fromAddressOverride) && $this->fromAddressOverride != $fromEmail ) { - $replyTo->add($fromEmail); + // Add the original from address as the reply-to address unless + // a reply-to address has been specified + if (count($replyTo) === 0) { + $replyTo->add($fromEmail); + } if (!($from instanceof Address)) { $from = new Address($from); } diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Mailer/MailerTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Mailer/MailerTest.php index fc07636a14d35f2b787c851b27a632440e508caa..4f6d852489d6597d9ca3ae1ca7d44d0f5ef87f99 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/Mailer/MailerTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Mailer/MailerTest.php @@ -148,6 +148,52 @@ class MailerTest extends \VuFindTest\Unit\TestCase $mailer->send('to@example.com', $address, 'subject', 'body'); } + /** + * Test sending an email using an explicitly set reply-to address. + * + * @return void + */ + public function testSendWithReplyTo() + { + $callback = function ($message) { + $fromString = $message->getFrom()->current()->toString(); + return '<to@example.com>' == $message->getTo()->current()->toString() + && '<reply-to@example.com>' == $message->getReplyTo()->current()->toString() + && '<me@example.com>' == $fromString + && 'body' == $message->getBody() + && 'subject' == $message->getSubject(); + }; + $transport = $this->createMock(\Zend\Mail\Transport\TransportInterface::class); + $transport->expects($this->once())->method('send')->with($this->callback($callback)); + $address = new Address('me@example.com'); + $mailer = new Mailer($transport); + $mailer->send('to@example.com', $address, 'subject', 'body', null, 'reply-to@example.com'); + } + + /** + * Test sending an email using a from address override + * and an explicitly set reply-to address. + * + * @return void + */ + public function testSendWithFromOverrideAndReplyTo() + { + $callback = function ($message) { + $fromString = $message->getFrom()->current()->toString(); + return '<to@example.com>' == $message->getTo()->current()->toString() + && '<reply-to@example.com>' == $message->getReplyTo()->current()->toString() + && 'me <no-reply@example.com>' == $fromString + && 'body' == $message->getBody() + && 'subject' == $message->getSubject(); + }; + $transport = $this->createMock(\Zend\Mail\Transport\TransportInterface::class); + $transport->expects($this->once())->method('send')->with($this->callback($callback)); + $address = new Address('me@example.com'); + $mailer = new Mailer($transport); + $mailer->setFromAddressOverride('no-reply@example.com'); + $mailer->send('to@example.com', $address, 'subject', 'body', null, 'reply-to@example.com'); + } + /** * Test bad to address. *