From f141c85642184394f03033ba7553716ab35ff856 Mon Sep 17 00:00:00 2001
From: Martin Kravec <kravec.martin@gmail.com>
Date: Tue, 23 Feb 2016 13:52:34 +0100
Subject: [PATCH] Mailer is now able to send name in "from" header

---
 module/VuFind/src/VuFind/Mailer/Mailer.php    | 24 +++++++-----
 .../src/VuFindTest/Mailer/MailerTest.php      | 37 +++++++++++++++++++
 2 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/module/VuFind/src/VuFind/Mailer/Mailer.php b/module/VuFind/src/VuFind/Mailer/Mailer.php
index 5c2c87eae76..15bdc4ce9e0 100644
--- a/module/VuFind/src/VuFind/Mailer/Mailer.php
+++ b/module/VuFind/src/VuFind/Mailer/Mailer.php
@@ -131,11 +131,12 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface
     /**
      * Send an email message.
      *
-     * @param string $to      Recipient email address (or delimited list)
-     * @param string $from    Sender email address
-     * @param string $subject Subject line for message
-     * @param string $body    Message body
-     * @param string $cc      CC recipient (null for none)
+     * @param string                    $to      Recipient email address (or
+     * delimited list)
+     * @param string|\Zend\Mail\Address $from    Sender name and email address
+     * @param string                    $subject Subject line for message
+     * @param string                    $body    Message body
+     * @param string                    $cc      CC recipient (null for none)
      *
      * @throws MailException
      * @return void
@@ -159,10 +160,11 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface
                 throw new MailException('Invalid Recipient Email Address');
             }
         }
-        if (!$validator->isValid($from)) {
+        $fromEmail = ($from instanceof \Zend\Mail\Address)
+            ? $from->getEmail() : $from;
+        if (!$validator->isValid($fromEmail)) {
             throw new MailException('Invalid Sender Email Address');
         }
-
         // Convert all exceptions thrown by mailer into MailException objects:
         try {
             // Send message
@@ -170,7 +172,8 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface
                 ->addFrom($from)
                 ->addTo($recipients)
                 ->setBody($body)
-                ->setSubject($subject);
+                ->setSubject($subject)
+                ->setReplyTo($from);
             if ($cc !== null) {
                 $message->addCc($cc);
             }
@@ -184,7 +187,7 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface
      * Send an email message representing a link.
      *
      * @param string                          $to      Recipient email address
-     * @param string                          $from    Sender email address
+     * @param string|\Zend\Mail\Address       $from    Sender name and email address
      * @param string                          $msg     User notes to include in
      * message
      * @param string                          $url     URL to share
@@ -225,7 +228,8 @@ class Mailer implements \VuFind\I18n\Translator\TranslatorAwareInterface
      * Send an email message representing a record.
      *
      * @param string                            $to      Recipient email address
-     * @param string                            $from    Sender email address
+     * @param string|\Zend\Mail\Address         $from    Sender name and email
+     * address
      * @param string                            $msg     User notes to include in
      * message
      * @param \VuFind\RecordDriver\AbstractBase $record  Record being emailed
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 6b85de76d84..64efbf8a1af 100644
--- a/module/VuFind/tests/unit-tests/src/VuFindTest/Mailer/MailerTest.php
+++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Mailer/MailerTest.php
@@ -27,6 +27,7 @@
  */
 namespace VuFindTest\Mailer;
 use VuFind\Mailer\Mailer;
+use Zend\Mail\Address;
 
 /**
  * Mailer Test Class
@@ -58,6 +59,27 @@ class MailerTest extends \VuFindTest\Unit\TestCase
         $mailer->send('to@example.com', 'from@example.com', 'subject', 'body');
     }
 
+    /**
+     * Test sending an email using an address object for the From field.
+     *
+     * @return void
+     */
+    public function testSendWithAddressObject()
+    {
+        $callback = function ($message) {
+            $fromString = $message->getFrom()->current()->toString();
+            return '<to@example.com>' == $message->getTo()->current()->toString()
+                && 'Sender TextName <from@example.com>' == $fromString
+                && 'body' == $message->getBody()
+                && 'subject' == $message->getSubject();
+        };
+        $transport = $this->getMock('Zend\Mail\Transport\TransportInterface');
+        $transport->expects($this->once())->method('send')->with($this->callback($callback));
+        $address = new Address('from@example.com', 'Sender TextName');
+        $mailer = new Mailer($transport);
+        $mailer->send('to@example.com', $address, 'subject', 'body');
+    }
+
     /**
      * Test bad to address.
      *
@@ -118,6 +140,21 @@ class MailerTest extends \VuFindTest\Unit\TestCase
         $mailer->send('to@example.com', 'bad@bad', 'subject', 'body');
     }
 
+    /**
+     * Test bad from address in Address object.
+     *
+     * @return void
+     *
+     * @expectedException        VuFind\Exception\Mail
+     * @expectedExceptionMessage Invalid Sender Email Address
+     */
+    public function testBadFromInAddressObject()
+    {
+        $transport = $this->getMock('Zend\Mail\Transport\TransportInterface');
+        $mailer = new Mailer($transport);
+        $mailer->send('to@example.com', new Address('bad@bad'), 'subject', 'body');
+    }
+
     /**
      * Test transport exception.
      *
-- 
GitLab