Skip to content
Snippets Groups Projects
Commit ca07a851 authored by Samuli Sillanpää's avatar Samuli Sillanpää Committed by Robert Lange
Browse files

Prevent multiple reply-to addresses in Mailer. (#1406)

- Includes tests.
parent cf022f3b
No related merge requests found
......@@ -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")
......
......@@ -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),
......
......@@ -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);
}
......
......@@ -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.
*
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment